Skip to content

Commit

Permalink
#29 do not double callback on throw
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonk000 committed Aug 27, 2016
1 parent e6e2bba commit 1f65c9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ let Service = function Service (Transports, config) {

if (response && typeof response.then === 'function') {
callbackHandler = null
return response.then(finish).catch(cb)
return response.then(finish, cb)
// if an exception was thrown from finish(), let it bubble up
// as an unhandled rejection, rather than also call cb()
}
}
})
Expand Down
39 changes: 39 additions & 0 deletions test/z-integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

"use strict";

const assert = require('assert');
const s = require('ht-schema');
const bluebird = require('bluebird');

const Service = require('../src/service');
const Client = require('../src/client');
const Transports = require('../src/transports');

describe("Integrations", function () {
it("does not double call when callback throws", function (done) {
var transport = new Transports.Local();
var service = new Service(transport);
var client = new Client({ s: transport });

let countCalled = 0

service.on("hello", null, function () {
return new Promise(function (resolve, reject) {
resolve('hello')
})
})

client.call('s', 'hello', function (err, result) {
countCalled++
throw Error('oops')
})

setTimeout(function () {
switch (countCalled) {
case 0: throw Error('did not get called at all')
case 1: return done()
case 2: throw Error('called multiple times')
}
}, 100)
});
});

0 comments on commit 1f65c9c

Please sign in to comment.