@bsb/events-rabbitmq 0.0.1

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 (32) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +109 -0
  3. package/lib/plugins/events-rabbitmq/events/broadcast.d.ts +19 -0
  4. package/lib/plugins/events-rabbitmq/events/broadcast.js +105 -0
  5. package/lib/plugins/events-rabbitmq/events/emit.d.ts +17 -0
  6. package/lib/plugins/events-rabbitmq/events/emit.js +89 -0
  7. package/lib/plugins/events-rabbitmq/events/emitAndReturn.d.ts +20 -0
  8. package/lib/plugins/events-rabbitmq/events/emitAndReturn.js +207 -0
  9. package/lib/plugins/events-rabbitmq/events/emitStreamAndReceiveStream.d.ts +24 -0
  10. package/lib/plugins/events-rabbitmq/events/emitStreamAndReceiveStream.js +515 -0
  11. package/lib/plugins/events-rabbitmq/events/lib.d.ts +12 -0
  12. package/lib/plugins/events-rabbitmq/events/lib.js +59 -0
  13. package/lib/plugins/events-rabbitmq/index.d.ts +55 -0
  14. package/lib/plugins/events-rabbitmq/index.js +150 -0
  15. package/lib/plugins/events-rabbitmq/plugin.config.json +39 -0
  16. package/lib/schemas/events-rabbitmq.json +118 -0
  17. package/lib/schemas/events-rabbitmq.plugin.json +124 -0
  18. package/lib/tests/mocks.d.ts +3 -0
  19. package/lib/tests/mocks.js +65 -0
  20. package/lib/tests/plugins/events/events/broadcast.d.ts +4 -0
  21. package/lib/tests/plugins/events/events/broadcast.js +320 -0
  22. package/lib/tests/plugins/events/events/emit.d.ts +4 -0
  23. package/lib/tests/plugins/events/events/emit.js +316 -0
  24. package/lib/tests/plugins/events/events/emitAndReturn.d.ts +4 -0
  25. package/lib/tests/plugins/events/events/emitAndReturn.js +307 -0
  26. package/lib/tests/plugins/events/events/emitStreamAndReceiveStream.d.ts +4 -0
  27. package/lib/tests/plugins/events/events/emitStreamAndReceiveStream.js +199 -0
  28. package/lib/tests/plugins/events/plugin.d.ts +2 -0
  29. package/lib/tests/plugins/events/plugin.js +47 -0
  30. package/lib/tests/trace.d.ts +4 -0
  31. package/lib/tests/trace.js +39 -0
  32. package/package.json +70 -0
