From 1b4147ea863cd48cfb585299f9fd607c8d727c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Edman?= Date: Wed, 21 Feb 2024 06:14:45 +0100 Subject: [PATCH] test Async ExpressionHandler #39 --- dist/definition/DefinitionExecution.js | 1 - package.json | 6 +- src/definition/DefinitionExecution.js | 1 - test/feature/issues/issue-39-feature.js | 81 +++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 test/feature/issues/issue-39-feature.js diff --git a/dist/definition/DefinitionExecution.js b/dist/definition/DefinitionExecution.js index 462906f..7532d13 100644 --- a/dist/definition/DefinitionExecution.js +++ b/dist/definition/DefinitionExecution.js @@ -412,7 +412,6 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout } case 'process.error': { - // message.ack(); if (inbound && inbound.length) { const calledFrom = inbound[0]; this._getProcessApi({ diff --git a/package.json b/package.json index 0e6b71d..dd88d05 100644 --- a/package.json +++ b/package.json @@ -69,11 +69,11 @@ "@babel/preset-env": "^7.23.9", "@babel/register": "^7.23.7", "@bonniernews/hot-bev": "^0.4.0", - "@types/node": "^16.18.80", + "@types/node": "^16.18.82", "bpmn-moddle": "^8.1.0", "c8": "^9.1.0", "camunda-bpmn-moddle": "^7.0.1", - "chai": "^5.0.3", + "chai": "^5.1.0", "chronokinesis": "^6.0.0", "debug": "^4.3.4", "eslint": "^8.56.0", @@ -82,7 +82,7 @@ "mocha": "^10.3.0", "mocha-cakes-2": "^3.3.0", "moddle-context-serializer": "^4.1.2", - "nock": "^13.5.1" + "nock": "^13.5.3" }, "dependencies": { "smqp": "^8.2.2" diff --git a/src/definition/DefinitionExecution.js b/src/definition/DefinitionExecution.js index fcd87cb..93708a3 100644 --- a/src/definition/DefinitionExecution.js +++ b/src/definition/DefinitionExecution.js @@ -422,7 +422,6 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout break; } case 'process.error': { - // message.ack(); if (inbound && inbound.length) { const calledFrom = inbound[0]; diff --git a/test/feature/issues/issue-39-feature.js b/test/feature/issues/issue-39-feature.js new file mode 100644 index 0000000..c9d4fd2 --- /dev/null +++ b/test/feature/issues/issue-39-feature.js @@ -0,0 +1,81 @@ +import { Definition, SequenceFlow } from '../../../src/index.js'; +import testHelpers from '../../helpers/testHelpers.js'; + +const source = ` + + + + + + + + + \${environment.services.discard()} + + + \${environment.services.take()} + + + + + +`; + +Feature('Issue 39 - resolve SequenceFlow expression promise', () => { + Scenario('expression function returns promise', () => { + let context, definition, end; + When('definition is ran with exclusive gataway with three flows and expression services that returns promise', async () => { + context = await testHelpers.context(source, { + types: { + SequenceFlow: FlorianSequenceFlow, + }, + }); + definition = new Definition(context, { + services: { + take() { + return Promise.resolve(true); + }, + discard() { + return Promise.resolve(false); + }, + }, + }); + end = definition.waitFor('end'); + definition.run(); + }); + + Then('run completes', () => { + return end; + }); + + And('promise resolved and took correct end event', () => { + expect(definition.getActivityById('takenend').counters).to.deep.equal({ taken: 1, discarded: 0 }); + }); + + And('discarded default and the other one', () => { + expect(definition.getActivityById('defaultend').counters, 'default').to.deep.equal({ taken: 0, discarded: 1 }); + expect(definition.getActivityById('theotherone').counters, 'the other one').to.deep.equal({ taken: 0, discarded: 1 }); + }); + }); +}); + +class FlorianSequenceFlow extends SequenceFlow { + getCondition() { + const condition = super.getCondition(); + if (condition?.type !== 'expression') return condition; + + const execute = condition.execute; + condition.execute = asyncExecute.bind(condition); + + return condition; + + function asyncExecute(message, callback) { + execute.call(condition, message, (executeErr, result) => { + if (executeErr) return callback(executeErr); + return Promise.resolve(result).then(r => + callback ? callback(null, r) : result).catch(err => + callback ? callback(err) : err); + }); + } + } +}