@mavenmachines/event-handler-lib 1.5.26-rc.3 → 1.5.26-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/README.md +103 -0
  2. package/dist/src/handlers/baseHandler.d.ts +19 -1
  3. package/dist/src/handlers/baseHandler.js +145 -23
  4. package/dist/src/handlers/baseHandler.js.map +1 -1
  5. package/dist/src/handlers/commandHandlers/baseCommandHandler.d.ts +4 -1
  6. package/dist/src/handlers/commandHandlers/baseCommandHandler.js +5 -3
  7. package/dist/src/handlers/commandHandlers/baseCommandHandler.js.map +1 -1
  8. package/dist/src/handlers/eventHandlers/baseEventHandler.d.ts +0 -1
  9. package/dist/src/handlers/eventHandlers/baseEventHandler.js +6 -3
  10. package/dist/src/handlers/eventHandlers/baseEventHandler.js.map +1 -1
  11. package/dist/src/helpers/projectionModifier.js +1 -1
  12. package/dist/src/helpers/projectionModifier.js.map +1 -1
  13. package/dist/src/repositories/baseDynamoRepository.js +1 -0
  14. package/dist/src/repositories/baseDynamoRepository.js.map +1 -1
  15. package/dist/src/testUtils/mocks/kafkajs.mock.js +11 -0
  16. package/dist/src/testUtils/mocks/kafkajs.mock.js.map +1 -1
  17. package/dist/src/testUtils/testDependencies.js +14 -6
  18. package/dist/src/testUtils/testDependencies.js.map +1 -1
  19. package/dist/src/types/enums/events.d.ts +2 -1
  20. package/dist/src/types/enums/events.js +1 -0
  21. package/dist/src/types/enums/events.js.map +1 -1
  22. package/dist/test/testImplementations/testEventHandler.js +6 -1
  23. package/dist/test/testImplementations/testEventHandler.js.map +1 -1
  24. package/dist/test/tests/eventHandlers/baseEventHandler.spec.js +387 -50
  25. package/dist/test/tests/eventHandlers/baseEventHandler.spec.js.map +1 -1
  26. package/package.json +1 -1
@@ -8,22 +8,23 @@ const assert_1 = require("assert");
8
8
  const firestore_1 = require("@google-cloud/firestore");
9
9
  const src_1 = require("../../../src");
10
10
  const testEventHandler_1 = require("../../testImplementations/testEventHandler");
11
+ const baseHandler_1 = require("../../../src/handlers/baseHandler");
11
12
  let appContainer;
