|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
2 | 3 | import { assert, it } from "@effect/vitest"; |
| 4 | +import * as Deferred from "effect/Deferred"; |
3 | 5 | import * as Effect from "effect/Effect"; |
4 | 6 | import * as Exit from "effect/Exit"; |
| 7 | +import * as Fiber from "effect/Fiber"; |
5 | 8 | import * as FileSystem from "effect/FileSystem"; |
6 | 9 | import * as Layer from "effect/Layer"; |
7 | 10 | import * as Path from "effect/Path"; |
8 | 11 | import * as PlatformError from "effect/PlatformError"; |
| 12 | +import * as TestClock from "effect/testing/TestClock"; |
9 | 13 |
|
10 | 14 | import { writeFileStringAtomically } from "./atomicWrite.ts"; |
11 | 15 |
|
@@ -101,6 +105,113 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => { |
101 | 105 | assert.strictEqual(yield* fs.readFileString(filePath), "fresh"); |
102 | 106 | }), |
103 | 107 | ); |
| 108 | + |
| 109 | + it.effect("retries a Windows replacement while another process holds the target", () => |
| 110 | + Effect.gen(function* () { |
| 111 | + const fs = yield* FileSystem.FileSystem; |
| 112 | + const path = yield* Path.Path; |
| 113 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 114 | + const filePath = path.join(root, "settings.json"); |
| 115 | + yield* fs.writeFileString(filePath, "before"); |
| 116 | + const firstAttempt = yield* Deferred.make<void>(); |
| 117 | + let attempts = 0; |
| 118 | + const lockedOnce = FileSystem.make({ |
| 119 | + ...fs, |
| 120 | + rename: (from, to) => |
| 121 | + Effect.suspend(() => { |
| 122 | + attempts += 1; |
| 123 | + return attempts <= 2 |
| 124 | + ? Deferred.succeed(firstAttempt, undefined).pipe( |
| 125 | + Effect.andThen(Effect.fail(renameError("EBUSY", to))), |
| 126 | + ) |
| 127 | + : fs.rename(from, to); |
| 128 | + }), |
| 129 | + }); |
| 130 | + |
| 131 | + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe( |
| 132 | + Effect.provideService(FileSystem.FileSystem, lockedOnce), |
| 133 | + Effect.provideService(HostProcessPlatform, "win32"), |
| 134 | + Effect.forkChild, |
| 135 | + ); |
| 136 | + yield* Deferred.await(firstAttempt); |
| 137 | + yield* TestClock.adjust("1 second"); |
| 138 | + yield* Fiber.join(writing); |
| 139 | + |
| 140 | + assert.strictEqual(attempts, 3); |
| 141 | + assert.strictEqual(yield* fs.readFileString(filePath), "after"); |
| 142 | + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]); |
| 143 | + }), |
| 144 | + ); |
| 145 | + |
| 146 | + it.effect("gives up on a Windows lock that does not clear and keeps the old contents", () => |
| 147 | + Effect.gen(function* () { |
| 148 | + const fs = yield* FileSystem.FileSystem; |
| 149 | + const path = yield* Path.Path; |
| 150 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 151 | + const filePath = path.join(root, "settings.json"); |
| 152 | + yield* fs.writeFileString(filePath, "before"); |
| 153 | + const firstAttempt = yield* Deferred.make<void>(); |
| 154 | + let attempts = 0; |
| 155 | + const locked = FileSystem.make({ |
| 156 | + ...fs, |
| 157 | + rename: (_from, to) => |
| 158 | + Effect.suspend(() => { |
| 159 | + attempts += 1; |
| 160 | + return Deferred.succeed(firstAttempt, undefined).pipe( |
| 161 | + Effect.andThen(Effect.fail(renameError("EPERM", to))), |
| 162 | + ); |
| 163 | + }), |
| 164 | + }); |
| 165 | + |
| 166 | + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe( |
| 167 | + Effect.provideService(FileSystem.FileSystem, locked), |
| 168 | + Effect.provideService(HostProcessPlatform, "win32"), |
| 169 | + Effect.exit, |
| 170 | + Effect.forkChild, |
| 171 | + ); |
| 172 | + yield* Deferred.await(firstAttempt); |
| 173 | + yield* TestClock.adjust("1 minute"); |
| 174 | + |
| 175 | + assert.isTrue(Exit.isFailure(yield* Fiber.join(writing))); |
| 176 | + assert.isAbove(attempts, 1); |
| 177 | + assert.strictEqual(yield* fs.readFileString(filePath), "before"); |
| 178 | + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]); |
| 179 | + }), |
| 180 | + ); |
| 181 | + |
| 182 | + it.effect("fails a rename at once outside Windows or for other errors", () => |
| 183 | + Effect.gen(function* () { |
| 184 | + const fs = yield* FileSystem.FileSystem; |
| 185 | + const path = yield* Path.Path; |
| 186 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 187 | + for (const [platform, code] of [ |
| 188 | + ["linux", "EPERM"], |
| 189 | + ["win32", "EXDEV"], |
| 190 | + ] as const) { |
| 191 | + let attempts = 0; |
| 192 | + const failing = FileSystem.make({ |
| 193 | + ...fs, |
| 194 | + rename: (_from, to) => |
| 195 | + Effect.suspend(() => { |
| 196 | + attempts += 1; |
| 197 | + return Effect.fail(renameError(code, to)); |
| 198 | + }), |
| 199 | + }); |
| 200 | + |
| 201 | + const result = yield* writeFileStringAtomically({ |
| 202 | + filePath: path.join(root, `${platform}.json`), |
| 203 | + contents: "after", |
| 204 | + }).pipe( |
| 205 | + Effect.provideService(FileSystem.FileSystem, failing), |
| 206 | + Effect.provideService(HostProcessPlatform, platform), |
| 207 | + Effect.exit, |
| 208 | + ); |
| 209 | + |
| 210 | + assert.isTrue(Exit.isFailure(result)); |
| 211 | + assert.strictEqual(attempts, 1); |
| 212 | + } |
| 213 | + }), |
| 214 | + ); |
104 | 215 | }); |
105 | 216 |
|
106 | 217 | it.effect("surfaces an unreadable link instead of writing over it", () => |
@@ -183,3 +294,12 @@ it.effect("succeeds when the write lands but its temp directory cannot be remove |
183 | 294 | assert.deepStrictEqual(removed, ["/home/settings.json.abc123"]); |
184 | 295 | }), |
185 | 296 | ); |
| 297 | + |
| 298 | +const renameError = (code: string, path: string) => |
| 299 | + PlatformError.systemError({ |
| 300 | + _tag: "Unknown", |
| 301 | + module: "FileSystem", |
| 302 | + method: "rename", |
| 303 | + pathOrDescriptor: path, |
| 304 | + cause: Object.assign(new Error(`${code}, rename`), { code }), |
| 305 | + }); |
0 commit comments