cleaning up server to server connection tests

This commit is contained in:
Naomi Seyfer
2013-04-11 16:02:41 -07:00
parent 9b2b6f219d
commit 654923abae
3 changed files with 42 additions and 20 deletions

View File

@@ -607,18 +607,19 @@ if (Meteor.isServer) {
});
}
(function () {
var conn;
testAsyncMulti("livedata - connect works from both client and server", [
function (test, expect) {
conn = Meteor.connect(Meteor.absoluteUrl());
Meteor.setTimeout(expect(function () {
test.isTrue(conn.status().connected, "Not connected");
}), 500);
var self = this;
self.conn = Meteor.connect(Meteor.absoluteUrl());
pollUntil(expect, function () {
return self.conn.status().connected;
}, 10000);
},
function (test, expect) {
if (conn.status().connected) {
conn.call('s2s', 'foo', expect(function (err, res) {
var self = this;
if (self.conn.status().connected) {
self.conn.call('s2s', 'foo', expect(function (err, res) {
if (err)
throw err;
test.equal(res, "s2s foo");
@@ -630,18 +631,19 @@ if (Meteor.isServer) {
if (Meteor.isServer) {
(function () {
var conn;
testAsyncMulti("livedata - method call on server blocks in a fiber way", [
function (test, expect) {
conn = Meteor.connect(Meteor.absoluteUrl());
Meteor.setTimeout(expect(function () {
test.isTrue(conn.status().connected, "Not connected");
}), 500);
var self = this;
self.conn = Meteor.connect(Meteor.absoluteUrl());
pollUntil(expect, function () {
return self.conn.status().connected;
}, 10000);
},
function (test, expect) {
if (conn.status().connected) {
test.equal(conn.call('s2s', 'foo'), "s2s foo");
var self = this;
if (self.conn.status().connected) {
test.equal(self.conn.call('s2s', 'foo'), "s2s foo");
}
}
]);
@@ -649,12 +651,12 @@ if (Meteor.isServer) {
}
(function () {
var conn;
testAsyncMulti("livedata - connect fails to unknown place", [
function (test, expect) {
conn = Meteor.connect("example.com");
var self = this;
self.conn = Meteor.connect("example.com");
Meteor.setTimeout(expect(function () {
test.isFalse(conn.status().connected, "Not connected");
test.isFalse(self.conn.status().connected, "Not connected");
}), 500);
}
]);

View File

@@ -836,9 +836,9 @@ if (Meteor.isServer) {
});
self.conn = Meteor.connect(Meteor.absoluteUrl());
Meteor.setTimeout(expect(function () {
test.isTrue(self.conn.status().connected, "Not connected");
}), 500);
pollUntil(expect, function () {
return self.conn.status().connected;
}, 10000);
},
function (test, expect) {

View File

@@ -151,3 +151,23 @@ _.extend(ExpectationManager.prototype, {
runNext();
});
};
/*global*/
pollUntil = function (expect, f, timeout, step) {
step = step || 100;
var expectation = expect(true);
var start = (new Date()).valueOf();
var helper = function () {
if (f()) {
expectation(true);
return;
}
if (start + timeout < (new Date()).valueOf()) {
expectation(false);
return;
}
Meteor.setTimeout(helper, step);
};
helper();
};