diff --git a/packages/mongo-livedata/doc_fetcher.js b/packages/mongo-livedata/doc_fetcher.js index 11051b6129..a33eabc755 100644 --- a/packages/mongo-livedata/doc_fetcher.js +++ b/packages/mongo-livedata/doc_fetcher.js @@ -25,7 +25,7 @@ _.extend(DocFetcher.prototype, { // it's done and return whatever it returns. if (_.has(self._futuresForCacheKey, cacheKey)) { var f = new Future; - self._futuresForCacheKey.push(f); + self._futuresForCacheKey[cacheKey].push(f); return f.wait(); } diff --git a/packages/mongo-livedata/doc_fetcher_tests.js b/packages/mongo-livedata/doc_fetcher_tests.js new file mode 100644 index 0000000000..cf4e05a8d0 --- /dev/null +++ b/packages/mongo-livedata/doc_fetcher_tests.js @@ -0,0 +1,38 @@ +var Fiber = Npm.require('fibers'); +var Future = Npm.require('fibers/future'); + +Tinytest.add("mongo-livedata - doc fetcher", function (test) { + var collName = "docfetcher-" + Random.id(); + var collection = new Meteor.Collection(collName); + var id1 = collection.insert({x: 1}); + var id2 = collection.insert({y: 2}); + + var fetcher = new MongoTest.DocFetcher( + MongoInternals.defaultRemoteCollectionDriver().mongo); + + // Test basic operation. + test.equal(fetcher.fetch(collName, id1, Random.id()), + {_id: id1, x: 1}); + test.equal(fetcher.fetch(collName, "nonexistent!", Random.id()), null); + + var future = new Future; + var fetched = false; + var cacheKey = Random.id(); + Fiber(function () { + var d = fetcher.fetch(collName, id2, cacheKey); + fetched = true; + future.return(d); + }).run(); + // The fetcher yields: + test.isFalse(fetched); + + // Now ask for another document with the same cache key. Because a fetch for + // that cache key is in flight, we will get the other fetch's document, not + // this random document. + var doc2a = fetcher.fetch(collName, Random.id(), cacheKey); + // Finally, wait for the original fetch to return: + var doc2b = future.wait(); + var expected = {_id: id2, y: 2}; + test.equal(doc2a, expected); + test.equal(doc2b, expected); +}); diff --git a/packages/mongo-livedata/mongo_driver.js b/packages/mongo-livedata/mongo_driver.js index 925cff8b53..19f604b8d0 100644 --- a/packages/mongo-livedata/mongo_driver.js +++ b/packages/mongo-livedata/mongo_driver.js @@ -1617,5 +1617,6 @@ MongoInternals.Connection = MongoConnection; MongoInternals.NpmModule = MongoDB; MongoTest = { - cursorSupportedByOplogTailing: cursorSupportedByOplogTailing + cursorSupportedByOplogTailing: cursorSupportedByOplogTailing, + DocFetcher: DocFetcher }; diff --git a/packages/mongo-livedata/package.js b/packages/mongo-livedata/package.js index 5389cd1157..b9179eb05e 100644 --- a/packages/mongo-livedata/package.js +++ b/packages/mongo-livedata/package.js @@ -36,7 +36,7 @@ Package.on_use(function (api) { // Stuff that should be exposed via a real API, but we haven't yet. api.export('MongoInternals', 'server'); // For tests only. - api.export('MongoTest', 'server'); + api.export('MongoTest', 'server', {testOnly: true}); api.add_files(['id_map.js', 'doc_fetcher.js'], 'server'); api.add_files('mongo_driver.js', 'server'); @@ -57,4 +57,5 @@ Package.on_test(function (api) { api.add_files('collection_tests.js', ['client', 'server']); api.add_files('observe_changes_tests.js', ['client', 'server']); api.add_files('oplog_tests.js', 'server'); + api.add_files('doc_fetcher_tests.js', 'server'); });