-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpmn-instances-tests.js
272 lines (230 loc) · 7.58 KB
/
bpmn-instances-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* eslint-env mocha */
import { Bpmn } from 'meteor/cquencial:bpmn-engine'
import { assert } from 'meteor/practicalmeteor:chai'
import { Random } from 'meteor/random'
import { Meteor } from 'meteor/meteor'
const {EventEmitter} = require('events')
const processXml = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<process id="theProcess" isExecutable="true">
<startEvent id="theStart" />
<userTask id="userTask" />
<endEvent id="theEnd" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="userTask" />
<sequenceFlow id="flow2" sourceRef="userTask" targetRef="theEnd" />
</process>
</definitions>`
const processWithUserTask = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<process id="theProcess" isExecutable="true">
<startEvent id="theStart" />
<userTask id="userTask" />
<endEvent id="theEnd" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="userTask" />
<sequenceFlow id="flow2" sourceRef="userTask" targetRef="theEnd" />
</process>
</definitions>`
const Events = {
start: 'start',
enter: 'enter',
end: 'end',
wait: 'wait',
leave: 'leave',
taken: 'taken',
cancel: 'cancel',
error: 'error',
discarded: 'discarded',
}
// used to startup check
let instancesAtStart
let instancesCachedAtStart
Meteor.startup(() => {
console.log('test startzp')
instancesAtStart = Bpmn.instances.collection.find().count()
instancesCachedAtStart = Bpmn.instances.size()
})
describe('bpmn-instances', function () {
const isDefined = function (target, expectedType) {
assert.isDefined(target)
assert.equal(typeof target, expectedType, 'expected ' + expectedType + ' got ' + typeof target)
}
beforeEach(() => {
Bpmn.instances.on()
})
afterEach(() => {
Bpmn.instances.off()
})
describe('Bpmn.instances.has', function () {
it('returns false if instance does not exists', function () {
const instanceId = Random.id()
const expectFalse = Bpmn.instances.has(instanceId)
assert.isFalse(expectFalse)
})
it('returns true if instance exists', function () {
const engine = new Bpmn.Engine({source: processXml})
const insertId = Bpmn.instances.add({instanceId: engine.instanceId, engine})
isDefined(insertId, 'string')
assert.isTrue(Bpmn.instances.has(engine.instanceId))
})
it('throws an error of no instanceId is given', function () {
assert.throws(function () {
Bpmn.instances.has()
})
})
})
describe('Bpmn.instances.add', function () {
it('adds an instance to the cache and collection', function () {
const engine = new Bpmn.Engine({source: processXml})
const {instanceId} = engine
isDefined(instanceId, 'string')
const countBefore = Bpmn.instances.size()
const insertId = Bpmn.instances.add({instanceId, engine})
assert.equal(Bpmn.instances.size(), countBefore + 1)
const instanceDoc = Bpmn.instances.collection.findOne(insertId)
assert.equal(instanceDoc.instanceId, instanceId)
const cachedInstance = Bpmn.instances.get(instanceId)
assert.equal(cachedInstance.instanceId, engine.instanceId)
})
it('adds the engine instance on execute', function (done) {
const engine = new Bpmn.Engine({source: processXml})
engine.execute({}, Meteor.bindEnvironment((err, result) => {
console.log('after execute callback received')
Meteor._sleepForMs(100)
const {instanceId} = engine
assert.isTrue(Bpmn.instances.has(instanceId))
done()
}))
})
it('throws an error if the instance is not a Bpmn.Engine instance', function () {
assert.throws(() => {
Bpmn.instances.add({
instanceId: Random.id(),
})
})
assert.throws(() => {
Bpmn.instances.add({
instanceId: Random.id(),
engine: {},
})
})
assert.throws(() => {
Bpmn.instances.add({
instanceId: Random.id(),
engine: function () {},
})
})
})
it('throws an error if the instanceId is not given', function () {
assert.throws(function () {
Bpmn.instances.add({
engine: new Bpmn.Engine({source: processXml}),
})
})
assert.throws(function () {
Bpmn.instances.add({
instanceId: '',
engine: new Bpmn.Engine({source: processXml}),
})
})
})
})
describe('Bpmn.instances.remove', function () {
it('removes an instance by a given instanceId from cache and collection', function (done) {
const engine = new Bpmn.Engine({source: processXml})
engine.execute({}, Meteor.bindEnvironment(() => {
Meteor._sleepForMs(100)
const {instanceId} = engine
assert.isTrue(Bpmn.instances.has(instanceId))
Bpmn.instances.remove({instanceId})
assert.isFalse(Bpmn.instances.has(instanceId))
done()
}))
})
it('throws an error if no instance id found by instanceId', function () {
assert.throws(function () {
Bpmn.instances.remove({instanceId: Random.id()})
})
assert.throws(function () {
Bpmn.instances.remove({})
})
})
})
describe('Engine.stop', function () {
it('removes an instance on stop', function (done) {
const engine = new Bpmn.Engine({
source: processWithUserTask,
})
const {instanceId} = engine
const listener = new EventEmitter()
listener.on('wait', Meteor.bindEnvironment((element, instance) => {
Meteor._sleepForMs(100)
assert.isTrue(Bpmn.instances.has(instanceId))
engine.stop()
Meteor._sleepForMs(100)
assert.isFalse(Bpmn.instances.has(instanceId))
done()
}))
engine.execute({
listener
})
})
})
describe('Engine.resume', function () {
it('adds instance on resume', function (done) {
const engine = new Bpmn.Engine({
source: processWithUserTask,
hooks: {
'instances': {
onStopAfter () {
assert.isFalse(Bpmn.instances.has(instanceId))
}
}
}
})
const {instanceId} = engine
let state
engine.on('end', () => { // TODO on End hooks
Bpmn.Engine.resume(state, {
instanceId,
hooks: {
'instances': {
onResumeAfter: Meteor.bindEnvironment(function () {
Meteor._sleepForMs(350)
assert.isTrue(Bpmn.instances.has(instanceId))
done()
})
}
}
})
})
const listener = new EventEmitter()
listener.on('wait', Meteor.bindEnvironment((element, instance) => {
state = engine.getState()
Meteor._sleepForMs(100)
assert.isTrue(Bpmn.instances.has(engine.instanceId))
engine.stop()
}))
engine.execute({
listener
})
})
})
describe('Bpmn.instances.collection', function () {
it('is empty after startup', function () {
assert.equal(instancesAtStart, 0)
})
it('has a name', function () {
isDefined(Bpmn.instances.collection.name, 'string')
})
it('has an optional schema definition', function () {
isDefined(Bpmn.instances.collection.schema, 'object')
})
})
describe('Bpmn.instances cache', function () {
it('is empty after startup', function () {
assert.equal(instancesCachedAtStart, 0)
})
})
})