|
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
|
const providerId = "locking-postgres" |
|
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
|
}, |
|
18
|
], |
|
19
|
}, |
|
20
|
testSuite: ({ service }) => { |
|
21
|
describe("Locking Module Service", () => { |
|
22
|
let stock = 5 |
|
23
|
function replenishStock() { |
|
24
|
stock = 5 |
|
25
|
} |
|
26
|
function hasStock() { |
|
27
|
return stock > 0 |
|
28
|
} |
|
29
|
async function reduceStock() { |
|
30
|
await setTimeout(10) |
|
31
|
stock-- |
|
32
|
} |
|
33
|
async function buy() { |
|
34
|
if (hasStock()) { |
|
35
|
await reduceStock() |
|
36
|
return true |
|
37
|
} |
|
38
|
return false |
|
39
|
} |
|
40
|
|
|
41
|
it("should execute functions respecting the key locked", async () => { |
|
42
|
// 10 parallel calls to buy should oversell the stock |
|
43
|
const prom: any[] = [] |
|
44
|
for (let i = 0; i < 10; i++) { |
|
45
|
prom.push(buy()) |
|
46
|
} |
|
47
|
await Promise.all(prom) |
|
48
|
expect(stock).toBe(-5) |
|
49
|
|
|
50
|
replenishStock() |
|
51
|
|
|
52
|
// 10 parallel calls to buy with lock should not oversell the stock |
|
53
|
const promWLock: any[] = [] |
|
54
|
for (let i = 0; i < 10; i++) { |
|
55
|
promWLock.push(service.execute("item_1", buy)) |
|
56
|
} |
|
57
|
await Promise.all(promWLock) |
|
58
|
|
|
59
|
expect(stock).toBe(0) |
|
60
|
}) |
|
61
|
|
|
62
|
it("should acquire lock and release it", async () => { |
|
63
|
await service.acquire("key_name", { |
|
64
|
ownerId: "user_id_123", |
|
65
|
}) |
|
66
|
|
|
67
|
const userReleased = await service.release("key_name", { |
|
68
|
ownerId: "user_id_456", |
|
69
|
}) |
|
70
|
const anotherUserLock = service.acquire("key_name", { |
|
71
|
ownerId: "user_id_456", |
|
72
|
}) |
|
73
|
|
|
74
|
expect(userReleased).toBe(false) |
|
75
|
await expect(anotherUserLock).rejects.toThrow( |
|
76
|
`Failed to acquire lock for key "key_name"` |
|
77
|
) |
|
78
|
|
|
79
|
const releasing = await service.release("key_name", { |
|
80
|
ownerId: "user_id_123", |
|
81
|
}) |
|
82
|
|
|
83
|
expect(releasing).toBe(true) |
|
84
|
}) |
|
85
|
|
|
86
|
it("should acquire lock and release it during parallel calls", async () => { |
|
87
|
const keyToLock = "mySpecialKey" |
|
88
|
const user_1 = { |
|
89
|
ownerId: "user_id_456", |
|
90
|
} |
|
91
|
const user_2 = { |
|
92
|
ownerId: "user_id_000", |
|
93
|
} |
|
94
|
|
|
95
|
await expect( |
|
96
|
service.acquire(keyToLock, user_1) |
|
97
|
).resolves.toBeUndefined() |
|
98
|
|
|
99
|
await expect( |
|
100
|
service.acquire(keyToLock, user_1) |
|
101
|
).resolves.toBeUndefined() |
|
102
|
|
|
103
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
104
|
`Failed to acquire lock for key "${keyToLock}"` |
|
105
|
) |
|
106
|
|
|
107
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
108
|
`Failed to acquire lock for key "${keyToLock}"` |
|
109
|
) |
|
110
|
|
|
111
|
await service.acquire(keyToLock, user_1) |
|
112
|
|
|
113
|
const releaseNotLocked = await service.release(keyToLock, { |
|
114
|
ownerId: "user_id_000", |
|
115
|
}) |
|
116
|
expect(releaseNotLocked).toBe(false) |
|
117
|
|
|
118
|
const release = await service.release(keyToLock, user_1) |
|
119
|
expect(release).toBe(true) |
|
120
|
}) |
|
121
|
|
|
122
|
it("should fail to acquire the same key when no owner is provided", async () => { |
|
123
|
const keyToLock = "mySpecialKey" |
|
124
|
|
|
125
|
const user_2 = { |
|
126
|
ownerId: "user_id_000", |
|
127
|
} |
|
128
|
|
|
129
|
await expect(service.acquire(keyToLock)).resolves.toBeUndefined() |
|
130
|
|
|
131
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
132
|
`Failed to acquire lock for key "${keyToLock}"` |
|
133
|
) |
|
134
|
|
|
135
|
await expect(service.acquire(keyToLock)).rejects.toThrow( |
|
136
|
`Failed to acquire lock for key "${keyToLock}"` |
|
137
|
) |
|
138
|
|
|
139
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
140
|
`Failed to acquire lock for key "${keyToLock}"` |
|
141
|
) |
|
142
|
|
|
143
|
await expect(service.acquire(keyToLock, user_2)).rejects.toThrow( |
|
144
|
`Failed to acquire lock for key "${keyToLock}"` |
|
145
|
) |
|
146
|
|
|
147
|
const releaseNotLocked = await service.release(keyToLock, { |
|
148
|
ownerId: "user_id_000", |
|
149
|
}) |
|
150
|
expect(releaseNotLocked).toBe(false) |
|
151
|
|
|
152
|
const release = await service.release(keyToLock) |
|
153
|
expect(release).toBe(true) |
|
154
|
}) |
|
155
|
}) |
|
156
|
|
|
157
|
it("should release lock in case of failure", async () => { |
|
158
|
const fn_1 = jest.fn(async () => { |
|
159
|
throw new Error("Error") |
|
160
|
}) |
|
161
|
const fn_2 = jest.fn(async () => {}) |
|
162
|
|
|
163
|
await service.execute("lock_key", fn_1).catch(() => {}) |
|
164
|
await service.execute("lock_key", fn_2).catch(() => {}) |
|
165
|
|
|
166
|
expect(fn_1).toHaveBeenCalledTimes(1) |
|
167
|
expect(fn_2).toHaveBeenCalledTimes(1) |
|
168
|
}) |
|
169
|
|
|
170
|
it.skip("should release lock in case of timeout failure", async () => { |
|
171
|
const fn_1 = jest.fn(async () => { |
|
172
|
await setTimeout(1010) |
|
173
|
return "fn_1" |
|
174
|
}) |
|
175
|
|
|
176
|
const fn_2 = jest.fn(async () => { |
|
177
|
return "fn_2" |
|
178
|
}) |
|
179
|
|
|
180
|
const fn_3 = jest.fn(async () => { |
|
181
|
return "fn_3" |
|
182
|
}) |
|
183
|
|
|
184
|
const ops = [ |
|
185
|
service |
|
186
|
.execute("lock_key", fn_1, { |
|
187
|
timeout: 1, |
|
188
|
}) |
|
189
|
.catch((e) => e), |
|
190
|
|
|
191
|
service |
|
192
|
.execute("lock_key", fn_2, { |
|
193
|
timeout: 1, |
|
194
|
}) |
|
195
|
.catch((e) => e), |
|
196
|
|
|
197
|
service |
|
198
|
.execute("lock_key", fn_3, { |
|
199
|
timeout: 2, |
|
200
|
}) |
|
201
|
.catch((e) => e), |
|
202
|
] |
|
203
|
|
|
204
|
const res = await promiseAll(ops) |
|
205
|
|
|
206
|
expect(res).toEqual(["fn_1", expect.any(Error), "fn_3"]) |
|
207
|
|
|
208
|
expect(fn_1).toHaveBeenCalledTimes(1) |
|
209
|
expect(fn_2).toHaveBeenCalledTimes(0) |
|
210
|
expect(fn_3).toHaveBeenCalledTimes(1) |
|
211
|
}) |
|
212
|
}, |
|
213
|
}) |