YUKI Hiroshi
null+****@clear*****
Wed Feb 6 13:02:43 JST 2013
YUKI Hiroshi 2013-02-06 13:02:43 +0900 (Wed, 06 Feb 2013) New Revision: 96668996d271227bfec2e11bf1ea68bc2b232e4a https://github.com/groonga/express-kotoumi/commit/96668996d271227bfec2e11bf1ea68bc2b232e4a Log: test: Run asynchronous setup correctly Modified files: test/backend-connection.test.js Modified: test/backend-connection.test.js (+50 -19) =================================================================== --- test/backend-connection.test.js 2013-02-06 12:50:47 +0900 (83bc3d0) +++ test/backend-connection.test.js 2013-02-06 13:02:43 +0900 (7cd0c1a) @@ -70,6 +70,10 @@ suite('Connection, initialization', function() { suite('Connection, basic features', function() { var connection; var backend; + var setupDone; + function assertReady() { + assert.equal(setupDone, true, 'setup process is not finished yet!'); + } setup(function(done) { createBackend() @@ -83,6 +87,7 @@ suite('Connection, basic features', function() { maxRetyrCount: 3, retryDelay: 1 }); + setupDone = true; done(); }); }); @@ -96,6 +101,7 @@ suite('Connection, basic features', function() { connection.close(); connection = undefined; } + setupDone = false; }); function createExpectedEnvelope(type, body) { @@ -127,6 +133,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); message = connection.emitMessage('testRequest', { command: 'foobar' }); assert.envelopeEqual(message, createExpectedEnvelope('testRequest', @@ -161,19 +168,26 @@ suite('Connection, basic features', function() { test('receiving message from the backend', function(done) { var callback = createMockedMessageCallback(); - connection.on('message', callback); - - var now = new Date(); - var message = { - id: now.getTime(), - date: now.toISOString(), - statusCode: 200, - type: 'testResponse', - body: 'first call' - }; - callback.takes(message); - var packet = ['test.message', Date.now(), message]; - utils.sendPacketTo(packet, utils.testReceivePort) + var message; + var packet; + Deferred + .wait(0.01) + .next(function() { + assertReady(); + connection.on('message', callback); + + var now = new Date(); + message = { + id: now.getTime(), + date: now.toISOString(), + statusCode: 200, + type: 'testResponse', + body: 'first call' + }; + callback.takes(message); + packet = ['test.message', Date.now(), message]; + return utils.sendPacketTo(packet, utils.testReceivePort); + }) .next(function() { callback.assert(); @@ -205,6 +219,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); message = connection.emitMessage('testRequest', { command: 'foobar' }, callback); @@ -250,6 +265,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); message = connection.emitMessage('testRequest', { command: 'foobar' }, callback); @@ -288,6 +304,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); message = connection.emitMessage('testRequest', { command: 'foobar' }, callback, @@ -331,6 +348,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); callback.takes(Connection.ERROR_GATEWAY_TIMEOUT, null); message = connection.emitMessage('testRequest', { command: 'foobar' }, @@ -369,6 +387,7 @@ suite('Connection, basic features', function() { Deferred .wait(0.01) .next(function() { + assertReady(); message = connection.emitMessage('testRequest', { command: 'foobar' }, callback, @@ -407,6 +426,10 @@ suite('Connection, basic features', function() { suite('Connection, to backend', function() { var connection; var backend; + var setupDone; + function assertReady() { + assert.equal(setupDone, true, 'setup process is not finished yet!'); + } setup(function(done) { createBackend() @@ -419,6 +442,7 @@ suite('Connection, to backend', function() { maxRetyrCount: 3, retryDelay: 1 }); + setupDone = true; done(); }); }); @@ -432,17 +456,21 @@ suite('Connection, to backend', function() { connection.close(); connection = undefined; } + setupDone = false; }); test('normal messaging', function(done) { Deferred .wait(0.01) .next(function() { + assertReady(); connection.emitMessage({ message: true }); }) .wait(0.01) .next(function() { - assert.equal(backend.received.length, 1, 'message should be sent'); + assert.equal(backend.received.length, + 1, + 'message should be sent: ' + JSON.stringify(backend.received)); assert.equal(backend.received[0][0], 'test.message'); done(); }) @@ -457,11 +485,14 @@ suite('Connection, to backend', function() { Deferred .wait(0.01) .next(function() { + assertReady(); connection.emitMessage('test', { message: true }); }) .wait(0.01) .next(function() { - assert.equal(backend.received.length, 1, 'message should be sent'); + assert.equal(backend.received.length, + 1, + 'message should be sent: ' + JSON.stringify(backend.received)); assert.equal(backend.received[0][0], 'test.message'); backend.close(); @@ -487,10 +518,10 @@ suite('Connection, to backend', function() { errorHandler.assertThrows(); assert.equal(backend.received.length, 1, - 'no new message should be sent to the old backend'); + 'no new message should be sent to the old backend' + JSON.stringify(backend.received)); assert.equal(restartedBackend.received.length, 0, - 'message should be destroyed by socket error'); + 'message should be destroyed by socket error' + JSON.stringify(restartedBackend.received)); connection.emitMessage('test', { message: true }); }) @@ -498,10 +529,10 @@ suite('Connection, to backend', function() { .next(function() { assert.equal(backend.received.length, 1, - 'no new message should be sent to the old backend'); + 'no new message should be sent to the old backend' + JSON.stringify(backend.received)); assert.equal(restartedBackend.received.length, 1, - 'message should be sent to the new backend'); + 'message should be sent to the new backend' + JSON.stringify(restartedBackend.received)); done(); }) .error(function(error) { -------------- next part -------------- HTML����������������������������...Download