test doc fetcher

This commit is contained in:
David Glasser
2013-09-18 13:41:24 -07:00
parent 85ada6a874
commit 33528ddbc3
4 changed files with 43 additions and 3 deletions

View File

@@ -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();
}

View File

@@ -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);
});

View File

@@ -1617,5 +1617,6 @@ MongoInternals.Connection = MongoConnection;
MongoInternals.NpmModule = MongoDB;
MongoTest = {
cursorSupportedByOplogTailing: cursorSupportedByOplogTailing
cursorSupportedByOplogTailing: cursorSupportedByOplogTailing,
DocFetcher: DocFetcher
};

View File

@@ -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');
});