-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlocking.test.ts
More file actions
48 lines (39 loc) · 1.44 KB
/
locking.test.ts
File metadata and controls
48 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { createRedisClient } from "@internal/redis";
import { redisTest } from "@internal/testcontainers";
import { expect } from "vitest";
import { RunLocker } from "./locking.js";
describe("RunLocker", () => {
redisTest("Test acquiring a lock works", { timeout: 15_000 }, async ({ redisOptions }) => {
const redis = createRedisClient(redisOptions);
try {
const runLock = new RunLocker({ redis });
expect(runLock.isInsideLock()).toBe(false);
await runLock.lock(["test-1"], 5000, async (signal) => {
expect(signal).toBeDefined();
expect(runLock.isInsideLock()).toBe(true);
});
expect(runLock.isInsideLock()).toBe(false);
} finally {
await redis.quit();
}
});
redisTest("Test double locking works", { timeout: 15_000 }, async ({ redisOptions }) => {
const redis = createRedisClient(redisOptions);
try {
const runLock = new RunLocker({ redis });
expect(runLock.isInsideLock()).toBe(false);
await runLock.lock(["test-1"], 5000, async (signal) => {
expect(signal).toBeDefined();
expect(runLock.isInsideLock()).toBe(true);
//should be able to "lock it again"
await runLock.lock(["test-1"], 5000, async (signal) => {
expect(signal).toBeDefined();
expect(runLock.isInsideLock()).toBe(true);
});
});
expect(runLock.isInsideLock()).toBe(false);
} finally {
await redis.quit();
}
});
});