@mavenmachines/event-handler-lib 1.5.26-rc.4 → 1.5.26-rc.6
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.
- package/README.md +103 -0
- package/dist/src/clients/openSearch/openSearchClient.d.ts +3 -2
- package/dist/src/clients/openSearch/openSearchClient.js +76 -70
- package/dist/src/clients/openSearch/openSearchClient.js.map +1 -1
- package/dist/src/clients/openSearch/types.d.ts +54 -0
- package/dist/src/clients/openSearch/types.js +86 -0
- package/dist/src/clients/openSearch/types.js.map +1 -0
- package/dist/src/exports/clients.d.ts +1 -0
- package/dist/src/exports/clients.js +15 -0
- package/dist/src/exports/clients.js.map +1 -1
- package/dist/src/handlers/baseHandler.d.ts +19 -1
- package/dist/src/handlers/baseHandler.js +145 -23
- package/dist/src/handlers/baseHandler.js.map +1 -1
- package/dist/src/handlers/commandHandlers/baseCommandHandler.d.ts +4 -1
- package/dist/src/handlers/commandHandlers/baseCommandHandler.js +5 -3
- package/dist/src/handlers/commandHandlers/baseCommandHandler.js.map +1 -1
- package/dist/src/handlers/crud/helpers.d.ts +1 -1
- package/dist/src/handlers/crud/helpers.js +1 -1
- package/dist/src/handlers/crud/helpers.js.map +1 -1
- package/dist/src/handlers/crud/searchProjectionsHandler.js.map +1 -1
- package/dist/src/handlers/crud/types.d.ts +1 -21
- package/dist/src/handlers/crud/types.js +1 -8
- package/dist/src/handlers/crud/types.js.map +1 -1
- package/dist/src/handlers/eventHandlers/baseEventHandler.d.ts +0 -1
- package/dist/src/handlers/eventHandlers/baseEventHandler.js +6 -3
- package/dist/src/handlers/eventHandlers/baseEventHandler.js.map +1 -1
- package/dist/src/helpers/array.helpers.d.ts +6 -0
- package/dist/src/helpers/array.helpers.js +59 -0
- package/dist/src/helpers/array.helpers.js.map +1 -0
- package/dist/src/helpers/dataStructure.helpers.d.ts +1 -0
- package/dist/src/helpers/dataStructure.helpers.js +19 -0
- package/dist/src/helpers/dataStructure.helpers.js.map +1 -1
- package/dist/src/repositories/baseDynamoRepository.js +1 -0
- package/dist/src/repositories/baseDynamoRepository.js.map +1 -1
- package/dist/src/testUtils/mocks/kafkajs.mock.js +11 -0
- package/dist/src/testUtils/mocks/kafkajs.mock.js.map +1 -1
- package/dist/src/testUtils/mocks/openSearch.client.mock.js +10 -4
- package/dist/src/testUtils/mocks/openSearch.client.mock.js.map +1 -1
- package/dist/src/testUtils/testDependencies.js +14 -6
- package/dist/src/testUtils/testDependencies.js.map +1 -1
- package/dist/src/types/enums/events.d.ts +2 -1
- package/dist/src/types/enums/events.js +1 -0
- package/dist/src/types/enums/events.js.map +1 -1
- package/dist/test/testImplementations/testCrud.js +1 -0
- package/dist/test/testImplementations/testCrud.js.map +1 -1
- package/dist/test/testImplementations/testEventHandler.js +6 -1
- package/dist/test/testImplementations/testEventHandler.js.map +1 -1
- package/dist/test/tests/eventHandlers/baseEventHandler.spec.js +386 -49
- package/dist/test/tests/eventHandlers/baseEventHandler.spec.js.map +1 -1
- package/dist/test/tests/eventHandlers/crudHandlers.spec.js +64 -1
- package/dist/test/tests/eventHandlers/crudHandlers.spec.js.map +1 -1
- package/dist/test/tests/helpers/array.helpers.spec.d.ts +1 -0
- package/dist/test/tests/helpers/array.helpers.spec.js +48 -0
- package/dist/test/tests/helpers/array.helpers.spec.js.map +1 -0
- 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
|
|
14
|
+
let deps;
|
|
14
15
|
let eventHandler;
|
|
15
16
|
before(async () => {
|
|
16
17
|
appContainer = await (0, common_1.getTestAppContainer)();
|
|
17
|
-
|
|
18
|
+
deps = await (0, src_1.getTestDependencies)(appContainer);
|
|
18
19
|
eventHandler = appContainer.get(testEventHandler_1.TestEventHandler);
|
|
19
20
|
});
|
|
20
|
-
(0, src_1.registerTestHooks)(() =>
|
|
21
|
+
(0, src_1.registerTestHooks)(() => deps);
|
|
21
22
|
it("should handle an event", async () => {
|
|
22
|
-
const body = await
|
|
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
|
|
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
|
|
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
|
|
57
|
-
await
|
|
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
|
|
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
|
|
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)(
|
|
84
|
-
const callArgs =
|
|
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
|
|
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
|
|
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)(
|
|
103
|
-
return
|
|
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,47 +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
|
|
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)(
|
|
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
|
-
|
|
125
|
-
const body = await
|
|
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)(
|
|
130
|
+
(0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.called).to.equal(false);
|
|
128
131
|
});
|
|
129
132
|
it("should send only return events not configured for exclusion", async () => {
|
|
130
133
|
const excludedReturnEvent = {
|
|
131
134
|
...returnEvent,
|
|
132
135
|
eventType: "excluded_event",
|
|
133
136
|
};
|
|
134
|
-
|
|
135
|
-
const body = await
|
|
137
|
+
deps.directoryServicesClientMock.setConfigurationObject(deps.companyId, "eventSourcing", "excludedReturnEventTypes", [excludedReturnEvent.eventType]);
|
|
138
|
+
const body = await deps.runEvent(eventHandler, {
|
|
136
139
|
returnEvents: [excludedReturnEvent, returnEvent],
|
|
137
140
|
});
|
|
138
141
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
|
|
139
|
-
(0, chai_1.expect)(
|
|
142
|
+
(0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.deep.equal([
|
|
143
|
+
[returnEvent],
|
|
144
|
+
]);
|
|
140
145
|
});
|
|
141
146
|
it("should send return events when exclusion configuration is malformed", async () => {
|
|
142
|
-
|
|
143
|
-
const body = await
|
|
147
|
+
deps.directoryServicesClientMock.setConfigurationObject(deps.companyId, "eventSourcing", "excludedReturnEventTypes", { eventTypes: [returnEvent.eventType] });
|
|
148
|
+
const body = await deps.runEvent(eventHandler, { returnEvent });
|
|
144
149
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
|
|
145
|
-
(0, chai_1.expect)(
|
|
150
|
+
(0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.deep.equal([
|
|
151
|
+
[returnEvent],
|
|
152
|
+
]);
|
|
146
153
|
});
|
|
147
154
|
it("should not generate return events on failure", async () => {
|
|
148
|
-
const body = await
|
|
155
|
+
const body = await deps.runEvent(eventHandler, {
|
|
149
156
|
returnEvent,
|
|
150
157
|
fail: true,
|
|
151
158
|
});
|
|
152
159
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Failed);
|
|
153
|
-
(0, chai_1.expect)(
|
|
154
|
-
.empty;
|
|
160
|
+
(0, chai_1.expect)(deps.returnEventService.addReturnEvent.stub.args).to.be.empty;
|
|
155
161
|
});
|
|
156
162
|
it("should successfully delete a projection declaration", async () => {
|
|
157
163
|
const projection = await bootstrapProjection({ key: "value" });
|
|
158
|
-
await
|
|
159
|
-
const body = await
|
|
164
|
+
await deps.runEvent(eventHandler, { projection });
|
|
165
|
+
const body = await deps.runEvent(eventHandler, {
|
|
160
166
|
projection: {
|
|
161
167
|
companyId: projection.companyId,
|
|
162
168
|
projectionType: projection.projectionType,
|
|
@@ -166,7 +172,7 @@ describe("BaseEventHandler", () => {
|
|
|
166
172
|
});
|
|
167
173
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
|
|
168
174
|
try {
|
|
169
|
-
await
|
|
175
|
+
await deps.projectionRepository.loadItem({
|
|
170
176
|
companyId: projection.companyId,
|
|
171
177
|
projectionType: projection.projectionType,
|
|
172
178
|
entityId: projection.entityId,
|
|
@@ -177,9 +183,9 @@ describe("BaseEventHandler", () => {
|
|
|
177
183
|
(0, chai_1.expect)(err).to.be.instanceOf(src_1.ItemNotFound);
|
|
178
184
|
(0, chai_1.expect)(err.message).to.equal(`Projection ${projection.companyId_projectionType_entityId} is deleted`);
|
|
179
185
|
}
|
|
180
|
-
(0, chai_1.expect)(
|
|
181
|
-
console.log(
|
|
182
|
-
const updatedEvent =
|
|
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];
|
|
183
189
|
(0, chai_1.expect)(updatedEvent.eventType).to.equal((0, src_1.projectionDeletedEventName)(projection.projectionType));
|
|
184
190
|
(0, chai_1.expect)(updatedEvent.payload.projection.payload).to.deep.equal({
|
|
185
191
|
isDeletion: true,
|
|
@@ -193,15 +199,15 @@ describe("BaseEventHandler", () => {
|
|
|
193
199
|
});
|
|
194
200
|
it("should successfully delete a loaded projection", async () => {
|
|
195
201
|
const projection = await bootstrapProjection({ key: "value" });
|
|
196
|
-
await
|
|
202
|
+
await deps.runEvent(eventHandler, { projection });
|
|
197
203
|
projection.version += 1;
|
|
198
|
-
const body = await
|
|
204
|
+
const body = await deps.runEvent(eventHandler, {
|
|
199
205
|
projection,
|
|
200
206
|
delete: true,
|
|
201
207
|
});
|
|
202
208
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
|
|
203
209
|
try {
|
|
204
|
-
await
|
|
210
|
+
await deps.projectionRepository.loadItem({
|
|
205
211
|
companyId: projection.companyId,
|
|
206
212
|
projectionType: projection.projectionType,
|
|
207
213
|
entityId: projection.entityId,
|
|
@@ -212,10 +218,10 @@ describe("BaseEventHandler", () => {
|
|
|
212
218
|
(0, chai_1.expect)(err).to.be.instanceOf(src_1.ItemNotFound);
|
|
213
219
|
(0, chai_1.expect)(err.message).to.equal(`Projection ${projection.companyId_projectionType_entityId} is deleted`);
|
|
214
220
|
}
|
|
215
|
-
const indexedProjection = await
|
|
221
|
+
const indexedProjection = await deps.openSearch.getProjectionIndex(projection);
|
|
216
222
|
(0, common_1.expectDeletedProjectionIndex)(indexedProjection);
|
|
217
|
-
(0, chai_1.expect)(
|
|
218
|
-
const updatedEvent =
|
|
223
|
+
(0, chai_1.expect)(deps.eventLogService.addNewEvent.stub.args.length).to.equal(1);
|
|
224
|
+
const updatedEvent = deps.eventLogService.addNewEvent.stub.args[0][0];
|
|
219
225
|
(0, chai_1.expect)(updatedEvent.eventType).to.equal((0, src_1.projectionDeletedEventName)(projection.projectionType));
|
|
220
226
|
(0, chai_1.expect)(updatedEvent.payload.projection.payload).to.deep.equal({
|
|
221
227
|
isDeletion: true,
|
|
@@ -231,11 +237,11 @@ describe("BaseEventHandler", () => {
|
|
|
231
237
|
const projection = getProjection({ key: "value" });
|
|
232
238
|
projection.projectionType = "noIndex";
|
|
233
239
|
projection.companyId_projectionType_entityId = `${projection.companyId}_${projection.projectionType}_${projection.entityId}`;
|
|
234
|
-
const body = await
|
|
240
|
+
const body = await deps.runEvent(eventHandler, {
|
|
235
241
|
projection,
|
|
236
242
|
});
|
|
237
243
|
(0, chai_1.expect)(body.status).to.equal(src_1.EventStatusEnum.Succeeded);
|
|
238
|
-
await
|
|
244
|
+
await deps.projectionRepository.loadItem(projection);
|
|
239
245
|
});
|
|
240
246
|
it("should roll back all projections on a one projection's failure", async () => {
|
|
241
247
|
const projection = await bootstrapProjection({
|
|
@@ -246,7 +252,7 @@ describe("BaseEventHandler", () => {
|
|
|
246
252
|
key2: "value2",
|
|
247
253
|
description: "This should fail with a key conflict",
|
|
248
254
|
});
|
|
249
|
-
const result = await
|
|
255
|
+
const result = await deps.runEvent(eventHandler, {
|
|
250
256
|
projection,
|
|
251
257
|
modifications: { newKey: "newValue" },
|
|
252
258
|
projection2: {
|
|
@@ -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
|
|
269
|
+
const loadedProjection1 = await deps.projectionRepository.loadItem(projection);
|
|
264
270
|
(0, chai_1.expect)(loadedProjection1).to.deep.equal(projection);
|
|
265
|
-
const loadedProjection2 = await
|
|
271
|
+
const loadedProjection2 = await deps.projectionRepository.maybeLoadItem(projection2);
|
|
266
272
|
(0, chai_1.expect)(loadedProjection2).to.deep.equal(projection2);
|
|
267
|
-
const indexedProjection1 = await
|
|
273
|
+
const indexedProjection1 = await deps.openSearch.getProjectionIndex(projection);
|
|
268
274
|
(0, common_1.expectValidProjectionIndex)(indexedProjection1, projection);
|
|
269
|
-
const indexedProjection2 = await
|
|
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
|