Skip to content

Commit 46e8eba

Browse files
committed
fix (invoke-handler) coderunner doesn't wait for async handlers completion in CLOUD mode
1 parent dfe19a1 commit 46e8eba

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

lib/server-code/runners/tasks/invoke-handler.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = function(task, runnerOpts, model) {
6464
}
6565
});
6666

67-
function sendResult(result) {
67+
function buildResponse(result) {
6868
if (result !== undefined) {
6969
req.context.prematureResult = res.result = result;
7070
}
@@ -75,7 +75,7 @@ module.exports = function(task, runnerOpts, model) {
7575
: req[name];
7676
});
7777

78-
resolve(encodeTaskArguments(taskArgs));
78+
return encodeTaskArguments(taskArgs);
7979
}
8080

8181
function invoke() {
@@ -85,14 +85,10 @@ module.exports = function(task, runnerOpts, model) {
8585
throw new Error(`${event.name}(${task.target}) event handler does not exist`);
8686
}
8787

88-
const result = handler.invoke.apply(null, handlerArgs);
89-
90-
if (task.async) {
91-
resolve(); //empty invocation result for async events
92-
} else {
93-
promise.timeout(task.timeout, Promise.resolve(result), TIMED_OUT_ERR)
94-
.then(sendResult, reject);
95-
}
88+
const invocationPromise = Promise.resolve(handler.invoke.apply(null, handlerArgs));
89+
promise.timeout(task.timeout, invocationPromise, TIMED_OUT_ERR).then(result => {
90+
resolve(task.async ? undefined : buildResponse(result));
91+
}, reject);
9692
}
9793

9894
const d = domain.create();

test/invoke-handler-task.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('[invoke-handler] task executor', function() {
6262
req.item.should.be.eql({ name: 'John' });
6363
}
6464

65-
return invokeAndParse(task, modelStub(handler)).should.be.fulfilled();
65+
return invokeAndParse(task, modelStub(handler));
6666
});
6767

6868
describe('should perform class mapping', function() {
@@ -310,18 +310,29 @@ describe('[invoke-handler] task executor', function() {
310310

311311
describe('for async events', function() {
312312
const task = createTask(AFTER_CREATE, [], true);
313+
task.timeout = 3;
314+
315+
it('should wait for handler`s promise', function() {
316+
let handlerFinished = false;
313317

314-
it('should not wait for handler`s promise', function() {
315318
function handler() {
316-
return new Promise(() => {
319+
return new Promise(resolve => {
320+
setTimeout(() => {
321+
handlerFinished = true;
322+
resolve();
323+
}, 2);
317324
});
318325
}
319326

320-
return invokeAndParse(task, modelStub(handler)).should.be.fulfilled();
327+
return invokeAndParse(task, modelStub(handler))
328+
.then(() => {
329+
handlerFinished.should.be.true();
330+
});
321331
});
322332

323333
it('should not return any result', function() {
324334
function handler() {
335+
return {};
325336
}
326337

327338
return invokeAndParse(task, modelStub(handler)).should.be.fulfilledWith(undefined);

0 commit comments

Comments
 (0)