Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
fix(tests): make flaky migration tests more stable
Browse files
src/lib/migrations/migrations.spec.ts
CHANGED
@@ -5,52 +5,58 @@ import { collections } from "$lib/server/database";
|
|
5 |
|
6 |
const LOCK_KEY = "migrations.test";
|
7 |
|
8 |
-
describe(
|
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 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
}
|
|
|
54 |
|
55 |
afterEach(async () => {
|
56 |
await collections.semaphores.deleteMany({});
|
|
|
5 |
|
6 |
const LOCK_KEY = "migrations.test";
|
7 |
|
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 |
+
});
|
19 |
|
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);
|
23 |
|
24 |
+
const semaphores = await collections.semaphores.find({}).toArray();
|
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 |
+
});
|
31 |
|
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 |
+
});
|
40 |
|
41 |
+
it("should refresh the lock", async () => {
|
42 |
+
const lockId = await acquireLock(LOCK_KEY);
|
43 |
|
44 |
+
assert(lockId);
|
45 |
|
46 |
+
// get the updatedAt time
|
47 |
|
48 |
+
const updatedAtInitially = (await collections.semaphores.findOne({}))?.updatedAt;
|
49 |
|
50 |
+
await refreshLock(LOCK_KEY, lockId);
|
51 |
|
52 |
+
const updatedAtAfterRefresh = (await collections.semaphores.findOne({}))?.updatedAt;
|
53 |
|
54 |
+
expect(updatedAtInitially).toBeDefined();
|
55 |
+
expect(updatedAtAfterRefresh).toBeDefined();
|
56 |
+
expect(updatedAtInitially).not.toBe(updatedAtAfterRefresh);
|
57 |
+
});
|
58 |
+
}
|
59 |
+
);
|
60 |
|
61 |
afterEach(async () => {
|
62 |
await collections.semaphores.deleteMany({});
|