nsarrazin HF Staff commited on
Commit
991f8ec
·
1 Parent(s): c44d626

fix(tests): make flaky migration tests more stable

Browse files
Files changed (1) hide show
  1. src/lib/migrations/migrations.spec.ts +41 -35
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("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
- });
 
 
 
 
 
14
 
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);
18
 
19
- const semaphores = await collections.semaphores.find({}).toArray();
20
 
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
 
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
- });
35
 
36
- it("should refresh the lock", async () => {
37
- const lockId = await acquireLock(LOCK_KEY);
38
 
39
- assert(lockId);
40
 
41
- // get the updatedAt time
42
 
43
- const updatedAtInitially = (await collections.semaphores.findOne({}))?.updatedAt;
44
 
45
- await refreshLock(LOCK_KEY, lockId);
46
 
47
- const updatedAtAfterRefresh = (await collections.semaphores.findOne({}))?.updatedAt;
48
 
49
- expect(updatedAtInitially).toBeDefined();
50
- expect(updatedAtAfterRefresh).toBeDefined();
51
- expect(updatedAtInitially).not.toBe(updatedAtAfterRefresh);
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({});