forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This should use fewer fibers. nim, can you benchmark?
- Loading branch information
Showing
3 changed files
with
86 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +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}); | ||
testAsyncMulti("mongo-livedata - doc fetcher", [ | ||
function (test, expect) { | ||
var self = this; | ||
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); | ||
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); | ||
// Test basic operation. | ||
fetcher.fetch(collName, id1, Random.id(), expect(null, {_id: id1, x: 1})); | ||
fetcher.fetch(collName, "nonexistent!", Random.id(), expect(null, 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); | ||
var fetched = false; | ||
var cacheKey = Random.id(); | ||
var expected = {_id: id2, y: 2}; | ||
fetcher.fetch(collName, id2, cacheKey, expect(function (e, d) { | ||
fetched = true; | ||
test.isFalse(e); | ||
test.equal(d, expected); | ||
})); | ||
// 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); | ||
}); | ||
// 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. | ||
fetcher.fetch(collName, Random.id(), cacheKey, expect(function (e, d) { | ||
test.isFalse(e); | ||
test.equal(d, expected); | ||
})); | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters