File size: 1,891 Bytes
a4c3fca
4dbcbb6
 
 
 
29b7d2a
a4c3fca
4dbcbb6
 
 
 
 
 
 
 
a4c3fca
4dbcbb6
 
 
 
 
 
 
29b7d2a
4dbcbb6
 
 
a4c3fca
 
 
 
 
 
4dbcbb6
 
 
a4c3fca
 
 
4dbcbb6
 
 
 
 
a4c3fca
4dbcbb6
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
import { afterEach, assert, describe, expect, it } from "vitest";
import { migrations } from "./routines";
import { acquireLock, isDBLocked, refreshLock, releaseLock } from "./lock";
import { collections } from "$lib/server/database";

const LOCK_KEY = "migrations.test";

describe("migrations", () => {
	it("should not have duplicates guid", async () => {
		const guids = migrations.map((m) => m._id.toString());
		const uniqueGuids = [...new Set(guids)];
		expect(uniqueGuids.length).toBe(guids.length);
	});

	it("should acquire only one lock on DB", async () => {
		const results = await Promise.all(new Array(1000).fill(0).map(() => acquireLock(LOCK_KEY)));
		const locks = results.filter((r) => r);

		const semaphores = await collections.semaphores.find({}).toArray();

		expect(locks.length).toBe(1);
		expect(semaphores).toBeDefined();
		expect(semaphores.length).toBe(1);
		expect(semaphores?.[0].key).toBe(LOCK_KEY);
	});

	it("should read the lock correctly", async () => {
		const lockId = await acquireLock(LOCK_KEY);
		assert(lockId);
		expect(await isDBLocked(LOCK_KEY)).toBe(true);
		expect(!!(await acquireLock(LOCK_KEY))).toBe(false);
		await releaseLock(LOCK_KEY, lockId);
		expect(await isDBLocked(LOCK_KEY)).toBe(false);
	});

	it("should refresh the lock", async () => {
		const lockId = await acquireLock(LOCK_KEY);

		assert(lockId);

		// get the updatedAt time

		const updatedAtInitially = (await collections.semaphores.findOne({}))?.updatedAt;

		await refreshLock(LOCK_KEY, lockId);

		const updatedAtAfterRefresh = (await collections.semaphores.findOne({}))?.updatedAt;

		expect(updatedAtInitially).toBeDefined();
		expect(updatedAtAfterRefresh).toBeDefined();
		expect(updatedAtInitially).not.toBe(updatedAtAfterRefresh);
	});
});

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