12
13
  describe("BaseEventHandler", () => {
13
- let testDependencies;
14
+ let deps;
14
15
  let eventHandler;
15
16
  before(async () => {
16
17
  appContainer = await (0, common_1.getTestAppContainer)();
17
- testDependencies = await (0, src_1.getTestDependencies)(appContainer);
18
+ deps = await (0, src_1.getTestDependencies)(appContainer);
18
19
  eventHandler = appContainer.get(testEventHandler_1.TestEventHandler);
19
20
  });
20
- (0, src_1.registerTestHooks)(() => testDependencies);
21
+ (0, src_1.registerTestHooks)(() => deps);
21
22
  it("should handle an event", async () => {
22
- const body = await testDependencies.runEvent(eventHandler, {});
23
+ const body = await deps.runEvent(eventHandler, {});
23
24
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
24
25
  });
25
26
  it("should fail on class validation error", async () => {
26
- const body = await testDependencies.runEvent(eventHandler, {
27
+ const body = await deps.runEvent(eventHandler, {
27
28
  numberField: "Not A Number",
28
29
  });
29
30
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Failed);
@@ -31,7 +32,7 @@ describe("BaseEventHandler", () => {
31
32
  (0, chai_1.expect)(body.reason).to.contain('numberField ("Not A Number"): numberField must be an integer number');
32
33
  });
33
34
  it("should return failed status on failure", async () => {
34
- const body = await testDependencies.runEvent(eventHandler, {
35
+ const body = await deps.runEvent(eventHandler, {
35
36
  fail: true,
36
37
  errorMessage: "Expected failure",
37
38
  });
@@ -53,19 +54,19 @@ describe("BaseEventHandler", () => {
53
54
  };
54
55
  const bootstrapProjection = async (payload) => {
55
56
  const projection = getProjection(payload);
56
- const inserted = await testDependencies.projectionRepository.putItem(projection);
57
- await testDependencies.openSearch.indexProjection(inserted);
57
+ const inserted = await deps.projectionRepository.putItem(projection);
58
+ await deps.openSearch.indexProjection(inserted);
58
59
  return inserted;
59
60
  };
60
61
  it("should publish updates on success", async () => {
61
62
  const ts = firestore_1.Timestamp.now();
62
63
  const projection = await bootstrapProjection({ key: "value", ts });
63
- const body = await testDependencies.runEvent(eventHandler, {
64
+ const body = await deps.runEvent(eventHandler, {
64
65
  projection,
65
66
  modifications: { newKey: "newValue" },
66
67
  });
67
68
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
68
- const storedProjection = await testDependencies.projectionRepository.loadItem({
69
+ const storedProjection = await deps.projectionRepository.loadItem({
69
70
  companyId: 1,
70
71
  projectionType: "testProjection",
71
72
  entityId: projection.entityId,
@@ -80,8 +81,8 @@ describe("BaseEventHandler", () => {
80
81
  newKey: "newValue",
81
82
  ts,
82
83
  });
83
- (0, chai_1.expect)(testDependencies.eventLogService.addNewEvent.stub.args.length).to.equal(1);
84
- const callArgs = testDependencies.eventLogService.addNewEvent.stub.args[0][0];
84
+ (0, chai_1.expect)(deps.eventLogService.addNewEvent.stub.args.length).to.equal(1);
85
+ const callArgs = deps.eventLogService.addNewEvent.stub.args[0][0];
85
86
  (0, chai_1.expect)(callArgs.companyId).to.equal(1);
86
87
  (0, chai_1.expect)(callArgs.eventType).to.equal("entity.testProjection.updated");
87
88
  (0, chai_1.expect)(callArgs.payload.referenceIds).to.deep.equal(projection.referenceIds);
@@ -89,18 +90,18 @@ describe("BaseEventHandler", () => {
89
90
  newKey: { previous: null, updated: "newValue" },
90
91
  });
91
92
  (0, chai_1.expect)(callArgs.payload.projection.payload.newKey).to.equal("newValue");
92
- const indexedProjection = await testDependencies.openSearch.getProjectionIndex(projection);
93
+ const indexedProjection = await deps.openSearch.getProjectionIndex(projection);
93
94
  (0, common_1.expectValidProjectionIndex)(indexedProjection, storedProjection);
94
95
  });
95
96
  it("should not publish updates on failure", async () => {
96
97
  const projection = await bootstrapProjection({ key: "value" });
97
- const body = await testDependencies.runEvent(eventHandler, {
98
+ const body = await deps.runEvent(eventHandler, {
98
99
  projection,
99
100
  fail: true,
100
101
  });
101
102
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Failed);
102
- (0, chai_1.expect)(testDependencies.eventLogService.addNewEvent.stub.args.length).to.equal(0);
103
- return testDependencies.projectionRepository
103
+ (0, chai_1.expect)(deps.eventLogService.addNewEvent.stub.args.length).to.equal(0);
104
+ return deps.projectionRepository
104
105
  .loadItem({
105
106
  companyId: 1,
106
107
  projectionType: "testProjection",
@@ -116,48 +117,52 @@ describe("BaseEventHandler", () => {
116
117
  timestamp: luxon_1.DateTime.now().toISO(),
117
118
  };
118
119
  it("should generate return events on success", async () => {
119
- const body = await testDependencies.runEvent(eventHandler, { returnEvent });
120
+ const body = await deps.runEvent(eventHandler, { returnEvent });
120
121
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
121
- (0, chai_1.expect)(testDependencies.returnEventService.addReturnEvent.stub.args).to.deep.equal([[returnEvent]]);
122
+ (0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.deep.equal([
123
+ [returnEvent],
124
+ ]);
122
125
  });
123
126
  it("should not send configured excluded return events", async () => {
124
- testDependencies.directoryServicesClientMock.setConfigurationObject(testDependencies.companyId, "eventSourcing", "excludedReturnEventTypes", [returnEvent.eventType]);
125
- const body = await testDependencies.runEvent(eventHandler, { returnEvent });
127
+ deps.directoryServicesClientMock.setConfigurationObject(deps.companyId, "eventSourcing", "excludedReturnEventTypes", [returnEvent.eventType]);
128
+ const body = await deps.runEvent(eventHandler, { returnEvent });
126
129
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
127
- (0, chai_1.expect)(testDependencies.returnEventService.addReturnEvent.stub.called).to
128
- .equal(false);
130
+ (0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.called).to.equal(false);
129
131
  });
130
132
  it("should send only return events not configured for exclusion", async () => {
131
133
  const excludedReturnEvent = {
132
134
  ...returnEvent,
133
135
  eventType: "excluded_event",
134
136
  };
135
- testDependencies.directoryServicesClientMock.setConfigurationObject(testDependencies.companyId, "eventSourcing", "excludedReturnEventTypes", [excludedReturnEvent.eventType]);
136
- const body = await testDependencies.runEvent(eventHandler, {
137
+ deps.directoryServicesClientMock.setConfigurationObject(deps.companyId, "eventSourcing", "excludedReturnEventTypes", [excludedReturnEvent.eventType]);
138
+ const body = await deps.runEvent(eventHandler, {
137
139
  returnEvents: [excludedReturnEvent, returnEvent],
138
140
  });
139
141
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
140
- (0, chai_1.expect)(testDependencies.returnEventService.addReturnEvent.stub.args).to.deep.equal([[returnEvent]]);
142
+ (0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.deep.equal([
143
+ [returnEvent],
144
+ ]);
141
145
  });
142
146
  it("should send return events when exclusion configuration is malformed", async () => {
143
- testDependencies.directoryServicesClientMock.setConfigurationObject(testDependencies.companyId, "eventSourcing", "excludedReturnEventTypes", { eventTypes: [returnEvent.eventType] });
144
- const body = await testDependencies.runEvent(eventHandler, { returnEvent });
147
+ deps.directoryServicesClientMock.setConfigurationObject(deps.companyId, "eventSourcing", "excludedReturnEventTypes", { eventTypes: [returnEvent.eventType] });
148
+ const body = await deps.runEvent(eventHandler, { returnEvent });
145
149
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
146
- (0, chai_1.expect)(testDependencies.returnEventService.addReturnEvent.stub.args).to.deep.equal([[returnEvent]]);
150
+ (0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.deep.equal([
151
+ [returnEvent],
152
+ ]);
147
153
  });
148
154
  it("should not generate return events on failure", async () => {
149
- const body = await testDependencies.runEvent(eventHandler, {
155
+ const body = await deps.runEvent(eventHandler, {
150
156
  returnEvent,
151
157
  fail: true,
152
158
  });
153
159
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Failed);
154
- (0, chai_1.expect)(testDependencies.returnEventService.addReturnEvent.stub.args).to.be
155
- .empty;
160
+ (0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.be.empty;
156
161
  });
157
162
  it("should successfully delete a projection declaration", async () => {
158
163
  const projection = await bootstrapProjection({ key: "value" });
159
- await testDependencies.runEvent(eventHandler, { projection });
160
- const body = await testDependencies.runEvent(eventHandler, {
164
+ await deps.runEvent(eventHandler, { projection });
165
+ const body = await deps.runEvent(eventHandler, {
161
166
  projection: {
162
167
  companyId: projection.companyId,
163
168
  projectionType: projection.projectionType,
@@ -167,7 +172,7 @@ describe("BaseEventHandler", () => {
167
172
  });
168
173
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
169
174
  try {
170
- await testDependencies.projectionRepository.loadItem({
175
+ await deps.projectionRepository.loadItem({
171
176
  companyId: projection.companyId,
172
177
  projectionType: projection.projectionType,
173
178
  entityId: projection.entityId,
@@ -178,9 +183,9 @@ describe("BaseEventHandler", () => {
178
183
  (0, chai_1.expect)(err).to.be.instanceOf(src_1.ItemNotFound);
179
184
  (0, chai_1.expect)(err.message).to.equal(`Projection ${projection.companyId_projectionType_entityId} is deleted`);
180
185
  }
181
- (0, chai_1.expect)(testDependencies.eventLogService.addNewEvent.stub.args.length).to.equal(1);
182
- console.log(testDependencies.eventLogService.addNewEvent.stub.args);
183
- const updatedEvent = testDependencies.eventLogService.addNewEvent.stub.args[0][0];
186
+ (0, chai_1.expect)(deps.eventLogService.addNewEvent.stub.args.length).to.equal(1);
187
+ console.log(deps.eventLogService.addNewEvent.stub.args);
188
+ const updatedEvent = deps.eventLogService.addNewEvent.stub.args[0][0];
184
189
  (0, chai_1.expect)(updatedEvent.eventType).to.equal((0, src_1.projectionDeletedEventName)(projection.projectionType));
185
190
  (0, chai_1.expect)(updatedEvent.payload.projection.payload).to.deep.equal({
186
191
  isDeletion: true,
@@ -194,15 +199,15 @@ describe("BaseEventHandler", () => {
194
199
  });
195
200
  it("should successfully delete a loaded projection", async () => {
196
201
  const projection = await bootstrapProjection({ key: "value" });
197
- await testDependencies.runEvent(eventHandler, { projection });
202
+ await deps.runEvent(eventHandler, { projection });
198
203
  projection.version += 1;
199
- const body = await testDependencies.runEvent(eventHandler, {
204
+ const body = await deps.runEvent(eventHandler, {
200
205
  projection,
201
206
  delete: true,
202
207
  });
203
208
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
204
209
  try {
205
- await testDependencies.projectionRepository.loadItem({
210
+ await deps.projectionRepository.loadItem({
206
211
  companyId: projection.companyId,
207
212
  projectionType: projection.projectionType,
208
213
  entityId: projection.entityId,
@@ -213,10 +218,10 @@ describe("BaseEventHandler", () => {
213
218
  (0, chai_1.expect)(err).to.be.instanceOf(src_1.ItemNotFound);
214
219
  (0, chai_1.expect)(err.message).to.equal(`Projection ${projection.companyId_projectionType_entityId} is deleted`);
215
220
  }
216
- const indexedProjection = await testDependencies.openSearch.getProjectionIndex(projection);
221
+ const indexedProjection = await deps.openSearch.getProjectionIndex(projection);
217
222
  (0, common_1.expectDeletedProjectionIndex)(indexedProjection);
218
- (0, chai_1.expect)(testDependencies.eventLogService.addNewEvent.stub.args.length).to.equal(1);
219
- const updatedEvent = testDependencies.eventLogService.addNewEvent.stub.args[0][0];
223
+ (0, chai_1.expect)(deps.eventLogService.addNewEvent.stub.args.length).to.equal(1);
224
+ const updatedEvent = deps.eventLogService.addNewEvent.stub.args[0][0];
220
225
  (0, chai_1.expect)(updatedEvent.eventType).to.equal((0, src_1.projectionDeletedEventName)(projection.projectionType));
221
226
  (0, chai_1.expect)(updatedEvent.payload.projection.payload).to.deep.equal({
222
227
  isDeletion: true,
@@ -232,11 +237,11 @@ describe("BaseEventHandler", () => {
232
237
  const projection = getProjection({ key: "value" });
233
238
  projection.projectionType = "noIndex";
234
239
  projection.companyId_projectionType_entityId = `${projection.companyId}_${projection.projectionType}_${projection.entityId}`;
235
- const body = await testDependencies.runEvent(eventHandler, {
240
+ const body = await deps.runEvent(eventHandler, {
236
241
  projection,
237
242
  });
238
243
  (0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
239
- await testDependencies.projectionRepository.loadItem(projection);
244
+ await deps.projectionRepository.loadItem(projection);
240
245
  });
241
246
  it("should roll back all projections on a one projection's failure", async () => {
242
247
  const projection = await bootstrapProjection({
@@ -247,11 +252,12 @@ describe("BaseEventHandler", () => {
247
252
  key2: "value2",
248
253
  description: "This should fail with a key conflict",
249
254
  });
250
- const result = await testDependencies.runEvent(eventHandler, {
255
+ const result = await deps.runEvent(eventHandler, {
251
256
  projection,
252
257
  modifications: { newKey: "newValue" },
253
258
  projection2: {
254
259
  ...projection2,
260
+ version: 0,
255
261
  payload: {
256
262
  ...projection2.payload,
257
263
  newKey: "value",
@@ -260,14 +266,345 @@ describe("BaseEventHandler", () => {
260
266
  });
261
267
  (0, chai_1.expect)(result.status).to.equal(src_1.EventStatusEnum.Failed);
262
268
  (0, chai_1.expect)(result.reason).to.include("ConditionalCheckFailedException");
263
- const loadedProjection1 = await testDependencies.projectionRepository.loadItem(projection);
269
+ const loadedProjection1 = await deps.projectionRepository.loadItem(projection);
264
270
  (0, chai_1.expect)(loadedProjection1).to.deep.equal(projection);
265
- const loadedProjection2 = await testDependencies.projectionRepository.maybeLoadItem(projection2);
271
+ const loadedProjection2 = await deps.projectionRepository.maybeLoadItem(projection2);
266
272
  (0, chai_1.expect)(loadedProjection2).to.deep.equal(projection2);
267
- const indexedProjection1 = await testDependencies.openSearch.getProjectionIndex(projection);
273
+ const indexedProjection1 = await deps.openSearch.getProjectionIndex(projection);
268
274
  (0, common_1.expectValidProjectionIndex)(indexedProjection1, projection);
269
- const indexedProjection2 = await testDependencies.openSearch.getProjectionIndex(projection2);
275
+ const indexedProjection2 = await deps.openSearch.getProjectionIndex(projection2);
270
276
  (0, common_1.expectValidProjectionIndex)(indexedProjection2, projection2);
271
277
  });
278
+ describe("Timeouts", async () => {
279
+ let revertTimeout;
280
+ beforeEach(async () => {
281
+ revertTimeout = (0, baseHandler_1.TEST_ONLY_setEventHandlerTimeout)(100);
282
+ });
283
+ afterEach(async () => {
284
+ revertTimeout();
285
+ });
286
+ async function doTest(handleEvent) {
287
+ class TimeoutEventHandler extends src_1.BaseEventHandler {
288
+ constructor() {
289
+ super(...arguments);
290
+ this.eventType = "timeout";
291
+ }
292
+ async handleEvent(context) {
293
+ await handleEvent(context);
294
+ }
295
+ }
296
+ const handler = await appContainer.getAsync(TimeoutEventHandler);
297
+ return deps.runEvent(handler, {}, src_1.EventStatusEnum.Timeout);
298
+ }
299
+ it("Should time out to prevent infinitely long event handlers", async () => {
300
+ await doTest(async () => {
301
+ await new Promise(() => { });
302
+ });
303
+ });
304
+ it("Should not await stashed promises after timeout", async () => {
305
+ let value = 100;
306
+ await doTest(async (context) => {
307
+ context.stashPromise(new Promise(async (resolve) => {
308
+ setTimeout(() => {
309
+ value = 200;
310
+ resolve(undefined);
311
+ }, 200);
312
+ }));
313
+ await new Promise((resolve) => { });
314
+ });
315
+ (0, chai_1.expect)(value).to.equal(100);
316
+ });
317
+ it("Should perform rollbacks after timeout", async () => {
318
+ let value = 100;
319
+ await doTest(async (context) => {
320
+ context.stashPromise(new Promise(async () => { }), async () => {
321
+ value = 200;
322
+ });
323
+ context.stashPromise(new Promise(async (resolve) => resolve(undefined)), async () => (value = 300));
324
+ });
325
+ (0, chai_1.expect)(value).to.equal(300);
326
+ });
327
+ it("Should timeout waiting for stashed promises", async () => {
328
+ let value = 100;
329
+ await doTest(async (context) => {
330
+ context.stashPromise(new Promise(async () => {
331
+ await new Promise(async () => { });
332
+ value = 200;
333
+ }), async () => {
334
+ value = 300;
335
+ });
336
+ });
337
+ (0, chai_1.expect)(value).to.equal(100);
338
+ });
339
+ it("Should timeout during rollbacks", async () => {
340
+ let value = 100;
341
+ await doTest(async (context) => {
342
+ context.stashPromise(new Promise(() => {
343
+ value = 200;
344
+ }), async () => {
345
+ await new Promise(() => { });
346
+ value = 300;
347
+ });
348
+ });
349
+ (0, chai_1.expect)(value).to.equal(200);
350
+ });
351
+ it("Should include multiple stages as cumulative timeouts", async () => {
352
+ await doTest(async (context) => {
353
+ await new Promise((resolve) => setTimeout(resolve, 50));
354
+ context.stashPromise(new Promise((resolve) => setTimeout(resolve, 60)));
355
+ });
356
+ });
357
+ });
358
+ describe("Stashed Promises", () => {
359
+ const ORIGINALS = { value1: 1, value2: 2, value3: 3 };
360
+ const SET_IN_STASHED = { value1: 10, value2: 20, value3: 30 };
361
+ const SET_IN_STASHED_ROLLBACK = { value1: 100, value2: 200, value3: 300 };
362
+ const SET_IN_ROLLBACK = { value1: 1000, value2: 2000, value3: 3000 };
363
+ const SET_IN_HANDLER = { value1: 10000, value2: 20000, value3: 30000 };
364
+ let value1;
365
+ let value2;
366
+ let value3;
367
+ beforeEach(async () => {
368
+ value1 = ORIGINALS.value1;
369
+ value2 = ORIGINALS.value2;
370
+ value3 = ORIGINALS.value3;
371
+ });
372
+ async function doTest(handleEvent, expected = src_1.EventStatusEnum.Succeeded) {
373
+ class StashedEventhandler extends src_1.BaseEventHandler {
374
+ constructor() {
375
+ super(...arguments);
376
+ this.eventType = "stash-event";
377
+ }
378
+ handleEvent(context) {
379
+ handleEvent(context);
380
+ return Promise.resolve();
381
+ }
382
+ }
383
+ const handler = await appContainer.getAsync(StashedEventhandler);
384
+ return deps.runEvent(handler, {}, expected);
385
+ }
386
+ it("Should allow a stashed promise to be awaited during post-handle activities", async () => {
387
+ await doTest((context) => {
388
+ context.stashPromise(new Promise((resolve) => setTimeout(() => {
389
+ value1 = SET_IN_STASHED.value1;
390
+ resolve(undefined);
391
+ }, 50)));
392
+ return Promise.resolve();
393
+ });
394
+ (0, chai_1.expect)(value1).to.equal(SET_IN_STASHED.value1);
395
+ });
396
+ it("Should not roll back a stashed promise if it fails", async () => {
397
+ await doTest((context) => {
398
+ context.stashPromise(new Promise(async (resolve, reject) => {
399
+ setTimeout(() => {
400
+ value1 = SET_IN_STASHED.value1;
401
+ reject(new Error("Test"));
402
+ }, 50);
403
+ }), async () => {
404
+ value1 = SET_IN_STASHED_ROLLBACK.value1;
405
+ });
406
+ return Promise.resolve();
407
+ }, src_1.EventStatusEnum.Failed);
408
+ (0, chai_1.expect)(value1).to.equal(SET_IN_STASHED.value1);
409
+ });
410
+ it("Should roll back successful promises if one fails", async () => {
411
+ await doTest((context) => {
412
+ context.stashPromise("Named promise with rollback", new Promise((resolve) => {
413
+ value1 = SET_IN_STASHED.value1;
414
+ resolve(undefined);
415
+ }), async () => {
416
+ value1 = SET_IN_STASHED_ROLLBACK.value1;
417
+ });
418
+ context.stashPromise(new Promise(async (resolve, reject) => {
419
+ value2 = SET_IN_STASHED.value2;
420
+ setTimeout(async () => {
421
+ reject(new Error("Test"));
422
+ }, 50);
423
+ }));
424
+ }, src_1.EventStatusEnum.Failed);
425
+ (0, chai_1.expect)(value1).to.equal(SET_IN_STASHED_ROLLBACK.value1);
426
+ (0, chai_1.expect)(value2).to.equal(SET_IN_STASHED.value2);
427
+ });
428
+ it("Should wait for all stashed promises, despite duration", async () => {
429
+ await doTest((context) => {
430
+ context.stashPromise(new Promise((resolve) => {
431
+ setTimeout(() => {
432
+ value1 = SET_IN_STASHED.value1;
433
+ resolve(undefined);
434
+ }, 10);
435
+ }));
436
+ context.stashPromise(new Promise((resolve) => {
437
+ setTimeout(() => {
438
+ value2 = SET_IN_STASHED.value2;
439
+ resolve(undefined);
440
+ }, 100);
441
+ }));
442
+ context.stashPromise(new Promise((resolve) => {
443
+ setTimeout(() => {
444
+ value3 = SET_IN_STASHED.value3;
445
+ resolve(undefined);
446
+ }, 200);
447
+ }));
448
+ });
449
+ (0, chai_1.expect)(value1).to.equal(SET_IN_STASHED.value1);
450
+ (0, chai_1.expect)(value2).to.equal(SET_IN_STASHED.value2);
451
+ (0, chai_1.expect)(value3).to.equal(SET_IN_STASHED.value3);
452
+ });
453
+ it("Should wait for all stashed promises, even if one fails immediately", async () => {
454
+ await doTest((context) => {
455
+ context.stashPromise(new Promise(async (resolve, reject) => {
456
+ setTimeout(() => reject(new Error("error")), 5);
457
+ }));
458
+ context.stashPromise(new Promise((resolve) => {
459
+ setTimeout(() => {
460
+ value2 = SET_IN_STASHED.value2;
461
+ resolve(undefined);
462
+ }, 100);
463
+ }));
464
+ context.stashPromise(new Promise((resolve) => {
465
+ setTimeout(() => {
466
+ value3 = SET_IN_STASHED.value3;
467
+ resolve(undefined);
468
+ }, 200);
469
+ }));
470
+ }, src_1.EventStatusEnum.Failed);
471
+ (0, chai_1.expect)(value1).to.equal(ORIGINALS.value1);
472
+ (0, chai_1.expect)(value2).to.equal(SET_IN_STASHED.value2);
473
+ (0, chai_1.expect)(value3).to.equal(SET_IN_STASHED.value3);
474
+ });
475
+ it("Should roll back if handler fails", async () => {
476
+ await doTest((context) => {
477
+ context.stashPromise("Named promise", new Promise((resolve) => {
478
+ value1 = SET_IN_STASHED.value1;
479
+ resolve(undefined);
480
+ }), async () => {
481
+ value1 = SET_IN_ROLLBACK.value1;
482
+ });
483
+ throw new Error("fail");
484
+ }, src_1.EventStatusEnum.Failed);
485
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
486
+ });
487
+ it("Should not roll back if everything succeeds", async () => {
488
+ await doTest((context) => {
489
+ context.stashPromise(new Promise((resolve) => {
490
+ value1 = SET_IN_STASHED.value1;
491
+ resolve(undefined);
492
+ }), async () => {
493
+ value1 = SET_IN_ROLLBACK.value1;
494
+ });
495
+ context.stashPromise(new Promise((resolve) => {
496
+ value2 = SET_IN_STASHED.value2;
497
+ resolve(undefined);
498
+ }), async () => {
499
+ value2 = SET_IN_ROLLBACK.value2;
500
+ });
501
+ });
502
+ (0, chai_1.expect)(value1).to.equal(SET_IN_STASHED.value1);
503
+ (0, chai_1.expect)(value2).to.equal(SET_IN_STASHED.value2);
504
+ });
505
+ it("Should roll back if modifications fail", async () => {
506
+ const projectionId = {
507
+ companyId: deps.companyId,
508
+ projectionType: "testProjection",
509
+ entityId: (0, crypto_1.randomUUID)(),
510
+ };
511
+ await deps.projectionRepository.putItem({
512
+ ...projectionId,
513
+ payload: { value: 1 },
514
+ });
515
+ await doTest((context) => {
516
+ context.stashPromise(new Promise((resolve) => {
517
+ value1 = SET_IN_STASHED.value1;
518
+ resolve(undefined);
519
+ }), async () => {
520
+ value1 = SET_IN_ROLLBACK.value1;
521
+ });
522
+ context.projectionModifier.createProjection({
523
+ ...projectionId,
524
+ payload: { value: 9 },
525
+ });
526
+ }, src_1.EventStatusEnum.Failed);
527
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
528
+ const loaded = await deps.projectionRepository.loadItem(projectionId);
529
+ (0, chai_1.expect)(loaded.payload.value).to.equal(1);
530
+ });
531
+ it("Should not fail to roll back if one rollback fails", async () => {
532
+ await doTest((context) => {
533
+ context.addRollback(async () => {
534
+ throw new Error("rollback failure ");
535
+ });
536
+ context.addRollback(async () => {
537
+ value1 = SET_IN_ROLLBACK.value1;
538
+ });
539
+ throw new Error("fail");
540
+ }, src_1.EventStatusEnum.Failed);
541
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
542
+ });
543
+ describe("Explicit Rollbacks", async () => {
544
+ it("Should not perform rollbacks if event handler succeeds", async () => {
545
+ await doTest((context) => {
546
+ value1 = SET_IN_HANDLER.value1;
547
+ context.addRollback(() => {
548
+ value1 = SET_IN_ROLLBACK.value1;
549
+ });
550
+ });
551
+ (0, chai_1.expect)(value1).to.equal(SET_IN_HANDLER.value1);
552
+ });
553
+ it("Should perform rollbacks if event handler fails", async () => {
554
+ await doTest((context) => {
555
+ value1 = SET_IN_HANDLER.value1;
556
+ context.addRollback(() => {
557
+ value1 = SET_IN_ROLLBACK.value1;
558
+ });
559
+ throw new Error("fail");
560
+ }, src_1.EventStatusEnum.Failed);
561
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
562
+ });
563
+ it("Should perform rollbacks if modifications fail", async () => {
564
+ const projectionId = {
565
+ companyId: deps.companyId,
566
+ projectionType: "testProjection",
567
+ entityId: (0, crypto_1.randomUUID)(),
568
+ };
569
+ await deps.projectionRepository.putItem({
570
+ ...projectionId,
571
+ payload: { value: 100 },
572
+ });
573
+ await doTest((context) => {
574
+ value1 = SET_IN_HANDLER.value1;
575
+ context.addRollback(() => {
576
+ value1 = SET_IN_ROLLBACK.value1;
577
+ });
578
+ context.projectionModifier.createProjection({
579
+ ...projectionId,
580
+ payload: { value: 1000 },
581
+ });
582
+ }, src_1.EventStatusEnum.Failed);
583
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
584
+ const loaded = await deps.projectionRepository.loadItem(projectionId);
585
+ (0, chai_1.expect)(loaded.payload.value).to.equal(100);
586
+ });
587
+ it("Should perform multiple rollbacks", async () => {
588
+ await doTest((context) => {
589
+ value1 = SET_IN_HANDLER.value1;
590
+ value2 = SET_IN_HANDLER.value2;
591
+ value3 = SET_IN_HANDLER.value3;
592
+ context.addRollback(() => {
593
+ value1 = SET_IN_ROLLBACK.value1;
594
+ });
595
+ context.addRollback("Named rollback", () => {
596
+ value2 = SET_IN_ROLLBACK.value2;
597
+ });
598
+ context.addRollback(() => {
599
+ value3 = SET_IN_ROLLBACK.value3;
600
+ });
601
+ throw new Error("Fail");
602
+ }, src_1.EventStatusEnum.Failed);
603
+ (0, chai_1.expect)(value1).to.equal(SET_IN_ROLLBACK.value1);
604
+ (0, chai_1.expect)(value2).to.equal(SET_IN_ROLLBACK.value2);
605
+ (0, chai_1.expect)(value3).to.equal(SET_IN_ROLLBACK.value3);
606
+ });
607
+ });
608
+ });
272
609
  });
273
610
  //# sourceMappingURL=baseEventHandler.spec.js.map