|
1
|
import { ILockingModule } from "@medusajs/framework/types" |
|
2
|
import { Modules, promiseAll } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { setTimeout } from "node:timers/promises" |
|
5
|
|
|
6
|
jest.setTimeout(10000) |
|
7
|
|
|
8
|
moduleIntegrationTestRunner<ILockingModule>({ |
|
9
|
moduleName: Modules.LOCKING, |
|
10
|
testSuite: ({ service }) => { |
|
11
|
describe("Locking Module Service", () => { |
|
12
|
let stock = 5 |
|
13
|
function replenishStock() { |
|
14
|
stock = 5 |
|
15
|
} |
|
16
|
function hasStock() { |
|
17
|
return stock > 0 |
|
18
|
} |
|
19
|
async function reduceStock() { |
|
20
|
await setTimeout(10) |
|
21
|
stock-- |
|
22
|
} |
|
23
|
async function buy() { |
|
24
|
if (hasStock()) { |
|
25
|
await reduceStock() |
|
26
|
return true |
|
27
|
} |
|
28
|
return false |
|
29
|
} |
|
30
|
|
|
31
|
it("should execute functions respecting the key locked", async () => { |
|
32
|
// 10 parallel calls to buy should oversell the stock |
|
33
|
const prom: any[] = [] |
|
34
|
for (let i = 0; i < 10; i++) { |
|
35
|
prom.push(buy()) |
|
36
|
} |
|
37
|
await Promise.all(prom) |
|
38
|
expect(stock).toBe(-5) |
|
39
|
|
|
40
|
replenishStock() |
|
41
|
|
|
42
|
// 10 parallel calls to buy with lock should not oversell the stock |
|
43
|
const promWLock: any[] = [] |
|
44
|
for (let i = 0; i < 10; i++) { |
|
45
|
promWLock.push(service.execute("item_1", buy)) |
|
46
|
} |
|
47
|
await Promise.all(promWLock) |
|
48
|
|
|
49
|
expect(stock).toBe(0) |
|
50
|
}) |
|
51
|
|
|
52
|
it("should acquire lock and release it", async () => { |
|
53
|
await service.acquire("key_name", { |
|
54
|
ownerId: "user_id_123", |
|
55
|
}) |
|
56
|
|
|
57
|
const userReleased = await service.release("key_name", { |
|
58
|
ownerId: "user_id_456", |
|
59
|
}) |
|
60
|
const anotherUserLock = service.acquire("key_name", { |
|
61
|
ownerId: "user_id_456", |
|
62
|
}) |
|
63
|
|
|
64
|
expect(userReleased).toBe(false) |
|
65
|
await expect(anotherUserLock).rejects.toThrowError( |
|
66
|
`Failed to acquire lock for key "key_name"` |
|
67
|
) |
|
68
|
|
|
69
|
const releasing = await service.release("key_name", { |
|
70
|
ownerId: "user_id_123", |
|
71
|
}) |
|
72
|
|
|
73
|
expect(releasing).toBe(true) |
|
74
|
}) |
|
75
|
|
|
76
|
it("should acquire lock and release it during parallel calls", async () => { |
|
77
|
const keyToLock = "mySpecialKey" |
|
78
|
const user_1 = { |
|
79
|
ownerId: "user_id_456", |
|
80
|
} |
|
81
|
const user_2 = { |
|
82
|
ownerId: "user_id_000", |
|
83
|
} |
|
84
|
|
|
85
|
await expect( |
|
86
|
service.acquire(keyToLock, user_1) |
|
87
|
).resolves.toBeUndefined() |
|
88
|
|
|
89
|
await expect( |
|
90
|
service.acquire(keyToLock, user_1) |
|
91
|
).resolves.toBeUndefined() |
|
92
|
|
|
93
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrowError( |
|
94
|
`Failed to acquire lock for key "${keyToLock}"` |
|
95
|
) |
|
96
|
|
|
97
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrowError( |
|
98
|
`Failed to acquire lock for key "${keyToLock}"` |
|
99
|
) |
|
100
|
|
|
101
|
await service.acquire(keyToLock, user_1) |
|
102
|
|
|
103
|
const releaseNotLocked = await service.release(keyToLock, { |
|
104
|
ownerId: "user_id_000", |
|
105
|
}) |
|
106
|
expect(releaseNotLocked).toBe(false) |
|
107
|
|
|
108
|
const release = await service.release(keyToLock, user_1) |
|
109
|
expect(release).toBe(true) |
|
110
|
}) |
|
111
|
|
|
112
|
it("should fail to acquire the same key when no owner is provided", async () => { |
|
113
|
const keyToLock = "mySpecialKey" |
|
114
|
|
|
115
|
const user_2 = { |
|
116
|
ownerId: "user_id_000", |
|
117
|
} |
|
118
|
|
|
119
|
await expect(service.acquire(keyToLock)).resolves.toBeUndefined() |
|
120
|
|
|
121
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
122
|
`Failed to acquire lock for key "${keyToLock}"` |
|
123
|
) |
|
124
|
|
|
125
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
126
|
`Failed to acquire lock for key "${keyToLock}"` |
|
127
|
) |
|
128
|
|
|
129
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
130
|
`Failed to acquire lock for key "${keyToLock}"` |
|
131
|
) |
|
132
|
|
|
133
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
134
|
`Failed to acquire lock for key "${keyToLock}"` |
|
135
|
) |
|
136
|
|
|
137
|
const releaseNotLocked = await service.release(keyToLock, { |
|
138
|
ownerId: "user_id_000", |
|
139
|
}) |
|
140
|
expect(releaseNotLocked).toBe(false) |
|
141
|
|
|
142
|
const release = await service.release(keyToLock) |
|
143
|
expect(release).toBe(true) |
|
144
|
}) |
|
145
|
}) |
|
146
|
|
|
147
|
it("should release lock in case of failure", async () => { |
|
148
|
const fn_1 = jest.fn(async () => { |
|
149
|
throw new Error("Error") |
|
150
|
}) |
|
151
|
const fn_2 = jest.fn(async () => {}) |
|
152
|
|
|
153
|
await service.execute("lock_key", fn_1).catch(() => {}) |
|
154
|
await service.execute("lock_key", fn_2).catch(() => {}) |
|
155
|
|
|
156
|
expect(fn_1).toBeCalledTimes(1) |
|
157
|
expect(fn_2).toBeCalledTimes(1) |
|
158
|
}) |
|
159
|
|
|
160
|
it("should release lock in case of timeout failure", async () => { |
|
161
|
const fn_1 = jest.fn(async () => { |
|
162
|
await setTimeout(1010) |
|
163
|
return "fn_1" |
|
164
|
}) |
|
165
|
|
|
166
|
const fn_2 = jest.fn(async () => { |
|
167
|
return "fn_2" |
|
168
|
}) |
|
169
|
|
|
170
|
const fn_3 = jest.fn(async () => { |
|
171
|
return "fn_3" |
|
172
|
}) |
|
173
|
|
|
174
|
const ops = [ |
|
175
|
service |
|
176
|
.execute("lock_key", fn_1, { |
|
177
|
timeout: 1, |
|
178
|
}) |
|
179
|
.catch((e) => e), |
|
180
|
|
|
181
|
service |
|
182
|
.execute("lock_key", fn_2, { |
|
183
|
timeout: 1, |
|
184
|
}) |
|
185
|
.catch((e) => e), |
|
186
|
|
|
187
|
service |
|
188
|
.execute("lock_key", fn_3, { |
|
189
|
timeout: 2, |
|
190
|
}) |
|
191
|
.catch((e) => e), |
|
192
|
] |
|
193
|
|
|
194
|
const res = await promiseAll(ops) |
|
195
|
|
|
196
|
expect(res).toEqual(["fn_1", Error("Timed-out acquiring lock."), "fn_3"]) |
|
197
|
|
|
198
|
expect(fn_1).toHaveBeenCalledTimes(1) |
|
199
|
expect(fn_2).toHaveBeenCalledTimes(0) |
|
200
|
expect(fn_3).toHaveBeenCalledTimes(1) |
|
201
|
}) |
|
202
|
|
|
203
|
it("should hold the keys until the job is done", async () => { |
|
204
|
const result = await service.execute("held_key", async () => { |
|
205
|
// No wait needed to see this: an acquisition that isn't queueing is |
|
206
|
// refused as soon as it finds the keys taken. |
|
207
|
await expect( |
|
208
|
service.acquire("held_key", { ownerId: "someone_else" }) |
|
209
|
).rejects.toThrow() |
|
210
|
|
|
211
|
return "done" |
|
212
|
}) |
|
213
|
|
|
214
|
expect(result).toEqual("done") |
|
215
|
|
|
216
|
// And the job being done is what frees them, under the same owner that |
|
217
|
// took them — a release under a different owner would leave them locked. |
|
218
|
await service.acquire("held_key", { ownerId: "someone_else" }) |
|
219
|
|
|
220
|
await expect( |
|
221
|
service.release("held_key", { ownerId: "someone_else" }) |
|
222
|
).resolves.toBe(true) |
|
223
|
}) |
|
224
|
}, |
|
225
|
}) |