Skip to content

Commit 2aab722

Browse files
committedMar 18, 2025·
fix(tests): make flaky migration tests more stable
1 parent 0d039a2 commit 2aab722

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed
 

‎src/lib/migrations/migrations.spec.ts

+41-35
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,58 @@ import { collections } from "$lib/server/database";
55

66
const LOCK_KEY = "migrations.test";
77

8-
describe("migrations", () => {
9-
it("should not have duplicates guid", async () => {
10-
const guids = migrations.map((m) => m._id.toString());
11-
const uniqueGuids = [...new Set(guids)];
12-
expect(uniqueGuids.length).toBe(guids.length);
13-
});
8+
describe(
9+
"migrations",
10+
{
11+
retry: 3,
12+
},
13+
() => {
14+
it("should not have duplicates guid", async () => {
15+
const guids = migrations.map((m) => m._id.toString());
16+
const uniqueGuids = [...new Set(guids)];
17+
expect(uniqueGuids.length).toBe(guids.length);
18+
});
1419

15-
it("should acquire only one lock on DB", async () => {
16-
const results = await Promise.all(new Array(1000).fill(0).map(() => acquireLock(LOCK_KEY)));
17-
const locks = results.filter((r) => r);
20+
it("should acquire only one lock on DB", async () => {
21+
const results = await Promise.all(new Array(1000).fill(0).map(() => acquireLock(LOCK_KEY)));
22+
const locks = results.filter((r) => r);
1823

19-
const semaphores = await collections.semaphores.find({}).toArray();
24+
const semaphores = await collections.semaphores.find({}).toArray();
2025

21-
expect(locks.length).toBe(1);
22-
expect(semaphores).toBeDefined();
23-
expect(semaphores.length).toBe(1);
24-
expect(semaphores?.[0].key).toBe(LOCK_KEY);
25-
});
26+
expect(locks.length).toBe(1);
27+
expect(semaphores).toBeDefined();
28+
expect(semaphores.length).toBe(1);
29+
expect(semaphores?.[0].key).toBe(LOCK_KEY);
30+
});
2631

27-
it("should read the lock correctly", async () => {
28-
const lockId = await acquireLock(LOCK_KEY);
29-
assert(lockId);
30-
expect(await isDBLocked(LOCK_KEY)).toBe(true);
31-
expect(!!(await acquireLock(LOCK_KEY))).toBe(false);
32-
await releaseLock(LOCK_KEY, lockId);
33-
expect(await isDBLocked(LOCK_KEY)).toBe(false);
34-
});
32+
it("should read the lock correctly", async () => {
33+
const lockId = await acquireLock(LOCK_KEY);
34+
assert(lockId);
35+
expect(await isDBLocked(LOCK_KEY)).toBe(true);
36+
expect(!!(await acquireLock(LOCK_KEY))).toBe(false);
37+
await releaseLock(LOCK_KEY, lockId);
38+
expect(await isDBLocked(LOCK_KEY)).toBe(false);
39+
});
3540

36-
it("should refresh the lock", async () => {
37-
const lockId = await acquireLock(LOCK_KEY);
41+
it("should refresh the lock", async () => {
42+
const lockId = await acquireLock(LOCK_KEY);
3843

39-
assert(lockId);
44+
assert(lockId);
4045

41-
// get the updatedAt time
46+
// get the updatedAt time
4247

43-
const updatedAtInitially = (await collections.semaphores.findOne({}))?.updatedAt;
48+
const updatedAtInitially = (await collections.semaphores.findOne({}))?.updatedAt;
4449

45-
await refreshLock(LOCK_KEY, lockId);
50+
await refreshLock(LOCK_KEY, lockId);
4651

47-
const updatedAtAfterRefresh = (await collections.semaphores.findOne({}))?.updatedAt;
52+
const updatedAtAfterRefresh = (await collections.semaphores.findOne({}))?.updatedAt;
4853

49-
expect(updatedAtInitially).toBeDefined();
50-
expect(updatedAtAfterRefresh).toBeDefined();
51-
expect(updatedAtInitially).not.toBe(updatedAtAfterRefresh);
52-
});
53-
});
54+
expect(updatedAtInitially).toBeDefined();
55+
expect(updatedAtAfterRefresh).toBeDefined();
56+
expect(updatedAtInitially).not.toBe(updatedAtAfterRefresh);
57+
});
58+
}
59+
);
5460

5561
afterEach(async () => {
5662
await collections.semaphores.deleteMany({});

0 commit comments

Comments
 (0)
Please sign in to comment.