YUKI Hiroshi
null+****@clear*****
Wed Jan 23 18:00:08 JST 2013
YUKI Hiroshi 2013-01-23 18:00:08 +0900 (Wed, 23 Jan 2013) New Revision: 5da4d3364de4a5a38270eb41c673f79e432e807f https://github.com/groonga/express-kotoumi/commit/5da4d3364de4a5a38270eb41c673f79e432e807f Log: Add tests for timeout feature of the connection Modified files: test/backend-adaptor.test.js Modified: test/backend-adaptor.test.js (+81 -23) =================================================================== --- test/backend-adaptor.test.js 2013-01-23 17:36:06 +0900 (051347d) +++ test/backend-adaptor.test.js 2013-01-23 18:00:08 +0900 (5a3646a) @@ -1,11 +1,13 @@ var assert = require('chai').assert; var nodemock = require('nodemock'); +var Deferred = require('jsdeferred').Deferred; var utils = require('./test-utils'); var TypeOf = utils.TypeOf; var InstanceOf = utils.InstanceOf; -var Connection = require('../lib/backend-adaptor').Connection; +var backendAdaptor = require('../lib/backend-adaptor'); +var Connection = backendAdaptor.Connection; suite('Connection', function() { var connection; @@ -29,15 +31,34 @@ suite('Connection', function() { receiver = undefined; }); + function createExpectedEnvelope(type, body) { + return { + id: TypeOf('string'), + date: InstanceOf(Date), + replyTo: 'localhost:' + utils.testReceivePort, + statusCode: 200, + type: type, + body: body + }; + } + + function createReplyEnvelopeFor(message, type, body) { + var now = new Date(); + var response = { + id: now.getTime(), + date: now.toISOString(), + inReplyTo: message.id, + statusCode: 200, + type: type, + body: body + }; + return response; + } + test('sending message without response (volatile message)', function() { var message = connection.emitMessage('testRequest', { command: 'foobar' }); assert.envelopeEqual(message, - { id: TypeOf('string'), - date: InstanceOf(Date), - replyTo: 'localhost:' + utils.testReceivePort, - statusCode: 200, - type: 'testRequest', - body: { command: 'foobar' } }); + createExpectedEnvelope('testRequest', { command: 'foobar' })); sender.assertSent('message', message); }); @@ -84,27 +105,14 @@ suite('Connection', function() { callback.assert(); }); - test('sending message with one response', function() { + test('sending message with one response, success', function() { var callback = createMockedMessageCallback(); var message = connection.emitMessage('testRequest', { command: 'foobar' }, callback); assert.envelopeEqual(message, - { id: TypeOf('string'), - date: InstanceOf(Date), - replyTo: 'localhost:' + utils.testReceivePort, - statusCode: 200, - type: 'testRequest', - body: { command: 'foobar' } }); + createExpectedEnvelope('testRequest', { command: 'foobar' })); sender.assertSent('message', message); - var now = new Date(); - var response = { - id: now.getTime(), - date: now.toISOString(), - inReplyTo: message.id, - statusCode: 200, - type: 'testResponse', - body: 'first call' - }; + var response = createReplyEnvelopeFor(message, 'testResponse', 'first call'); callback.takes(null, response); receiver.emitMessage(response); callback.assert(); @@ -114,5 +122,55 @@ suite('Connection', function() { receiver.emitMessage(response); callback.assert(); }); + + test('sending message with one response, with error', function() { + var callback = createMockedMessageCallback(); + var message = connection.emitMessage('testRequest', { command: 'foobar' }, callback); + assert.envelopeEqual(message, + createExpectedEnvelope('testRequest', { command: 'foobar' })); + sender.assertSent('message', message); + + var response = createReplyEnvelopeFor(message, 'testResponse', 'first call'); + response.statusCode = 503; + callback.takes(503, response); + receiver.emitMessage(response); + callback.assert(); + }); + + test('sending message with one response, timeout (not timed out)', function() { + var callback = createMockedMessageCallback(); + var message = connection.emitMessage('testRequest', { command: 'foobar' }, callback, 1000); + assert.envelopeEqual(message, + createExpectedEnvelope('testRequest', { command: 'foobar' })); + sender.assertSent('message', message); + assert.equal(connection.listeners('inReplyTo:' + message.id).length, 1); + + var response = createReplyEnvelopeFor(message, 'testResponse', 'first call'); + callback.takes(null, response); + receiver.emitMessage(response); + callback.assert(); + assert.equal(connection.listeners('inReplyTo:' + message.id).length, 0); + }); + + test('sending message with one response, timeout (timed out)', function(done) { + var callback = createMockedMessageCallback(); + var message = connection.emitMessage('testRequest', { command: 'foobar' }, callback, 1); + assert.envelopeEqual(message, + createExpectedEnvelope('testRequest', { command: 'foobar' })); + sender.assertSent('message', message); + assert.equal(connection.listeners('inReplyTo:' + message.id).length, 1); + + callback.takes(backendAdaptor.ERROR_GATEWAY_TIMEOUT, null); + Deferred + .wait(0.01) + .next(function() { + assert.equal(connection.listeners('inReplyTo:' + message.id).length, 0); + callback.assert(); + done(); + }) + .error(function(error) { + done(error); + }); + }); }); -------------- next part -------------- HTML����������������������������...Download