|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { IEventBusModuleService } from "@medusajs/types" |
|
3
|
import { composeMessage, Modules, PaymentWebhookEvents } from "@medusajs/utils" |
|
4
|
import path from "path" |
|
5
|
|
|
6
|
jest.setTimeout(100000) |
|
7
|
|
|
8
|
function getJobCounts(queue) { |
|
9
|
return queue.waiting + queue.delayed + (queue.prioritized || 0) |
|
10
|
} |
|
11
|
|
|
12
|
medusaIntegrationTestRunner({ |
|
13
|
medusaConfigFile: path.join( |
|
14
|
__dirname, |
|
15
|
"../../__fixtures__/worker-mode-server" |
|
16
|
), |
|
17
|
testSuite: ({ getContainer }) => { |
|
18
|
describe("Event Bus - Server Worker Mode", () => { |
|
19
|
let eventBus: IEventBusModuleService |
|
20
|
|
|
21
|
beforeAll(() => { |
|
22
|
eventBus = getContainer().resolve(Modules.EVENT_BUS) |
|
23
|
}) |
|
24
|
|
|
25
|
it("should register subscribers, queue events with subscribers, and skip events without subscribers", async () => { |
|
26
|
const subscribersMap = (eventBus as any).eventToSubscribersMap |
|
27
|
expect(subscribersMap).toBeDefined() |
|
28
|
expect(subscribersMap.size).toBeGreaterThan(0) |
|
29
|
|
|
30
|
const paymentWebhookSubscribers = subscribersMap.get( |
|
31
|
PaymentWebhookEvents.WebhookReceived |
|
32
|
) |
|
33
|
expect(paymentWebhookSubscribers).toBeDefined() |
|
34
|
expect(paymentWebhookSubscribers.length).toBeGreaterThan(0) |
|
35
|
|
|
36
|
const bullWorker = (eventBus as any).bullWorker_ |
|
37
|
expect(bullWorker).toBeUndefined() |
|
38
|
|
|
39
|
const testEventName = "test.server-mode-event" |
|
40
|
const subscriberMock = jest.fn() |
|
41
|
|
|
42
|
eventBus.subscribe(testEventName, subscriberMock, { |
|
43
|
subscriberId: "test-server-mode-subscriber", |
|
44
|
}) |
|
45
|
expect(subscribersMap.get(testEventName)).toBeDefined() |
|
46
|
|
|
47
|
const queue = (eventBus as any).queue_ |
|
48
|
const jobCountsBefore = await queue.getJobCounts() |
|
49
|
const totalJobsBefore = getJobCounts(jobCountsBefore) |
|
50
|
|
|
51
|
await eventBus.emit( |
|
52
|
composeMessage(testEventName, { |
|
53
|
data: { test: "data" }, |
|
54
|
object: "test", |
|
55
|
source: "integration-test", |
|
56
|
action: "created", |
|
57
|
}) |
|
58
|
) |
|
59
|
|
|
60
|
const jobCountsAfterWithSubscriber = await queue.getJobCounts() |
|
61
|
const totalJobsAfterWithSubscriber = getJobCounts( |
|
62
|
jobCountsAfterWithSubscriber |
|
63
|
) |
|
64
|
|
|
65
|
expect(totalJobsAfterWithSubscriber).toBeGreaterThan(totalJobsBefore) |
|
66
|
|
|
67
|
await new Promise((resolve) => setTimeout(resolve, 500)) |
|
68
|
|
|
69
|
expect(subscriberMock).not.toHaveBeenCalled() |
|
70
|
|
|
71
|
const eventWithNoSubscribers = "test.event-without-subscribers" |
|
72
|
expect(subscribersMap.get(eventWithNoSubscribers)).toBeUndefined() |
|
73
|
|
|
74
|
const jobCountsBeforeNoSub = await queue.getJobCounts() |
|
75
|
const totalJobsBeforeNoSub = |
|
76
|
jobCountsBeforeNoSub.waiting + jobCountsBeforeNoSub.delayed |
|
77
|
|
|
78
|
await eventBus.emit( |
|
79
|
composeMessage(eventWithNoSubscribers, { |
|
80
|
data: { test: "should-not-be-queued" }, |
|
81
|
object: "test", |
|
82
|
source: "integration-test", |
|
83
|
action: "created", |
|
84
|
}) |
|
85
|
) |
|
86
|
|
|
87
|
const jobCountsAfterNoSub = await queue.getJobCounts() |
|
88
|
const totalJobsAfterNoSub = |
|
89
|
jobCountsAfterNoSub.waiting + jobCountsAfterNoSub.delayed |
|
90
|
|
|
91
|
expect(totalJobsAfterNoSub).toBe(totalJobsBeforeNoSub) |
|
92
|
}) |
|
93
|
}) |
|
94
|
}, |
|
95
|
}) |