@daiso-tech/core 0.22.2 → 0.23.0
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.
- package/dist/cjs/lock/contracts/lock.errors.js +4 -4
- package/dist/cjs/lock/contracts/lock.errors.js.map +1 -1
- package/dist/cjs/lock/implementations/_shared/lock-adapter.test-suite.js +0 -129
- package/dist/cjs/lock/implementations/_shared/lock-adapter.test-suite.js.map +1 -1
- package/dist/cjs/lock/implementations/_shared/lock-provider.test-suite.js +487 -489
- package/dist/cjs/lock/implementations/_shared/lock-provider.test-suite.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +0 -16
- package/dist/cjs/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +5 -5
- package/dist/cjs/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +0 -13
- package/dist/cjs/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/derivables/lock-provider/database-lock-adapter.js +0 -32
- package/dist/cjs/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock-provider.js +22 -12
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock-state.js +58 -0
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -0
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock.js +32 -39
- package/dist/cjs/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/cjs/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +1 -1
- package/dist/cjs/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/cjs/utilities/contracts/_module.js +0 -1
- package/dist/cjs/utilities/contracts/_module.js.map +1 -1
- package/dist/esm/lock/contracts/lock.errors.js +2 -2
- package/dist/esm/lock/contracts/lock.errors.js.map +1 -1
- package/dist/esm/lock/implementations/_shared/lock-adapter.test-suite.js +0 -129
- package/dist/esm/lock/implementations/_shared/lock-adapter.test-suite.js.map +1 -1
- package/dist/esm/lock/implementations/_shared/lock-provider.test-suite.js +488 -490
- package/dist/esm/lock/implementations/_shared/lock-provider.test-suite.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +1 -1
- package/dist/esm/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js +1 -1
- package/dist/esm/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +1 -17
- package/dist/esm/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +6 -6
- package/dist/esm/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +1 -14
- package/dist/esm/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js +1 -1
- package/dist/esm/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/derivables/lock-provider/database-lock-adapter.js +1 -33
- package/dist/esm/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/esm/lock/implementations/derivables/lock-provider/lock-provider.js +22 -12
- package/dist/esm/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/esm/lock/implementations/derivables/lock-provider/lock-state.js +54 -0
- package/dist/esm/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -0
- package/dist/esm/lock/implementations/derivables/lock-provider/lock.js +33 -40
- package/dist/esm/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/esm/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +1 -1
- package/dist/esm/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/esm/utilities/contracts/_module.js +0 -1
- package/dist/esm/utilities/contracts/_module.js.map +1 -1
- package/dist/types/lock/contracts/database-lock-adapter.contract.d.ts +1 -2
- package/dist/types/lock/contracts/lock-adapter.contract.d.ts +0 -9
- package/dist/types/lock/contracts/lock.contract.d.ts +1 -1
- package/dist/types/lock/contracts/lock.errors.d.ts +2 -2
- package/dist/types/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +2 -2
- package/dist/types/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.d.ts +2 -2
- package/dist/types/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +1 -3
- package/dist/types/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +2 -1
- package/dist/types/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +1 -3
- package/dist/types/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.d.ts +2 -2
- package/dist/types/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +2 -7
- package/dist/types/lock/implementations/derivables/lock-provider/lock-provider.d.ts +8 -10
- package/dist/types/lock/implementations/derivables/lock-provider/lock-state.d.ts +40 -0
- package/dist/types/lock/implementations/derivables/lock-provider/lock.d.ts +8 -2
- package/dist/types/utilities/contracts/_module.d.ts +0 -1
- package/package.json +1 -1
- package/dist/cjs/utilities/contracts/serde-registrable.contract.js +0 -3
- package/dist/cjs/utilities/contracts/serde-registrable.contract.js.map +0 -1
- package/dist/esm/utilities/contracts/serde-registrable.contract.js +0 -1
- package/dist/esm/utilities/contracts/serde-registrable.contract.js.map +0 -1
- package/dist/types/utilities/contracts/serde-registrable.contract.d.ts +0 -11
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UnownedRefreshLockEvent, KeyForceReleasedLockEvent, KeyAlreadyAcquiredLockEvent, UnownedReleaseLockEvent, KeyReleasedLockEvent, KeyAcquiredLockEvent, KeyAlreadyAcquiredLockError,
|
|
1
|
+
import { UnownedRefreshLockEvent, KeyForceReleasedLockEvent, KeyAlreadyAcquiredLockEvent, UnownedReleaseLockEvent, KeyReleasedLockEvent, KeyAcquiredLockEvent, KeyAlreadyAcquiredLockError, UnownedRefreshLockError, UnownedReleaseLockError, KeyRefreshedLockEvent, } from "../../../lock/contracts/_module";
|
|
2
2
|
import { TimeSpan } from "../../../utilities/_module";
|
|
3
3
|
import { delay, LazyPromise } from "../../../async/_module";
|
|
4
4
|
import { NoOpSerde } from "../../../serde/implementations/_module";
|
|
@@ -13,538 +13,536 @@ export function lockProviderTestSuite(settings) {
|
|
|
13
13
|
});
|
|
14
14
|
const TTL = TimeSpan.fromMilliseconds(50);
|
|
15
15
|
describe("Api tests:", () => {
|
|
16
|
-
describe("
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
ttl,
|
|
23
|
-
});
|
|
24
|
-
const [result, error] = await lock.run(async () => {
|
|
25
|
-
await delay(TTL);
|
|
26
|
-
return "a";
|
|
27
|
-
});
|
|
28
|
-
expect(result).toBe("a");
|
|
29
|
-
expect(error).toBeNull();
|
|
30
|
-
});
|
|
31
|
-
test("Should return null when lock is already acquired", async () => {
|
|
32
|
-
const key = "a";
|
|
33
|
-
const ttl = null;
|
|
34
|
-
const lock = lockProviderA.create(key, {
|
|
35
|
-
ttl,
|
|
36
|
-
});
|
|
37
|
-
await lock.acquire();
|
|
38
|
-
const [result, error] = await lock.run(async () => {
|
|
39
|
-
await delay(TTL);
|
|
40
|
-
return "a";
|
|
41
|
-
});
|
|
42
|
-
expect(result).toBeNull();
|
|
43
|
-
expect(error).toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
16
|
+
describe("method: run", () => {
|
|
17
|
+
test("Should return string when lock is available", async () => {
|
|
18
|
+
const key = "a";
|
|
19
|
+
const ttl = null;
|
|
20
|
+
const lock = lockProviderA.create(key, {
|
|
21
|
+
ttl,
|
|
44
22
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const lock = lockProviderA.create(key, {
|
|
49
|
-
ttl,
|
|
50
|
-
});
|
|
51
|
-
const [result, error] = await lock.run(new LazyPromise(async () => {
|
|
52
|
-
await delay(TTL);
|
|
53
|
-
return "a";
|
|
54
|
-
}));
|
|
55
|
-
expect(result).toBe("a");
|
|
56
|
-
expect(error).toBeNull();
|
|
23
|
+
const [result, error] = await lock.run(async () => {
|
|
24
|
+
await delay(TTL);
|
|
25
|
+
return "a";
|
|
57
26
|
});
|
|
27
|
+
expect(result).toBe("a");
|
|
28
|
+
expect(error).toBeNull();
|
|
58
29
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
ttl,
|
|
65
|
-
});
|
|
66
|
-
const result = await lock.runOrFail(async () => {
|
|
67
|
-
await delay(TTL);
|
|
68
|
-
return "a";
|
|
69
|
-
});
|
|
70
|
-
expect(result).toBe("a");
|
|
71
|
-
});
|
|
72
|
-
test("Should throw KeyAlreadyAcquiredLockError when lock is already acquired", async () => {
|
|
73
|
-
const key = "a";
|
|
74
|
-
const ttl = null;
|
|
75
|
-
const lock = lockProviderA.create(key, {
|
|
76
|
-
ttl,
|
|
77
|
-
});
|
|
78
|
-
await lock.acquire();
|
|
79
|
-
const result = lock.runOrFail(async () => {
|
|
80
|
-
await delay(TTL);
|
|
81
|
-
return "a";
|
|
82
|
-
});
|
|
83
|
-
await expect(result).rejects.toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
30
|
+
test("Should return null when lock is already acquired", async () => {
|
|
31
|
+
const key = "a";
|
|
32
|
+
const ttl = null;
|
|
33
|
+
const lock = lockProviderA.create(key, {
|
|
34
|
+
ttl,
|
|
84
35
|
});
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
ttl,
|
|
90
|
-
});
|
|
91
|
-
const result = await lock.runOrFail(new LazyPromise(async () => {
|
|
92
|
-
await delay(TTL);
|
|
93
|
-
return "a";
|
|
94
|
-
}));
|
|
95
|
-
expect(result).toBe("a");
|
|
36
|
+
await lock.acquire();
|
|
37
|
+
const [result, error] = await lock.run(async () => {
|
|
38
|
+
await delay(TTL);
|
|
39
|
+
return "a";
|
|
96
40
|
});
|
|
41
|
+
expect(result).toBeNull();
|
|
42
|
+
expect(error).toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
97
43
|
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
ttl,
|
|
104
|
-
});
|
|
105
|
-
const result = await lock.acquire();
|
|
106
|
-
expect(result).toBe(true);
|
|
107
|
-
});
|
|
108
|
-
test("Should return false when lock is already acquired", async () => {
|
|
109
|
-
const key = "a";
|
|
110
|
-
const ttl = null;
|
|
111
|
-
const lock = lockProviderA.create(key, {
|
|
112
|
-
ttl,
|
|
113
|
-
});
|
|
114
|
-
await lock.acquire();
|
|
115
|
-
const result = await lock.acquire();
|
|
116
|
-
expect(result).toBe(false);
|
|
117
|
-
});
|
|
118
|
-
test("Should not be expired when released by same owner", async () => {
|
|
119
|
-
const key = "a";
|
|
120
|
-
const ttl = null;
|
|
121
|
-
const owner = "b";
|
|
122
|
-
const lock = lockProviderA.create(key, {
|
|
123
|
-
ttl,
|
|
124
|
-
owner,
|
|
125
|
-
});
|
|
126
|
-
await lock.acquire();
|
|
127
|
-
const result = await lock.isExpired();
|
|
128
|
-
expect(result).toBe(false);
|
|
129
|
-
});
|
|
130
|
-
test("Should be loked when released by same owner", async () => {
|
|
131
|
-
const key = "a";
|
|
132
|
-
const ttl = null;
|
|
133
|
-
const owner = "b";
|
|
134
|
-
const lock = lockProviderA.create(key, {
|
|
135
|
-
ttl,
|
|
136
|
-
owner,
|
|
137
|
-
});
|
|
138
|
-
await lock.acquire();
|
|
139
|
-
const result = await lock.isLocked();
|
|
140
|
-
expect(result).toBe(true);
|
|
44
|
+
test("Should work with LazyPromise", async () => {
|
|
45
|
+
const key = "a";
|
|
46
|
+
const ttl = null;
|
|
47
|
+
const lock = lockProviderA.create(key, {
|
|
48
|
+
ttl,
|
|
141
49
|
});
|
|
50
|
+
const [result, error] = await lock.run(new LazyPromise(async () => {
|
|
51
|
+
await delay(TTL);
|
|
52
|
+
return "a";
|
|
53
|
+
}));
|
|
54
|
+
expect(result).toBe("a");
|
|
55
|
+
expect(error).toBeNull();
|
|
142
56
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const result = lock.acquireOrFail();
|
|
151
|
-
await expect(result).resolves.toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
describe("method: runOrFail", () => {
|
|
59
|
+
test("Should return string when lock is available", async () => {
|
|
60
|
+
const key = "a";
|
|
61
|
+
const ttl = null;
|
|
62
|
+
const lock = lockProviderA.create(key, {
|
|
63
|
+
ttl,
|
|
152
64
|
});
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const lock = lockProviderA.create(key, {
|
|
157
|
-
ttl,
|
|
158
|
-
});
|
|
159
|
-
await lock.acquireOrFail();
|
|
160
|
-
const result = lock.acquireOrFail();
|
|
161
|
-
await expect(result).rejects.toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
65
|
+
const result = await lock.runOrFail(async () => {
|
|
66
|
+
await delay(TTL);
|
|
67
|
+
return "a";
|
|
162
68
|
});
|
|
69
|
+
expect(result).toBe("a");
|
|
163
70
|
});
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const lock = lockProviderA.create(key, {
|
|
170
|
-
ttl,
|
|
171
|
-
owner,
|
|
172
|
-
});
|
|
173
|
-
await lock.acquire();
|
|
174
|
-
const result = await lock.release();
|
|
175
|
-
expect(result).toBe(true);
|
|
176
|
-
});
|
|
177
|
-
test("Should return false when released by different owner", async () => {
|
|
178
|
-
const key = "a";
|
|
179
|
-
const ttl = null;
|
|
180
|
-
const owner1 = "b";
|
|
181
|
-
const lock1 = lockProviderA.create(key, {
|
|
182
|
-
ttl,
|
|
183
|
-
owner: owner1,
|
|
184
|
-
});
|
|
185
|
-
await lock1.acquire();
|
|
186
|
-
const owner2 = "c";
|
|
187
|
-
const lock2 = lockProviderA.create(key, {
|
|
188
|
-
ttl,
|
|
189
|
-
owner: owner2,
|
|
190
|
-
});
|
|
191
|
-
const result = await lock2.release();
|
|
192
|
-
expect(result).toBe(false);
|
|
193
|
-
});
|
|
194
|
-
test("Should be expired when released by same owner", async () => {
|
|
195
|
-
const key = "a";
|
|
196
|
-
const ttl = null;
|
|
197
|
-
const owner = "b";
|
|
198
|
-
const lock = lockProviderA.create(key, {
|
|
199
|
-
ttl,
|
|
200
|
-
owner,
|
|
201
|
-
});
|
|
202
|
-
await lock.acquire();
|
|
203
|
-
await lock.release();
|
|
204
|
-
const result = await lock.isExpired();
|
|
205
|
-
expect(result).toBe(true);
|
|
71
|
+
test("Should throw KeyAlreadyAcquiredLockError when lock is already acquired", async () => {
|
|
72
|
+
const key = "a";
|
|
73
|
+
const ttl = null;
|
|
74
|
+
const lock = lockProviderA.create(key, {
|
|
75
|
+
ttl,
|
|
206
76
|
});
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const lock = lockProviderA.create(key, {
|
|
212
|
-
ttl,
|
|
213
|
-
owner,
|
|
214
|
-
});
|
|
215
|
-
await lock.acquire();
|
|
216
|
-
await lock.release();
|
|
217
|
-
const result = await lock.isLocked();
|
|
218
|
-
expect(result).toBe(false);
|
|
77
|
+
await lock.acquire();
|
|
78
|
+
const result = lock.runOrFail(async () => {
|
|
79
|
+
await delay(TTL);
|
|
80
|
+
return "a";
|
|
219
81
|
});
|
|
82
|
+
await expect(result).rejects.toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
220
83
|
});
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const lock = lockProviderA.create(key, {
|
|
227
|
-
ttl,
|
|
228
|
-
owner,
|
|
229
|
-
});
|
|
230
|
-
await lock.acquire();
|
|
231
|
-
const result = lock.releaseOrFail();
|
|
232
|
-
await expect(result).resolves.toBeUndefined();
|
|
233
|
-
});
|
|
234
|
-
test("Should throw UnownedReleaseLockError when released by different owner", async () => {
|
|
235
|
-
const key = "a";
|
|
236
|
-
const ttl = null;
|
|
237
|
-
const owner1 = "b";
|
|
238
|
-
const lock1 = lockProviderA.create(key, {
|
|
239
|
-
ttl,
|
|
240
|
-
owner: owner1,
|
|
241
|
-
});
|
|
242
|
-
await lock1.acquire();
|
|
243
|
-
const owner2 = "c";
|
|
244
|
-
const lock2 = lockProviderA.create(key, {
|
|
245
|
-
ttl,
|
|
246
|
-
owner: owner2,
|
|
247
|
-
});
|
|
248
|
-
const result = lock2.releaseOrFail();
|
|
249
|
-
await expect(result).rejects.toBeInstanceOf(UnownedReleaseLockError);
|
|
250
|
-
});
|
|
251
|
-
test("Should be expired when released by same owner", async () => {
|
|
252
|
-
const key = "a";
|
|
253
|
-
const ttl = null;
|
|
254
|
-
const owner = "b";
|
|
255
|
-
const lock = lockProviderA.create(key, {
|
|
256
|
-
ttl,
|
|
257
|
-
owner,
|
|
258
|
-
});
|
|
259
|
-
await lock.acquire();
|
|
260
|
-
await lock.releaseOrFail();
|
|
261
|
-
const result = await lock.isExpired();
|
|
262
|
-
expect(result).toBe(true);
|
|
84
|
+
test("Should work with LazyPromise", async () => {
|
|
85
|
+
const key = "a";
|
|
86
|
+
const ttl = null;
|
|
87
|
+
const lock = lockProviderA.create(key, {
|
|
88
|
+
ttl,
|
|
263
89
|
});
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
90
|
+
const result = await lock.runOrFail(new LazyPromise(async () => {
|
|
91
|
+
await delay(TTL);
|
|
92
|
+
return "a";
|
|
93
|
+
}));
|
|
94
|
+
expect(result).toBe("a");
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
describe("method: acquire", () => {
|
|
98
|
+
test("Should return true when lock is available", async () => {
|
|
99
|
+
const key = "a";
|
|
100
|
+
const ttl = null;
|
|
101
|
+
const lock = lockProviderA.create(key, {
|
|
102
|
+
ttl,
|
|
276
103
|
});
|
|
104
|
+
const result = await lock.acquire();
|
|
105
|
+
expect(result).toBe(true);
|
|
277
106
|
});
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const lock1 = lockProviderA.create(key, {
|
|
284
|
-
ttl,
|
|
285
|
-
owner: owner1,
|
|
286
|
-
});
|
|
287
|
-
await lock1.acquire();
|
|
288
|
-
const owner2 = "c";
|
|
289
|
-
const lock2 = lockProviderA.create(key, {
|
|
290
|
-
ttl,
|
|
291
|
-
owner: owner2,
|
|
292
|
-
});
|
|
293
|
-
await lock2.forceRelease();
|
|
294
|
-
const result = await lock1.acquire();
|
|
295
|
-
expect(result).toBe(true);
|
|
107
|
+
test("Should return false when lock is already acquired", async () => {
|
|
108
|
+
const key = "a";
|
|
109
|
+
const ttl = null;
|
|
110
|
+
const lock = lockProviderA.create(key, {
|
|
111
|
+
ttl,
|
|
296
112
|
});
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
expect(result).toBe(true);
|
|
113
|
+
await lock.acquire();
|
|
114
|
+
const result = await lock.acquire();
|
|
115
|
+
expect(result).toBe(false);
|
|
116
|
+
});
|
|
117
|
+
test("Should not be expired when released by same owner", async () => {
|
|
118
|
+
const key = "a";
|
|
119
|
+
const ttl = null;
|
|
120
|
+
const owner = "b";
|
|
121
|
+
const lock = lockProviderA.create(key, {
|
|
122
|
+
ttl,
|
|
123
|
+
owner,
|
|
309
124
|
});
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
expect(result).toBe(false);
|
|
125
|
+
await lock.acquire();
|
|
126
|
+
const result = await lock.isExpired();
|
|
127
|
+
expect(result).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
test("Should be loked when released by same owner", async () => {
|
|
130
|
+
const key = "a";
|
|
131
|
+
const ttl = null;
|
|
132
|
+
const owner = "b";
|
|
133
|
+
const lock = lockProviderA.create(key, {
|
|
134
|
+
ttl,
|
|
135
|
+
owner,
|
|
322
136
|
});
|
|
137
|
+
await lock.acquire();
|
|
138
|
+
const result = await lock.isLocked();
|
|
139
|
+
expect(result).toBe(true);
|
|
323
140
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
owner,
|
|
332
|
-
});
|
|
333
|
-
await lock.acquire();
|
|
334
|
-
const result = await lock.isExpired();
|
|
335
|
-
expect(result).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
describe("method: acquireOrFail", () => {
|
|
143
|
+
test("Should not throw KeyAlreadyAcquiredLockError when lock is available", async () => {
|
|
144
|
+
const key = "a";
|
|
145
|
+
const ttl = null;
|
|
146
|
+
const lock = lockProviderA.create(key, {
|
|
147
|
+
ttl,
|
|
336
148
|
});
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
await lock.acquire();
|
|
346
|
-
const result = await lock.isExpired();
|
|
347
|
-
expect(result).toBe(false);
|
|
149
|
+
const result = lock.acquireOrFail();
|
|
150
|
+
await expect(result).resolves.toBeUndefined();
|
|
151
|
+
});
|
|
152
|
+
test("Should throw KeyAlreadyAcquiredLockError when lock is already acquired", async () => {
|
|
153
|
+
const key = "a";
|
|
154
|
+
const ttl = null;
|
|
155
|
+
const lock = lockProviderA.create(key, {
|
|
156
|
+
ttl,
|
|
348
157
|
});
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
158
|
+
await lock.acquireOrFail();
|
|
159
|
+
const result = lock.acquireOrFail();
|
|
160
|
+
await expect(result).rejects.toBeInstanceOf(KeyAlreadyAcquiredLockError);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
describe("method: release", () => {
|
|
164
|
+
test("Should return true when released by same owner", async () => {
|
|
165
|
+
const key = "a";
|
|
166
|
+
const ttl = null;
|
|
167
|
+
const owner = "b";
|
|
168
|
+
const lock = lockProviderA.create(key, {
|
|
169
|
+
ttl,
|
|
170
|
+
owner,
|
|
361
171
|
});
|
|
172
|
+
await lock.acquire();
|
|
173
|
+
const result = await lock.release();
|
|
174
|
+
expect(result).toBe(true);
|
|
362
175
|
});
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
176
|
+
test("Should return false when released by different owner", async () => {
|
|
177
|
+
const key = "a";
|
|
178
|
+
const ttl = null;
|
|
179
|
+
const owner1 = "b";
|
|
180
|
+
const lock1 = lockProviderA.create(key, {
|
|
181
|
+
ttl,
|
|
182
|
+
owner: owner1,
|
|
183
|
+
});
|
|
184
|
+
await lock1.acquire();
|
|
185
|
+
const owner2 = "c";
|
|
186
|
+
const lock2 = lockProviderA.create(key, {
|
|
187
|
+
ttl,
|
|
188
|
+
owner: owner2,
|
|
189
|
+
});
|
|
190
|
+
const result = await lock2.release();
|
|
191
|
+
expect(result).toBe(false);
|
|
192
|
+
});
|
|
193
|
+
test("Should be expired when released by same owner", async () => {
|
|
194
|
+
const key = "a";
|
|
195
|
+
const ttl = null;
|
|
196
|
+
const owner = "b";
|
|
197
|
+
const lock = lockProviderA.create(key, {
|
|
198
|
+
ttl,
|
|
199
|
+
owner,
|
|
200
|
+
});
|
|
201
|
+
await lock.acquire();
|
|
202
|
+
await lock.release();
|
|
203
|
+
const result = await lock.isExpired();
|
|
204
|
+
expect(result).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
test("Should be not loked when released by same owner", async () => {
|
|
207
|
+
const key = "a";
|
|
208
|
+
const ttl = null;
|
|
209
|
+
const owner = "b";
|
|
210
|
+
const lock = lockProviderA.create(key, {
|
|
211
|
+
ttl,
|
|
212
|
+
owner,
|
|
375
213
|
});
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
214
|
+
await lock.acquire();
|
|
215
|
+
await lock.release();
|
|
216
|
+
const result = await lock.isLocked();
|
|
217
|
+
expect(result).toBe(false);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
describe("method: releaseOrFail", () => {
|
|
221
|
+
test("Should not throw UnownedReleaseLockError when released by same owner", async () => {
|
|
222
|
+
const key = "a";
|
|
223
|
+
const ttl = null;
|
|
224
|
+
const owner = "b";
|
|
225
|
+
const lock = lockProviderA.create(key, {
|
|
226
|
+
ttl,
|
|
227
|
+
owner,
|
|
387
228
|
});
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
229
|
+
await lock.acquire();
|
|
230
|
+
const result = lock.releaseOrFail();
|
|
231
|
+
await expect(result).resolves.toBeUndefined();
|
|
232
|
+
});
|
|
233
|
+
test("Should throw UnownedReleaseLockError when released by different owner", async () => {
|
|
234
|
+
const key = "a";
|
|
235
|
+
const ttl = null;
|
|
236
|
+
const owner1 = "b";
|
|
237
|
+
const lock1 = lockProviderA.create(key, {
|
|
238
|
+
ttl,
|
|
239
|
+
owner: owner1,
|
|
240
|
+
});
|
|
241
|
+
await lock1.acquire();
|
|
242
|
+
const owner2 = "c";
|
|
243
|
+
const lock2 = lockProviderA.create(key, {
|
|
244
|
+
ttl,
|
|
245
|
+
owner: owner2,
|
|
246
|
+
});
|
|
247
|
+
const result = lock2.releaseOrFail();
|
|
248
|
+
await expect(result).rejects.toBeInstanceOf(UnownedReleaseLockError);
|
|
249
|
+
});
|
|
250
|
+
test("Should be expired when released by same owner", async () => {
|
|
251
|
+
const key = "a";
|
|
252
|
+
const ttl = null;
|
|
253
|
+
const owner = "b";
|
|
254
|
+
const lock = lockProviderA.create(key, {
|
|
255
|
+
ttl,
|
|
256
|
+
owner,
|
|
257
|
+
});
|
|
258
|
+
await lock.acquire();
|
|
259
|
+
await lock.releaseOrFail();
|
|
260
|
+
const result = await lock.isExpired();
|
|
261
|
+
expect(result).toBe(true);
|
|
262
|
+
});
|
|
263
|
+
test("Should be not loked when released by same owner", async () => {
|
|
264
|
+
const key = "a";
|
|
265
|
+
const ttl = null;
|
|
266
|
+
const owner = "b";
|
|
267
|
+
const lock = lockProviderA.create(key, {
|
|
268
|
+
ttl,
|
|
269
|
+
owner,
|
|
400
270
|
});
|
|
271
|
+
await lock.acquire();
|
|
272
|
+
await lock.releaseOrFail();
|
|
273
|
+
const result = await lock.isLocked();
|
|
274
|
+
expect(result).toBe(false);
|
|
401
275
|
});
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
276
|
+
});
|
|
277
|
+
describe("method: forceRelease", () => {
|
|
278
|
+
test("Should release lock no regardless of the owner", async () => {
|
|
279
|
+
const key = "a";
|
|
280
|
+
const ttl = null;
|
|
281
|
+
const owner1 = "b";
|
|
282
|
+
const lock1 = lockProviderA.create(key, {
|
|
283
|
+
ttl,
|
|
284
|
+
owner: owner1,
|
|
285
|
+
});
|
|
286
|
+
await lock1.acquire();
|
|
287
|
+
const owner2 = "c";
|
|
288
|
+
const lock2 = lockProviderA.create(key, {
|
|
289
|
+
ttl,
|
|
290
|
+
owner: owner2,
|
|
291
|
+
});
|
|
292
|
+
await lock2.forceRelease();
|
|
293
|
+
const result = await lock1.acquire();
|
|
294
|
+
expect(result).toBe(true);
|
|
295
|
+
});
|
|
296
|
+
test("Should be expired when released", async () => {
|
|
297
|
+
const key = "a";
|
|
298
|
+
const ttl = null;
|
|
299
|
+
const owner = "b";
|
|
300
|
+
const lock = lockProviderA.create(key, {
|
|
301
|
+
ttl,
|
|
302
|
+
owner,
|
|
303
|
+
});
|
|
304
|
+
await lock.acquire();
|
|
305
|
+
await lock.forceRelease();
|
|
306
|
+
const result = await lock.isExpired();
|
|
307
|
+
expect(result).toBe(true);
|
|
308
|
+
});
|
|
309
|
+
test("Should be not loked when released", async () => {
|
|
310
|
+
const key = "a";
|
|
311
|
+
const ttl = null;
|
|
312
|
+
const owner = "b";
|
|
313
|
+
const lock = lockProviderA.create(key, {
|
|
314
|
+
ttl,
|
|
315
|
+
owner,
|
|
415
316
|
});
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const result = await lock2.refresh();
|
|
431
|
-
expect(result).toBe(false);
|
|
317
|
+
await lock.acquire();
|
|
318
|
+
await lock.forceRelease();
|
|
319
|
+
const result = await lock.isLocked();
|
|
320
|
+
expect(result).toBe(false);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
describe("method: isExpired", () => {
|
|
324
|
+
test("Should return false when lock has no expiration", async () => {
|
|
325
|
+
const key = "a";
|
|
326
|
+
const ttl = null;
|
|
327
|
+
const owner = "b";
|
|
328
|
+
const lock = lockProviderA.create(key, {
|
|
329
|
+
ttl,
|
|
330
|
+
owner,
|
|
432
331
|
});
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
const time = await lock.getRemainingTime();
|
|
445
|
-
expect(time?.toMilliseconds()).toBeGreaterThan(0);
|
|
332
|
+
await lock.acquire();
|
|
333
|
+
const result = await lock.isExpired();
|
|
334
|
+
expect(result).toBe(false);
|
|
335
|
+
});
|
|
336
|
+
test("Should return false when lock has not expired", async () => {
|
|
337
|
+
const key = "a";
|
|
338
|
+
const owner = "b";
|
|
339
|
+
const ttl = TTL;
|
|
340
|
+
const lock = lockProviderA.create(key, {
|
|
341
|
+
ttl,
|
|
342
|
+
owner,
|
|
446
343
|
});
|
|
344
|
+
await lock.acquire();
|
|
345
|
+
const result = await lock.isExpired();
|
|
346
|
+
expect(result).toBe(false);
|
|
447
347
|
});
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
owner,
|
|
456
|
-
});
|
|
457
|
-
await lock.acquire();
|
|
458
|
-
await delay(ttl);
|
|
459
|
-
const result = lock.refreshOrFail();
|
|
460
|
-
await expect(result).resolves.toBeUndefined();
|
|
348
|
+
test("Should return true when lock has expired", async () => {
|
|
349
|
+
const key = "a";
|
|
350
|
+
const owner = "b";
|
|
351
|
+
const ttl = TTL;
|
|
352
|
+
const lock = lockProviderA.create(key, {
|
|
353
|
+
ttl,
|
|
354
|
+
owner,
|
|
461
355
|
});
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const result = lock2.refreshOrFail();
|
|
477
|
-
await expect(result).rejects.toBeInstanceOf(UnownedExtendLockError);
|
|
356
|
+
await lock.acquire();
|
|
357
|
+
await delay(ttl.addMilliseconds(25));
|
|
358
|
+
const result = await lock.isExpired();
|
|
359
|
+
expect(result).toBe(true);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
describe("method: isLocked", () => {
|
|
363
|
+
test("Should return true when lock has no expiration", async () => {
|
|
364
|
+
const key = "a";
|
|
365
|
+
const ttl = null;
|
|
366
|
+
const owner = "b";
|
|
367
|
+
const lock = lockProviderA.create(key, {
|
|
368
|
+
ttl,
|
|
369
|
+
owner,
|
|
478
370
|
});
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const time = await lock.getRemainingTime();
|
|
491
|
-
expect(time?.toMilliseconds()).toBeGreaterThan(0);
|
|
371
|
+
await lock.acquire();
|
|
372
|
+
const result = await lock.isLocked();
|
|
373
|
+
expect(result).toBe(true);
|
|
374
|
+
});
|
|
375
|
+
test("Should return true when lock has not expired", async () => {
|
|
376
|
+
const key = "a";
|
|
377
|
+
const owner = "b";
|
|
378
|
+
const ttl = TTL;
|
|
379
|
+
const lock = lockProviderA.create(key, {
|
|
380
|
+
ttl,
|
|
381
|
+
owner,
|
|
492
382
|
});
|
|
383
|
+
await lock.acquire();
|
|
384
|
+
const result = await lock.isLocked();
|
|
385
|
+
expect(result).toBe(true);
|
|
493
386
|
});
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
owner,
|
|
502
|
-
});
|
|
503
|
-
const result = await lock.getRemainingTime();
|
|
504
|
-
expect(result).toBeNull();
|
|
387
|
+
test("Should return false when lock has expired", async () => {
|
|
388
|
+
const key = "a";
|
|
389
|
+
const owner = "b";
|
|
390
|
+
const ttl = TTL;
|
|
391
|
+
const lock = lockProviderA.create(key, {
|
|
392
|
+
ttl,
|
|
393
|
+
owner,
|
|
505
394
|
});
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
395
|
+
await lock.acquire();
|
|
396
|
+
await delay(ttl.addMilliseconds(25));
|
|
397
|
+
const result = await lock.isLocked();
|
|
398
|
+
expect(result).toBe(false);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
describe("method: refresh", () => {
|
|
402
|
+
test("Should return true when refreshed by same owner", async () => {
|
|
403
|
+
const key = "a";
|
|
404
|
+
const ttl = TTL;
|
|
405
|
+
const owner = "b";
|
|
406
|
+
const lock = lockProviderA.create(key, {
|
|
407
|
+
ttl,
|
|
408
|
+
owner,
|
|
409
|
+
});
|
|
410
|
+
await lock.acquire();
|
|
411
|
+
await delay(ttl);
|
|
412
|
+
const result = await lock.refresh();
|
|
413
|
+
expect(result).toBe(true);
|
|
414
|
+
});
|
|
415
|
+
test("Should return false when refreshed by different owner", async () => {
|
|
416
|
+
const key = "a";
|
|
417
|
+
const ttl = TTL;
|
|
418
|
+
const owner1 = "b";
|
|
419
|
+
const lock1 = lockProviderA.create(key, {
|
|
420
|
+
ttl,
|
|
421
|
+
owner: owner1,
|
|
422
|
+
});
|
|
423
|
+
await lock1.acquire();
|
|
424
|
+
const owner2 = "c";
|
|
425
|
+
const lock2 = lockProviderA.create(key, {
|
|
426
|
+
ttl,
|
|
427
|
+
owner: owner2,
|
|
428
|
+
});
|
|
429
|
+
const result = await lock2.refresh();
|
|
430
|
+
expect(result).toBe(false);
|
|
431
|
+
});
|
|
432
|
+
test("Should refresh expiration by same owner", async () => {
|
|
433
|
+
const key = "a";
|
|
434
|
+
const ttl = TTL;
|
|
435
|
+
const owner = "b";
|
|
436
|
+
const lock = lockProviderA.create(key, {
|
|
437
|
+
ttl,
|
|
438
|
+
owner,
|
|
439
|
+
});
|
|
440
|
+
await lock.acquire();
|
|
441
|
+
await delay(ttl.subtractMilliseconds(10));
|
|
442
|
+
await lock.refresh();
|
|
443
|
+
const time = await lock.getRemainingTime();
|
|
444
|
+
expect(time?.toMilliseconds()).toBeGreaterThan(0);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
describe("method: refreshOrFail", () => {
|
|
448
|
+
test("Should not throw UnownedRefreshLockError when refreshed by same owner", async () => {
|
|
449
|
+
const key = "a";
|
|
450
|
+
const ttl = TTL;
|
|
451
|
+
const owner = "b";
|
|
452
|
+
const lock = lockProviderA.create(key, {
|
|
453
|
+
ttl,
|
|
454
|
+
owner,
|
|
455
|
+
});
|
|
456
|
+
await lock.acquire();
|
|
457
|
+
await delay(ttl);
|
|
458
|
+
const result = lock.refreshOrFail();
|
|
459
|
+
await expect(result).resolves.toBeUndefined();
|
|
460
|
+
});
|
|
461
|
+
test("Should throw UnownedRefreshLockError when refreshed by different owner", async () => {
|
|
462
|
+
const key = "a";
|
|
463
|
+
const ttl = TTL;
|
|
464
|
+
const owner1 = "b";
|
|
465
|
+
const lock1 = lockProviderA.create(key, {
|
|
466
|
+
ttl,
|
|
467
|
+
owner: owner1,
|
|
468
|
+
});
|
|
469
|
+
await lock1.acquire();
|
|
470
|
+
const owner2 = "c";
|
|
471
|
+
const lock2 = lockProviderA.create(key, {
|
|
472
|
+
ttl,
|
|
473
|
+
owner: owner2,
|
|
474
|
+
});
|
|
475
|
+
const result = lock2.refreshOrFail();
|
|
476
|
+
await expect(result).rejects.toBeInstanceOf(UnownedRefreshLockError);
|
|
477
|
+
});
|
|
478
|
+
test("Should refresh expiration by same owner", async () => {
|
|
479
|
+
const key = "a";
|
|
480
|
+
const ttl = TTL;
|
|
481
|
+
const owner = "b";
|
|
482
|
+
const lock = lockProviderA.create(key, {
|
|
483
|
+
ttl,
|
|
484
|
+
owner,
|
|
485
|
+
});
|
|
486
|
+
await lock.acquireOrFail();
|
|
487
|
+
await delay(ttl.subtractMilliseconds(10));
|
|
488
|
+
await lock.refresh();
|
|
489
|
+
const time = await lock.getRemainingTime();
|
|
490
|
+
expect(time?.toMilliseconds()).toBeGreaterThan(0);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
describe("method: getRemainingTime", () => {
|
|
494
|
+
test("Should return null when lock is not acquired", async () => {
|
|
495
|
+
const key = "a";
|
|
496
|
+
const ttl = null;
|
|
497
|
+
const owner = "b";
|
|
498
|
+
const lock = lockProviderA.create(key, {
|
|
499
|
+
ttl,
|
|
500
|
+
owner,
|
|
517
501
|
});
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
expect(result?.toMilliseconds()).toBe(ttl.toMilliseconds());
|
|
502
|
+
const result = await lock.getRemainingTime();
|
|
503
|
+
expect(result).toBeNull();
|
|
504
|
+
});
|
|
505
|
+
test("Should return null when lock is acquired and has expiration", async () => {
|
|
506
|
+
const key = "a";
|
|
507
|
+
const ttl = null;
|
|
508
|
+
const owner = "b";
|
|
509
|
+
const lock = lockProviderA.create(key, {
|
|
510
|
+
ttl,
|
|
511
|
+
owner,
|
|
529
512
|
});
|
|
513
|
+
await lock.acquire();
|
|
514
|
+
const result = await lock.getRemainingTime();
|
|
515
|
+
expect(result).toBeNull();
|
|
530
516
|
});
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
const result = await lock.getOwner();
|
|
539
|
-
expect(result).toBe(owner);
|
|
517
|
+
test("Should return remaining time when lock is not acquired", async () => {
|
|
518
|
+
const key = "a";
|
|
519
|
+
const ttl = TTL;
|
|
520
|
+
const owner = "b";
|
|
521
|
+
const lock = lockProviderA.create(key, {
|
|
522
|
+
ttl,
|
|
523
|
+
owner,
|
|
540
524
|
});
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
525
|
+
await lock.acquire();
|
|
526
|
+
const result = await lock.getRemainingTime();
|
|
527
|
+
expect(result?.toMilliseconds()).toBe(ttl.toMilliseconds());
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
describe("method: getOwner", () => {
|
|
531
|
+
test("Should return the owner", async () => {
|
|
532
|
+
const key = "a";
|
|
533
|
+
const owner = "b";
|
|
534
|
+
const lock = lockProviderA.create(key, {
|
|
535
|
+
owner,
|
|
547
536
|
});
|
|
537
|
+
const result = await lock.getOwner();
|
|
538
|
+
expect(result).toBe(owner);
|
|
539
|
+
});
|
|
540
|
+
test("Should return the auto generated owner", async () => {
|
|
541
|
+
const key = "a";
|
|
542
|
+
const lock = lockProviderA.create(key);
|
|
543
|
+
const result = await lock.getOwner();
|
|
544
|
+
expect(result).toBeDefined();
|
|
545
|
+
expect(typeof result).toBe("string");
|
|
548
546
|
});
|
|
549
547
|
});
|
|
550
548
|
});
|