Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serviceSelectionLock.test.ts

30 lines1.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6​
7import { acquireServiceSelectionLock } from "./serviceSelectionLock.ts";
8​
9it.layer(NodeServices.layer)("service selection lock", (it) => {
10 it.effect("reclaims a lease from an earlier boot even when its PID is live again", () =>
11 Effect.gen(function* () {
12 const fs = yield* FileSystem.FileSystem;
13 const path = yield* Path.Path;
14 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-selection-lock-" });
15 const selectionPath = path.join(root, "service-state.json");
16 const lock = `${selectionPath}.selection-lock`;
17 const stale = `${process.pid}.1.00000000-0000-4000-8000-000000000000`;
18 yield* fs.makeDirectory(lock, { recursive: true });
19 yield* fs.writeFileString(path.join(lock, stale), "");
20​
21 const release = yield* Effect.promise(() => acquireServiceSelectionLock(selectionPath));
22 const owners = yield* fs.readDirectory(lock);
23 assert.equal(owners.length, 1);
24 assert.notEqual(owners[0], stale);
25 assert.isTrue(owners[0]!.startsWith(`${process.pid}.`));
26 release();
27 assert.isFalse(yield* fs.exists(lock));
28 }),
29 );
30});