@@ -0,0 +1,320 @@
1
+ import * as assert from "assert";
2
+ import { randomUUID } from "crypto";
3
+ import { SmartFunctionCallSync } from "@bsb/base";
4
+ import { createTestObservable } from "../../../trace.js";
5
+ const randomName = () => randomUUID();
6
+ export function broadcast(genNewPlugin, maxTimeoutToExpectAResponse) {
7
+ let emitter;
8
+ beforeEach(async () => {
9
+ emitter = await genNewPlugin();
10
+ });
11
+ afterEach(function () {
12
+ SmartFunctionCallSync(emitter, emitter.dispose);
13
+ });
14
+ describe("EmitBroadcast", async function () {
15
+ this.timeout(maxTimeoutToExpectAResponse + 100);
16
+ describe("emitBroadcast", async () => {
17
+ const emitData = true;
18
+ it("all plugins should receive the event", async () => {
19
+ const thisPlugin = randomName();
20
+ const thisEvent = randomName();
21
+ const obs = createTestObservable();
22
+ let receiveCounter = 0;
23
+ setTimeout(() => {
24
+ if (receiveCounter === 2) {
25
+ return assert.ok(receiveCounter);
26
+ }
27
+ if (receiveCounter === 0) {
28
+ return assert.fail("Event not received");
29
+ }
30
+ assert.fail("Received " + receiveCounter + " events");
31
+ }, maxTimeoutToExpectAResponse);
32
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async (receivedObs, args) => {
33
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
34
+ receiveCounter++;
35
+ });
36
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async (receivedObs, args) => {
37
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
38
+ receiveCounter++;
39
+ });
40
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent, [emitData]);
41
+ await new Promise(resolve => setTimeout(resolve, 10));
42
+ });
43
+ it("diff plugin names should not receive same event", async () => {
44
+ const thisPlugin = randomName();
45
+ const thisPlugin2 = randomName();
46
+ const thisEvent = randomName();
47
+ const obs = createTestObservable();
48
+ let receiveCounter = 0;
49
+ setTimeout(() => {
50
+ if (receiveCounter === 2) {
51
+ return assert.ok(receiveCounter);
52
+ }
53
+ if (receiveCounter === 0) {
54
+ return assert.fail("Event not received");
55
+ }
56
+ assert.fail("Received " + receiveCounter + " events");
57
+ }, maxTimeoutToExpectAResponse);
58
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async () => {
59
+ assert.fail("Received on diff plugin name");
60
+ });
61
+ await emitter.onBroadcast(obs, thisPlugin2, thisEvent, async (receivedObs, args) => {
62
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
63
+ receiveCounter++;
64
+ });
65
+ await emitter.onBroadcast(obs, thisPlugin2, thisEvent, async (receivedObs, args) => {
66
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
67
+ receiveCounter++;
68
+ });
69
+ await emitter.emitBroadcast(obs, thisPlugin2, thisEvent, [emitData]);
70
+ await new Promise(resolve => setTimeout(resolve, 10));
71
+ });
72
+ it("should be able to emit to events with plugin name defined", async () => {
73
+ const thisPlugin = randomName();
74
+ const thisEvent = randomName();
75
+ const obs = createTestObservable();
76
+ const emitTimeout = setTimeout(() => {
77
+ assert.fail("Event not received");
78
+ }, maxTimeoutToExpectAResponse);
79
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
80
+ clearTimeout(emitTimeout);
81
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
82
+ assert.ok(data[0]);
83
+ });
84
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent, [emitData]);
85
+ await new Promise(resolve => setTimeout(resolve, 10));
86
+ });
87
+ it("should be able to emit to events with self", async () => {
88
+ const thisCaller = randomName();
89
+ const thisEvent = randomName();
90
+ const obs = createTestObservable();
91
+ const emitTimeout = setTimeout(() => {
92
+ assert.fail("Event not received");
93
+ }, maxTimeoutToExpectAResponse);
94
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async (receivedObs, data) => {
95
+ clearTimeout(emitTimeout);
96
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
97
+ assert.ok(data[0]);
98
+ });
99
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent, [emitData]);
100
+ await new Promise(resolve => setTimeout(resolve, 10));
101
+ });
102
+ it("should be able to emit to events with self multi-args", async () => {
103
+ const thisCaller = randomName();
104
+ const thisEvent = randomName();
105
+ const obs = createTestObservable();
106
+ const emitTimeout = setTimeout(() => {
107
+ assert.fail("Event not received");
108
+ }, maxTimeoutToExpectAResponse);
109
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async (receivedObs, data) => {
110
+ clearTimeout(emitTimeout);
111
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
112
+ assert.deepEqual(data[0], 0);
113
+ assert.deepEqual(data[1], 1);
114
+ assert.deepEqual(data[2], 2);
115
+ assert.deepEqual(data[3], 3);
116
+ });
117
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent, [0, 1, 2, 3]);
118
+ await new Promise(resolve => setTimeout(resolve, 10));
119
+ });
120
+ it("should not be able to emit to other events with plugin name defined", async () => {
121
+ const thisPlugin = randomName();
122
+ const thisEvent = randomName();
123
+ const thisEvent2 = randomName();
124
+ const obs = createTestObservable();
125
+ const emitTimeout = setTimeout(() => {
126
+ assert.ok(true);
127
+ }, maxTimeoutToExpectAResponse);
128
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async () => {
129
+ clearTimeout(emitTimeout);
130
+ assert.fail("Event received");
131
+ });
132
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent2, [emitData]);
133
+ await new Promise(resolve => setTimeout(resolve, 10));
134
+ });
135
+ it("should not be able to emit to other events with self", async () => {
136
+ const thisCaller = randomName();
137
+ const thisEvent = randomName();
138
+ const thisEvent2 = randomName();
139
+ const obs = createTestObservable();
140
+ const emitTimeout = setTimeout(() => {
141
+ assert.ok(true);
142
+ }, maxTimeoutToExpectAResponse);
143
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async () => {
144
+ clearTimeout(emitTimeout);
145
+ assert.fail("Event received");
146
+ });
147
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent2, [emitData]);
148
+ await new Promise(resolve => setTimeout(resolve, 10));
149
+ });
150
+ });
151
+ describe("onBroadcast", async () => {
152
+ const emitData = "ABCD";
153
+ it("should be able to emit to events with plugin name defined", async () => {
154
+ const thisPlugin = randomName();
155
+ const thisEvent = randomName();
156
+ const obs = createTestObservable();
157
+ const emitTimeout = setTimeout(() => {
158
+ assert.fail("Event not received");
159
+ }, maxTimeoutToExpectAResponse);
160
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
161
+ clearTimeout(emitTimeout);
162
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
163
+ assert.deepEqual(data[0], emitData);
164
+ });
165
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent, [emitData]);
166
+ await new Promise(resolve => setTimeout(resolve, 10));
167
+ });
168
+ it("should be able to emit to events with self", async () => {
169
+ const thisCaller = randomName();
170
+ const thisEvent = randomName();
171
+ const obs = createTestObservable();
172
+ const emitTimeout = setTimeout(() => {
173
+ assert.fail("Event not received");
174
+ }, maxTimeoutToExpectAResponse);
175
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async (receivedObs, data) => {
176
+ clearTimeout(emitTimeout);
177
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
178
+ assert.deepEqual(data[0], emitData);
179
+ });
180
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent, [emitData]);
181
+ await new Promise(resolve => setTimeout(resolve, 10));
182
+ });
183
+ it("should not be able to emit to other events with plugin name defined", async () => {
184
+ const thisPlugin = randomName();
185
+ const thisEvent = randomName();
186
+ const thisEvent2 = randomName();
187
+ const obs = createTestObservable();
188
+ const emitTimeout = setTimeout(() => {
189
+ assert.ok(true);
190
+ }, maxTimeoutToExpectAResponse);
191
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async () => {
192
+ clearTimeout(emitTimeout);
193
+ assert.fail("Event received");
194
+ });
195
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent2, [emitData]);
196
+ await new Promise(resolve => setTimeout(resolve, 10));
197
+ });
198
+ it("should not be able to emit to other events with self", async () => {
199
+ const thisCaller = randomName();
200
+ const thisEvent = randomName();
201
+ const thisEvent2 = randomName();
202
+ const obs = createTestObservable();
203
+ const emitTimeout = setTimeout(() => {
204
+ assert.ok(true);
205
+ }, maxTimeoutToExpectAResponse);
206
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async () => {
207
+ clearTimeout(emitTimeout);
208
+ assert.fail("Event received");
209
+ });
210
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent2, [emitData]);
211
+ await new Promise(resolve => setTimeout(resolve, 10));
212
+ });
213
+ });
214
+ const typesToTest = [
215
+ {
216
+ name: "Null",
217
+ data: null,
218
+ },
219
+ {
220
+ name: "Boolean true",
221
+ data: true,
222
+ },
223
+ {
224
+ name: "Boolean false",
225
+ data: false,
226
+ },
227
+ {
228
+ name: "String",
229
+ data: "HELLO WO4lD",
230
+ },
231
+ {
232
+ name: "Min Number",
233
+ data: Number.MIN_SAFE_INTEGER,
234
+ },
235
+ {
236
+ name: "Max Number",
237
+ data: Number.MAX_SAFE_INTEGER,
238
+ },
239
+ {
240
+ name: "Array",
241
+ data: [0, "Hello", true],
242
+ },
243
+ {
244
+ name: "Object",
245
+ data: {
246
+ name: "Sarah",
247
+ surname: "Blond",
248
+ age: 24,
249
+ meta: {
250
+ location: [-12212, 55336],
251
+ },
252
+ },
253
+ },
254
+ ];
255
+ for (const typeToTest of typesToTest) {
256
+ describe(`emitBroadcast ${typeToTest.name}`, async () => {
257
+ it("should be able to emit to events with plugin name defined", async () => {
258
+ const thisPlugin = randomName();
259
+ const thisEvent = randomName();
260
+ const obs = createTestObservable();
261
+ const emitTimeout = setTimeout(() => {
262
+ assert.fail("Event not received");
263
+ }, maxTimeoutToExpectAResponse);
264
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
265
+ clearTimeout(emitTimeout);
266
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
267
+ assert.deepEqual(data[0], typeToTest.data);
268
+ });
269
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent, [typeToTest.data]);
270
+ await new Promise(resolve => setTimeout(resolve, 10));
271
+ });
272
+ it("should be able to emit to events with self", async () => {
273
+ const thisCaller = randomName();
274
+ const thisEvent = randomName();
275
+ const obs = createTestObservable();
276
+ const emitTimeout = setTimeout(() => {
277
+ assert.fail("Event not received");
278
+ }, maxTimeoutToExpectAResponse);
279
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async (receivedObs, data) => {
280
+ clearTimeout(emitTimeout);
281
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
282
+ assert.deepEqual(data[0], typeToTest.data);
283
+ });
284
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent, [typeToTest.data]);
285
+ await new Promise(resolve => setTimeout(resolve, 10));
286
+ });
287
+ it("should not be able to emit to other events with plugin name defined", async () => {
288
+ const thisPlugin = randomName();
289
+ const thisEvent = randomName();
290
+ const thisEvent2 = randomName();
291
+ const obs = createTestObservable();
292
+ const emitTimeout = setTimeout(() => {
293
+ assert.ok(true);
294
+ }, maxTimeoutToExpectAResponse);
295
+ await emitter.onBroadcast(obs, thisPlugin, thisEvent, async () => {
296
+ clearTimeout(emitTimeout);
297
+ assert.fail("Event received");
298
+ });
299
+ await emitter.emitBroadcast(obs, thisPlugin, thisEvent2, [typeToTest.data]);
300
+ await new Promise(resolve => setTimeout(resolve, 10));
301
+ });
302
+ it("should not be able to emit to other events with self", async () => {
303
+ const thisCaller = randomName();
304
+ const thisEvent = randomName();
305
+ const thisEvent2 = randomName();
306
+ const obs = createTestObservable();
307
+ const emitTimeout = setTimeout(() => {
308
+ assert.ok(true);
309
+ }, maxTimeoutToExpectAResponse);
310
+ await emitter.onBroadcast(obs, thisCaller, thisEvent, async () => {
311
+ clearTimeout(emitTimeout);
312
+ assert.fail("Event received");
313
+ });
314
+ await emitter.emitBroadcast(obs, thisCaller, thisEvent2, [typeToTest.data]);
315
+ await new Promise(resolve => setTimeout(resolve, 10));
316
+ });
317
+ });
318
+ }
319
+ });
320
+ }
@@ -0,0 +1,4 @@
1
+ import { BSBEvents } from "@bsb/base";
2
+ export declare function emit(genNewPlugin: {
3
+ (): Promise<BSBEvents>;
4
+ }, maxTimeoutToExpectAResponse: number): void;
@@ -0,0 +1,316 @@
1
+ import * as assert from "assert";
2
+ import { randomUUID } from "crypto";
3
+ import { SmartFunctionCallSync } from "@bsb/base";
4
+ import { createTestObservable } from "../../../trace.js";
5
+ const randomName = () => randomUUID();
6
+ export function emit(genNewPlugin, maxTimeoutToExpectAResponse) {
7
+ let emitter;
8
+ beforeEach(async () => {
9
+ emitter = await genNewPlugin();
10
+ });
11
+ afterEach(function () {
12
+ SmartFunctionCallSync(emitter, emitter.dispose);
13
+ });
14
+ describe("Emit", async function () {
15
+ this.timeout(maxTimeoutToExpectAResponse + 100);
16
+ describe("emitEvent", async () => {
17
+ const emitData = true;
18
+ it("only one plugin should receive the event", async () => {
19
+ const thisPlugin = randomName();
20
+ const thisEvent = randomName();
21
+ const obs = createTestObservable();
22
+ let receiveCounter = 0;
23
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async (receivedObs, args) => {
24
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
25
+ receiveCounter++;
26
+ });
27
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async (receivedObs, args) => {
28
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
29
+ receiveCounter++;
30
+ });
31
+ await emitter.emitEvent(obs, thisPlugin, thisEvent, [emitData]);
32
+ await new Promise(resolve => setTimeout(resolve, 10));
33
+ if (receiveCounter === 1) {
34
+ return assert.ok(receiveCounter);
35
+ }
36
+ if (receiveCounter === 0) {
37
+ return assert.fail("Event not received");
38
+ }
39
+ assert.fail("Received " + receiveCounter + " events");
40
+ });
41
+ it("diff plugin names should not receive same event", async () => {
42
+ const thisPlugin = randomName();
43
+ const thisPlugin2 = randomName();
44
+ const thisEvent = randomName();
45
+ const obs = createTestObservable();
46
+ let receiveCounter = 0;
47
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async () => {
48
+ assert.fail("Received on diff plugin name");
49
+ });
50
+ await emitter.onEvent(obs, thisPlugin2, thisEvent, async (receivedObs, args) => {
51
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
52
+ receiveCounter++;
53
+ });
54
+ await emitter.onEvent(obs, thisPlugin2, thisEvent, async (receivedObs, args) => {
55
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
56
+ receiveCounter++;
57
+ });
58
+ await emitter.emitEvent(obs, thisPlugin2, thisEvent, [emitData]);
59
+ await new Promise(resolve => setTimeout(resolve, 10));
60
+ if (receiveCounter === 1) {
61
+ return assert.ok(receiveCounter);
62
+ }
63
+ if (receiveCounter === 0) {
64
+ return assert.fail("Event not received");
65
+ }
66
+ assert.fail("Received " + receiveCounter + " events");
67
+ });
68
+ it("should be able to emit to events with plugin name defined", async () => {
69
+ const thisPlugin = randomName();
70
+ const thisEvent = randomName();
71
+ const obs = createTestObservable();
72
+ const emitTimeout = setTimeout(() => {
73
+ assert.fail("Event not received");
74
+ }, maxTimeoutToExpectAResponse);
75
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
76
+ clearTimeout(emitTimeout);
77
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
78
+ assert.ok(data[0]);
79
+ });
80
+ await emitter.emitEvent(obs, thisPlugin, thisEvent, [emitData]);
81
+ await new Promise(resolve => setTimeout(resolve, 10));
82
+ });
83
+ it("should be able to emit to events with self", async () => {
84
+ const thisCaller = randomName();
85
+ const thisEvent = randomName();
86
+ const obs = createTestObservable();
87
+ const emitTimeout = setTimeout(() => {
88
+ assert.fail("Event not received");
89
+ }, maxTimeoutToExpectAResponse);
90
+ await emitter.onEvent(obs, thisCaller, thisEvent, async (receivedObs, data) => {
91
+ clearTimeout(emitTimeout);
92
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
93
+ assert.ok(data[0]);
94
+ });
95
+ await emitter.emitEvent(obs, thisCaller, thisEvent, [emitData]);
96
+ await new Promise(resolve => setTimeout(resolve, 10));
97
+ });
98
+ it("should be able to emit to events with self multi-args", async () => {
99
+ const thisCaller = randomName();
100
+ const thisEvent = randomName();
101
+ const obs = createTestObservable();
102
+ const emitTimeout = setTimeout(() => {
103
+ assert.fail("Event not received");
104
+ }, maxTimeoutToExpectAResponse);
105
+ await emitter.onEvent(obs, thisCaller, thisEvent, async (receivedObs, data) => {
106
+ clearTimeout(emitTimeout);
107
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
108
+ assert.deepEqual(data[0], 0);
109
+ assert.deepEqual(data[1], 1);
110
+ assert.deepEqual(data[2], 2);
111
+ assert.deepEqual(data[3], 3);
112
+ });
113
+ await emitter.emitEvent(obs, thisCaller, thisEvent, [0, 1, 2, 3]);
114
+ await new Promise(resolve => setTimeout(resolve, 10));
115
+ });
116
+ it("should not be able to emit to other events with plugin name defined", async () => {
117
+ const thisPlugin = randomName();
118
+ const thisEvent = randomName();
119
+ const thisEvent2 = randomName();
120
+ const obs = createTestObservable();
121
+ const emitTimeout = setTimeout(() => {
122
+ assert.ok(true);
123
+ }, maxTimeoutToExpectAResponse);
124
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async () => {
125
+ clearTimeout(emitTimeout);
126
+ assert.fail("Event received");
127
+ });
128
+ await emitter.emitEvent(obs, thisPlugin, thisEvent2, [emitData]);
129
+ await new Promise(resolve => setTimeout(resolve, 10));
130
+ });
131
+ it("should not be able to emit to other events with self", async () => {
132
+ const thisCaller = randomName();
133
+ const thisEvent = randomName();
134
+ const thisEvent2 = randomName();
135
+ const obs = createTestObservable();
136
+ const emitTimeout = setTimeout(() => {
137
+ assert.ok(true);
138
+ }, maxTimeoutToExpectAResponse);
139
+ await emitter.onEvent(obs, thisCaller, thisEvent, async () => {
140
+ clearTimeout(emitTimeout);
141
+ assert.fail("Event received");
142
+ });
143
+ await emitter.emitEvent(obs, thisCaller, thisEvent2, [emitData]);
144
+ await new Promise(resolve => setTimeout(resolve, 10));
145
+ });
146
+ });
147
+ describe("onEvent", async () => {
148
+ const emitData = "ABCD";
149
+ it("should be able to emit to events with plugin name defined", async () => {
150
+ const thisPlugin = randomName();
151
+ const thisEvent = randomName();
152
+ const obs = createTestObservable();
153
+ const emitTimeout = setTimeout(() => {
154
+ assert.fail("Event not received");
155
+ }, maxTimeoutToExpectAResponse);
156
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
157
+ clearTimeout(emitTimeout);
158
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
159
+ assert.deepEqual(data[0], emitData);
160
+ });
161
+ await emitter.emitEvent(obs, thisPlugin, thisEvent, [emitData]);
162
+ await new Promise(resolve => setTimeout(resolve, 10));
163
+ });
164
+ it("should be able to emit to events with self", async () => {
165
+ const thisCaller = randomName();
166
+ const thisEvent = randomName();
167
+ const obs = createTestObservable();
168
+ const emitTimeout = setTimeout(() => {
169
+ assert.fail("Event not received");
170
+ }, maxTimeoutToExpectAResponse);
171
+ await emitter.onEvent(obs, thisCaller, thisEvent, async (receivedObs, data) => {
172
+ clearTimeout(emitTimeout);
173
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
174
+ assert.deepEqual(data[0], emitData);
175
+ });
176
+ await emitter.emitEvent(obs, thisCaller, thisEvent, [emitData]);
177
+ await new Promise(resolve => setTimeout(resolve, 10));
178
+ });
179
+ it("should not be able to emit to other events with plugin name defined", async () => {
180
+ const thisPlugin = randomName();
181
+ const thisEvent = randomName();
182
+ const thisEvent2 = randomName();
183
+ const obs = createTestObservable();
184
+ const emitTimeout = setTimeout(() => {
185
+ assert.ok(true);
186
+ }, maxTimeoutToExpectAResponse);
187
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async () => {
188
+ clearTimeout(emitTimeout);
189
+ assert.fail("Event received");
190
+ });
191
+ await emitter.emitEvent(obs, thisPlugin, thisEvent2, [emitData]);
192
+ await new Promise(resolve => setTimeout(resolve, 10));
193
+ });
194
+ it("should not be able to emit to other events with self", async () => {
195
+ const thisCaller = randomName();
196
+ const thisEvent = randomName();
197
+ const thisEvent2 = randomName();
198
+ const obs = createTestObservable();
199
+ const emitTimeout = setTimeout(() => {
200
+ assert.ok(true);
201
+ }, maxTimeoutToExpectAResponse);
202
+ await emitter.onEvent(obs, thisCaller, thisEvent, async () => {
203
+ clearTimeout(emitTimeout);
204
+ assert.fail("Event received");
205
+ });
206
+ await emitter.emitEvent(obs, thisCaller, thisEvent2, [emitData]);
207
+ await new Promise(resolve => setTimeout(resolve, 10));
208
+ });
209
+ });
210
+ const typesToTest = [
211
+ {
212
+ name: "Null",
213
+ data: null,
214
+ },
215
+ {
216
+ name: "Boolean true",
217
+ data: true,
218
+ },
219
+ {
220
+ name: "Boolean false",
221
+ data: false,
222
+ },
223
+ {
224
+ name: "String",
225
+ data: "HELLO WO4lD",
226
+ },
227
+ {
228
+ name: "Min Number",
229
+ data: Number.MIN_SAFE_INTEGER,
230
+ },
231
+ {
232
+ name: "Max Number",
233
+ data: Number.MAX_SAFE_INTEGER,
234
+ },
235
+ {
236
+ name: "Array",
237
+ data: [0, "Hello", true],
238
+ },
239
+ {
240
+ name: "Object",
241
+ data: {
242
+ name: "Sarah",
243
+ surname: "Blond",
244
+ age: 24,
245
+ meta: {
246
+ location: [-12212, 55336],
247
+ },
248
+ },
249
+ },
250
+ ];
251
+ for (const typeToTest of typesToTest) {
252
+ describe(`emitEvent ${typeToTest.name}`, async () => {
253
+ it("should be able to emit to events with plugin name defined", async () => {
254
+ const thisPlugin = randomName();
255
+ const thisEvent = randomName();
256
+ const obs = createTestObservable();
257
+ const emitTimeout = setTimeout(() => {
258
+ assert.fail("Event not received");
259
+ }, maxTimeoutToExpectAResponse);
260
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async (receivedObs, data) => {
261
+ clearTimeout(emitTimeout);
262
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
263
+ assert.deepEqual(data[0], typeToTest.data);
264
+ });
265
+ await emitter.emitEvent(obs, thisPlugin, thisEvent, [typeToTest.data]);
266
+ await new Promise(resolve => setTimeout(resolve, 10));
267
+ });
268
+ it("should be able to emit to events with self", async () => {
269
+ const thisCaller = randomName();
270
+ const thisEvent = randomName();
271
+ const obs = createTestObservable();
272
+ const emitTimeout = setTimeout(() => {
273
+ assert.fail("Event not received");
274
+ }, maxTimeoutToExpectAResponse);
275
+ await emitter.onEvent(obs, thisCaller, thisEvent, async (receivedObs, data) => {
276
+ clearTimeout(emitTimeout);
277
+ assert.strictEqual(receivedObs.traceId, obs.traceId);
278
+ assert.deepEqual(data[0], typeToTest.data);
279
+ });
280
+ await emitter.emitEvent(obs, thisCaller, thisEvent, [typeToTest.data]);
281
+ await new Promise(resolve => setTimeout(resolve, 10));
282
+ });
283
+ it("should not be able to emit to other events with plugin name defined", async () => {
284
+ const thisPlugin = randomName();
285
+ const thisEvent = randomName();
286
+ const thisEvent2 = randomName();
287
+ const obs = createTestObservable();
288
+ const emitTimeout = setTimeout(() => {
289
+ assert.ok(true);
290
+ }, maxTimeoutToExpectAResponse);
291
+ await emitter.onEvent(obs, thisPlugin, thisEvent, async () => {
292
+ clearTimeout(emitTimeout);
293
+ assert.fail("Event received");
294
+ });
295
+ await emitter.emitEvent(obs, thisPlugin, thisEvent2, [typeToTest.data]);
296
+ await new Promise(resolve => setTimeout(resolve, 10));
297
+ });
298
+ it("should not be able to emit to other events with self", async () => {
299
+ const thisCaller = randomName();
300
+ const thisEvent = randomName();
301
+ const thisEvent2 = randomName();
302
+ const obs = createTestObservable();
303
+ const emitTimeout = setTimeout(() => {
304
+ assert.ok(true);
305
+ }, maxTimeoutToExpectAResponse);
306
+ await emitter.onEvent(obs, thisCaller, thisEvent, async () => {
307
+ clearTimeout(emitTimeout);
308
+ assert.fail("Event received");
309
+ });
310
+ await emitter.emitEvent(obs, thisCaller, thisEvent2, [typeToTest.data]);
311
+ await new Promise(resolve => setTimeout(resolve, 10));
312
+ });
313
+ });
314
+ }
315
+ });
316
+ }
@@ -0,0 +1,4 @@
1
+ import { BSBEvents } from "@bsb/base";
2
+ export declare function emitAndReturn(genNewPlugin: {
3
+ (): Promise<BSBEvents>;
4
+ }, maxTimeoutToExpectAResponse: number): void;