Files
meteor/packages/tinytest/tinytest_server.js
David Glasser 32035e8039 Replace _ServerTestResults collection with custom pub/sub.
This gets rid of some awkward GC code, and as a bonus gives us a second example
usage of the registerStore API.
2012-10-29 12:21:34 -07:00

64 lines
1.8 KiB
JavaScript

(function () {
var handlesForRun = {};
var reportsForRun = {};
Meteor.publish(Meteor._ServerTestResultsSubscription, function (runId) {
var self = this;
if (!_.has(handlesForRun, runId))
handlesForRun[runId] = [self];
else
handlesForRun[runId].push(self);
self.onStop(function () {
handlesForRun[runId] = _.without(handlesForRun[runId], self);
});
if (_.has(reportsForRun, runId)) {
self.set(Meteor._ServerTestResultsCollection, runId,
reportsForRun[runId]);
}
self.complete();
self.flush();
});
Meteor.methods({
'tinytest/run': function (runId) {
this.unblock();
// XXX using private API === lame
var Future = __meteor_bootstrap__.require('fibers/future');
var future = new Future;
reportsForRun[runId] = {};
var onReport = function (report) {
if (! Fiber.current) {
Meteor._debug("Trying to report a test not in a fiber! "+
"You probably forgot to wrap a callback in bindEnvironment.");
console.trace();
}
var dummyKey = Meteor.uuid();
var setObject = {};
setObject[dummyKey] = report;
_.each(handlesForRun[runId], function (handle) {
handle.set(Meteor._ServerTestResultsCollection, runId, setObject);
handle.flush();
});
// Save for future subscriptions.
reportsForRun[runId][dummyKey] = report;
};
var onComplete = function() {
_.each(handlesForRun[runId], function (handle) {
handle.stop();
});
delete handlesForRun[runId];
delete reportsForRun[runId];
future.ret();
};
Meteor._runTests(onReport, onComplete);
future.wait();
}
});
}());