From ea062a888ecb682d9e46ac1bc8b39011d675318d Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Tue, 14 Oct 2014 14:55:25 -0700 Subject: [PATCH] unit test for parallel calls to multi-cache --- test/multi_caching.unit.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 78835cb..6521d9a 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -1,4 +1,5 @@ var assert = require('assert'); +var async = require('async'); var sinon = require('sinon'); var support = require('./support'); var check_err = support.check_err; @@ -438,6 +439,41 @@ describe("multi_caching", function () { }); }); }); + + describe("when called multiple times in parallel with same key", function () { + var construct; + + beforeEach(function () { + multi_cache = multi_caching([memory_cache, memory_cache3]); + + construct = sinon.spy(function (val, cb) { + var timeout = support.random.number(100); + setTimeout(function () { + cb(null, 'value'); + }, timeout); + }); + }); + + it("calls the wrapped function once", function (done) { + var values = []; + for (var i = 0; i < 5; i++) { + values.push(i); + } + + async.each(values, function (val, async_cb) { + multi_cache.wrap('key', function (cb) { + construct(val, cb); + }, function (err, result) { + assert.equal(result, 'value'); + async_cb(err); + }); + }, function (err) { + check_err(err); + assert.equal(construct.callCount, 1); + done(); + }); + }); + }); }); context("when instantiated with a non-Array 'caches' arg", function () {