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

windowsFileRetry.test.ts

100 lines3.1 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as PlatformError from "effect/PlatformError";
4​
5import { retryWindowsFileSystemOperation } from "./windowsFileRetry.ts";
6​
7const failure = (tag: PlatformError.SystemErrorTag, cause?: unknown) =>
8 PlatformError.systemError({
9 _tag: tag,
10 module: "FileSystem",
11 method: "rename",
12 pathOrDescriptor: "C:\\state\\settings.json",
13 ...(cause === undefined ? {} : { cause }),
14 });
15​
16describe("retryWindowsFileSystemOperation", () => {
17 it.effect("retries transient Windows sharing failures", () =>
18 Effect.gen(function* () {
19 let attempts = 0;
20 const operation = Effect.suspend(() => {
21 attempts += 1;
22 return attempts < 3 ? Effect.fail(failure("Busy")) : Effect.succeed("ok");
23 });
24​
25 const result = yield* retryWindowsFileSystemOperation(operation, {
26 platform: "win32",
27 delaysMs: [0, 0, 0],
28 });
29 assert.equal(result, "ok");
30 assert.equal(attempts, 3);
31 }),
32 );
33​
34 it.effect("retries Windows permission-denied replacement races", () =>
35 Effect.gen(function* () {
36 let attempts = 0;
37 const operation = Effect.suspend(() => {
38 attempts += 1;
39 return attempts < 2 ? Effect.fail(failure("PermissionDenied")) : Effect.succeed(attempts);
40 });
41 assert.equal(
42 yield* retryWindowsFileSystemOperation(operation, {
43 platform: "win32",
44 delaysMs: [0],
45 }),
46 2,
47 );
48 }),
49 );
50​
51 it.effect("retries an Unknown wrapper when the original Windows errno is transient", () =>
52 Effect.gen(function* () {
53 let attempts = 0;
54 const cause = Object.assign(new Error("sharing violation"), { code: "EPERM" });
55 const operation = Effect.suspend(() => {
56 attempts += 1;
57 return attempts < 2 ? Effect.fail(failure("Unknown", cause)) : Effect.succeed(true);
58 });
59 assert.isTrue(
60 yield* retryWindowsFileSystemOperation(operation, {
61 platform: "win32",
62 delaysMs: [0],
63 }),
64 );
65 assert.equal(attempts, 2);
66 }),
67 );
68​
69 it.effect("does not retry permanent failures", () =>
70 Effect.gen(function* () {
71 let attempts = 0;
72 const permanent = failure("NotFound");
73 const error = yield* retryWindowsFileSystemOperation(
74 Effect.suspend(() => {
75 attempts += 1;
76 return Effect.fail(permanent);
77 }),
78 { platform: "win32", delaysMs: [0, 0] },
79 ).pipe(Effect.flip);
80 assert.strictEqual(error, permanent);
81 assert.equal(attempts, 1);
82 }),
83 );
84​
85 it.effect("does not apply Windows retries on other platforms", () =>
86 Effect.gen(function* () {
87 let attempts = 0;
88 const transient = failure("Busy");
89 const error = yield* retryWindowsFileSystemOperation(
90 Effect.suspend(() => {
91 attempts += 1;
92 return Effect.fail(transient);
93 }),
94 { platform: "linux", delaysMs: [0, 0] },
95 ).pipe(Effect.flip);
96 assert.strictEqual(error, transient);
97 assert.equal(attempts, 1);
98 }),
99 );
100});