From 654923abaec1d5f2471123c021f320ee4b436bab Mon Sep 17 00:00:00 2001 From: Naomi Seyfer Date: Thu, 11 Apr 2013 16:02:41 -0700 Subject: [PATCH] cleaning up server to server connection tests --- packages/livedata/livedata_tests.js | 36 ++++++++++--------- .../mongo-livedata/mongo_livedata_tests.js | 6 ++-- packages/test-helpers/async_multi.js | 20 +++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/livedata/livedata_tests.js b/packages/livedata/livedata_tests.js index 99486f4de8..f9e234aa84 100644 --- a/packages/livedata/livedata_tests.js +++ b/packages/livedata/livedata_tests.js @@ -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); } ]); diff --git a/packages/mongo-livedata/mongo_livedata_tests.js b/packages/mongo-livedata/mongo_livedata_tests.js index 892778b60b..3c1eb33538 100644 --- a/packages/mongo-livedata/mongo_livedata_tests.js +++ b/packages/mongo-livedata/mongo_livedata_tests.js @@ -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) { diff --git a/packages/test-helpers/async_multi.js b/packages/test-helpers/async_multi.js index 643301635c..56a93ca0d2 100644 --- a/packages/test-helpers/async_multi.js +++ b/packages/test-helpers/async_multi.js @@ -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(); +};