|
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(5000) |
|
7
|
|
|
8
|
const providerId = "locking-redis" |
|
9
|
moduleIntegrationTestRunner<ILockingModule>({ |
|
10
|
moduleName: Modules.LOCKING, |
|
11
|
moduleOptions: { |
|
12
|
providers: [ |
|
13
|
{ |
|
14
|
id: providerId, |
|
15
|
resolve: require.resolve("../../src"), |
|
16
|
is_default: true, |
|
17
|
options: { |
|
18
|
redisUrl: process.env.REDIS_URL ?? "redis://localhost:6379", |
|
19
|
}, |
|
20
|
}, |
|
21
|
], |
|
22
|
}, |
|
23
|
testSuite: ({ service }) => { |
|
24
|
describe("Locking Module Service", () => { |
|
25
|
let stock = 5 |
|
26
|
function replenishStock() { |
|
27
|
stock = 5 |
|
28
|
} |
|
29
|
function hasStock() { |
|
30
|
return stock > 0 |
|
31
|
} |
|
32
|
async function reduceStock() { |
|
33
|
await setTimeout(10) |
|
34
|
stock-- |
|
35
|
} |
|
36
|
async function buy() { |
|
37
|
if (hasStock()) { |
|
38
|
await reduceStock() |
|
39
|
return true |
|
40
|
} |
|
41
|
return false |
|
42
|
} |
|
43
|
|
|
44
|
beforeEach(async () => { |
|
45
|
await service.releaseAll() |
|
46
|
}) |
|
47
|
|
|
48
|
it("should execute functions respecting the key locked", async () => { |
|
49
|
// 10 parallel calls to buy should oversell the stock |
|
50
|
const prom: any[] = [] |
|
51
|
for (let i = 0; i < 10; i++) { |
|
52
|
prom.push(buy()) |
|
53
|
} |
|
54
|
await Promise.all(prom) |
|
55
|
expect(stock).toBe(-5) |
|
56
|
|
|
57
|
replenishStock() |
|
58
|
|
|
59
|
// 10 parallel calls to buy with lock should not oversell the stock |
|
60
|
const promWLock: any[] = [] |
|
61
|
for (let i = 0; i < 10; i++) { |
|
62
|
promWLock.push(service.execute("item_1", buy)) |
|
63
|
} |
|
64
|
await Promise.all(promWLock) |
|
65
|
|
|
66
|
expect(stock).toBe(0) |
|
67
|
}) |
|
68
|
|
|
69
|
it("should acquire lock and release it", async () => { |
|
70
|
await service.acquire("key_name", { |
|
71
|
ownerId: "user_id_123", |
|
72
|
}) |
|
73
|
|
|
74
|
const userReleased = await service.release("key_name", { |
|
75
|
ownerId: "user_id_456", |
|
76
|
}) |
|
77
|
const anotherUserLock = service.acquire("key_name", { |
|
78
|
ownerId: "user_id_456", |
|
79
|
}) |
|
80
|
|
|
81
|
expect(userReleased).toBe(false) |
|
82
|
await expect(anotherUserLock).rejects.toThrow( |
|
83
|
`Failed to acquire lock for key "key_name"` |
|
84
|
) |
|
85
|
|
|
86
|
const releasing = await service.release("key_name", { |
|
87
|
ownerId: "user_id_123", |
|
88
|
}) |
|
89
|
|
|
90
|
expect(releasing).toBe(true) |
|
91
|
}) |
|
92
|
|
|
93
|
it("should acquire lock and release it during parallel calls", async () => { |
|
94
|
const keyToLock = "mySpecialKey" |
|
95
|
const user_1 = { |
|
96
|
ownerId: "user_id_456", |
|
97
|
} |
|
98
|
const user_2 = { |
|
99
|
ownerId: "user_id_000", |
|
100
|
} |
|
101
|
|
|
102
|
await expect( |
|
103
|
service.acquire(keyToLock, user_1) |
|
104
|
).resolves.toBeUndefined() |
|
105
|
|
|
106
|
await expect( |
|
107
|
service.acquire(keyToLock, user_1) |
|
108
|
).resolves.toBeUndefined() |
|
109
|
|
|
110
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
111
|
`Failed to acquire lock for key "${keyToLock}"` |
|
112
|
) |
|
113
|
|
|
114
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
115
|
`Failed to acquire lock for key "${keyToLock}"` |
|
116
|
) |
|
117
|
|
|
118
|
await service.acquire(keyToLock, user_1) |
|
119
|
|
|
120
|
const releaseNotLocked = await service.release(keyToLock, { |
|
121
|
ownerId: "user_id_000", |
|
122
|
}) |
|
123
|
expect(releaseNotLocked).toBe(false) |
|
124
|
|
|
125
|
const release = await service.release(keyToLock, user_1) |
|
126
|
expect(release).toBe(true) |
|
127
|
}) |
|
128
|
|
|
129
|
it("should fail to acquire the same key when no owner is provided", async () => { |
|
130
|
const keyToLock = "mySpecialKey" |
|
131
|
|
|
132
|
const user_2 = { |
|
133
|
ownerId: "user_id_000", |
|
134
|
} |
|
135
|
|
|
136
|
await expect(service.acquire(keyToLock)).resolves.toBeUndefined() |
|
137
|
|
|
138
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
139
|
`Failed to acquire lock for key "${keyToLock}"` |
|
140
|
) |
|
141
|
|
|
142
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
143
|
`Failed to acquire lock for key "${keyToLock}"` |
|
144
|
) |
|
145
|
|
|
146
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
147
|
`Failed to acquire lock for key "${keyToLock}"` |
|
148
|
) |
|
149
|
|
|
150
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
151
|
`Failed to acquire lock for key "${keyToLock}"` |
|
152
|
) |
|
153
|
|
|
154
|
const releaseNotLocked = await service.release(keyToLock, { |
|
155
|
ownerId: "user_id_000", |
|
156
|
}) |
|
157
|
expect(releaseNotLocked).toBe(false) |
|
158
|
|
|
159
|
const release = await service.release(keyToLock) |
|
160
|
expect(release).toBe(true) |
|
161
|
}) |
|
162
|
}) |
|
163
|
|
|
164
|
it("should release lock in case of failure", async () => { |
|
165
|
const fn_1 = jest.fn(async () => { |
|
166
|
throw new Error("Error") |
|
167
|
}) |
|
168
|
const fn_2 = jest.fn(async () => {}) |
|
169
|
|
|
170
|
await service.execute("lock_key", fn_1).catch(() => {}) |
|
171
|
await service.execute("lock_key", fn_2).catch(() => {}) |
|
172
|
|
|
173
|
expect(fn_1).toHaveBeenCalledTimes(1) |
|
174
|
expect(fn_2).toHaveBeenCalledTimes(1) |
|
175
|
}) |
|
176
|
|
|
177
|
it("should release lock in case of timeout failure", async () => { |
|
178
|
const fn_1 = jest.fn(async () => { |
|
179
|
await setTimeout(1010) |
|
180
|
return "fn_1" |
|
181
|
}) |
|
182
|
|
|
183
|
const fn_2 = jest.fn(async () => { |
|
184
|
return "fn_2" |
|
185
|
}) |
|
186
|
|
|
187
|
const fn_3 = jest.fn(async () => { |
|
188
|
return "fn_3" |
|
189
|
}) |
|
190
|
|
|
191
|
const ops = [ |
|
192
|
service |
|
193
|
.execute("lock_key", fn_1, { |
|
194
|
timeout: 1, |
|
195
|
}) |
|
196
|
.catch((e) => e), |
|
197
|
|
|
198
|
service |
|
199
|
.execute("lock_key", fn_2, { |
|
200
|
timeout: 1, |
|
201
|
}) |
|
202
|
.catch((e) => e), |
|
203
|
|
|
204
|
service |
|
205
|
.execute("lock_key", fn_3, { |
|
206
|
timeout: 5, |
|
207
|
}) |
|
208
|
.catch((e) => e), |
|
209
|
] |
|
210
|
|
|
211
|
const res = await promiseAll(ops) |
|
212
|
|
|
213
|
expect(res).toEqual(["fn_1", Error("Timed-out acquiring lock."), "fn_3"]) |
|
214
|
|
|
215
|
expect(fn_1).toHaveBeenCalledTimes(1) |
|
216
|
expect(fn_2).toHaveBeenCalledTimes(0) |
|
217
|
expect(fn_3).toHaveBeenCalledTimes(1) |
|
218
|
}) |
|
219
|
|
|
220
|
it("should hold the keys until the job is done", async () => { |
|
221
|
const result = await service.execute("held_key", async () => { |
|
222
|
// No wait needed to see this: an acquisition that isn't queueing is |
|
223
|
// refused as soon as it finds the keys taken. |
|
224
|
await expect( |
|
225
|
service.acquire("held_key", { ownerId: "someone_else" }) |
|
226
|
).rejects.toThrow() |
|
227
|
|
|
228
|
return "done" |
|
229
|
}) |
|
230
|
|
|
231
|
expect(result).toEqual("done") |
|
232
|
|
|
233
|
// And the job being done is what frees them, under the same owner that |
|
234
|
// took them — a release under a different owner would leave them locked. |
|
235
|
await service.acquire("held_key", { ownerId: "someone_else" }) |
|
236
|
|
|
237
|
await expect( |
|
238
|
service.release("held_key", { ownerId: "someone_else" }) |
|
239
|
).resolves.toBe(true) |
|
240
|
}) |
|
241
|
|
|
242
|
it("should renew the lease of a job that outlives it", async () => { |
|
243
|
// One second is the shortest lease the provider allows, and the only way |
|
244
|
// to watch a lease being extended is to outlast it, so this test waits |
|
245
|
// out one. Everything else about renewal is covered by the unit tests. |
|
246
|
const result = await service.execute( |
|
247
|
"renewed_key", |
|
248
|
async () => { |
|
249
|
await setTimeout(1200) |
|
250
|
|
|
251
|
// Still exclusively held, well past the lease it was acquired with. |
|
252
|
await expect( |
|
253
|
service.acquire("renewed_key", { ownerId: "someone_else" }) |
|
254
|
).rejects.toThrow() |
|
255
|
|
|
256
|
return "done" |
|
257
|
}, |
|
258
|
{ expire: 1 } |
|
259
|
) |
|
260
|
|
|
261
|
expect(result).toEqual("done") |
|
262
|
}) |
|
263
|
}, |
|
264
|
}) |