Skip to content

Commit

Permalink
test Async ExpressionHandler #39
Browse files Browse the repository at this point in the history
  • Loading branch information
paed01 committed Feb 21, 2024
1 parent 6ad9bbb commit 1b4147e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
1 change: 0 additions & 1 deletion dist/definition/DefinitionExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/definition/DefinitionExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout
break;
}
case 'process.error': {
// message.ack();
if (inbound && inbound.length) {
const calledFrom = inbound[0];

Expand Down
81 changes: 81 additions & 0 deletions test/feature/issues/issue-39-feature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Definition, SequenceFlow } from '../../../src/index.js';
import testHelpers from '../../helpers/testHelpers.js';

const source = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definitions_1" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<process id="mainProcess" isExecutable="true">
<startEvent id="start" />
<sequenceFlow id="to-decisions" sourceRef="start" targetRef="decisions" />
<exclusiveGateway id="decisions" default="to-defaultend" />
<sequenceFlow id="to-defaultend" sourceRef="decisions" targetRef="defaultend" />
<sequenceFlow id="to-theotherone" sourceRef="decisions" targetRef="theotherone">
<conditionExpression xsi:type="tFormalExpression">\${environment.services.discard()}</conditionExpression>
</sequenceFlow>
<sequenceFlow id="to-takenend" sourceRef="decisions" targetRef="takenend">
<conditionExpression xsi:type="tFormalExpression">\${environment.services.take()}</conditionExpression>
</sequenceFlow>
<endEvent id="defaultend" />
<endEvent id="theotherone" />
<endEvent id="takenend" />
</process>
</definitions>`;

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);
});
}
}
}

0 comments on commit 1b4147e

Please sign in to comment.