|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 2 | import { assert, it } from "@effect/vitest"; |
| 3 | +import * as Deferred from "effect/Deferred"; |
3 | 4 | import * as Effect from "effect/Effect"; |
4 | 5 | import * as Exit from "effect/Exit"; |
| 6 | +import * as Fiber from "effect/Fiber"; |
5 | 7 | import * as FileSystem from "effect/FileSystem"; |
6 | 8 | import * as Layer from "effect/Layer"; |
7 | 9 | import * as Path from "effect/Path"; |
8 | 10 | import * as PlatformError from "effect/PlatformError"; |
| 11 | +import * as TestClock from "effect/testing/TestClock"; |
9 | 12 |
|
10 | 13 | import { writeFileStringAtomically } from "./atomicWrite.ts"; |
| 14 | +import * as HostProcess from "./HostProcess.ts"; |
11 | 15 |
|
12 | 16 | it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => { |
13 | 17 | it.effect("keeps a symlinked file linked and rewrites its destination", () => |
@@ -101,8 +105,169 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => { |
101 | 105 | assert.strictEqual(yield* fs.readFileString(filePath), "fresh"); |
102 | 106 | }), |
103 | 107 | ); |
| 108 | + |
| 109 | + it.effect("waits out a Windows scanner holding the target, then replaces it", () => |
| 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 lockedTwice = 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, lockedTwice), |
| 133 | + Effect.provideService(HostProcess.Platform, "win32"), |
| 134 | + Effect.forkChild, |
| 135 | + ); |
| 136 | + yield* Deferred.await(firstAttempt); |
| 137 | + yield* advanceUntilDone(writing); |
| 138 | + |
| 139 | + assert.strictEqual(attempts, 3); |
| 140 | + assert.strictEqual(yield* fs.readFileString(filePath), "after"); |
| 141 | + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]); |
| 142 | + }), |
| 143 | + ); |
| 144 | + |
| 145 | + it.effect("gives up on a Windows lock that does not clear and keeps the old contents", () => |
| 146 | + Effect.gen(function* () { |
| 147 | + const fs = yield* FileSystem.FileSystem; |
| 148 | + const path = yield* Path.Path; |
| 149 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 150 | + const filePath = path.join(root, "settings.json"); |
| 151 | + yield* fs.writeFileString(filePath, "before"); |
| 152 | + const firstAttempt = yield* Deferred.make<void>(); |
| 153 | + let attempts = 0; |
| 154 | + const locked = FileSystem.make({ |
| 155 | + ...fs, |
| 156 | + rename: (_from, to) => |
| 157 | + Effect.suspend(() => { |
| 158 | + attempts += 1; |
| 159 | + return Deferred.succeed(firstAttempt, undefined).pipe( |
| 160 | + Effect.andThen(Effect.fail(renameError("EPERM", to))), |
| 161 | + ); |
| 162 | + }), |
| 163 | + }); |
| 164 | + |
| 165 | + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe( |
| 166 | + Effect.provideService(FileSystem.FileSystem, locked), |
| 167 | + Effect.provideService(HostProcess.Platform, "win32"), |
| 168 | + Effect.exit, |
| 169 | + Effect.forkChild, |
| 170 | + ); |
| 171 | + yield* Deferred.await(firstAttempt); |
| 172 | + |
| 173 | + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(writing))); |
| 174 | + assert.isAbove(attempts, 1); |
| 175 | + assert.strictEqual(yield* fs.readFileString(filePath), "before"); |
| 176 | + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]); |
| 177 | + }), |
| 178 | + ); |
| 179 | + |
| 180 | + it.effect("does not replace a newer save that landed while the lock was held", () => |
| 181 | + Effect.gen(function* () { |
| 182 | + const fs = yield* FileSystem.FileSystem; |
| 183 | + const path = yield* Path.Path; |
| 184 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 185 | + const filePath = path.join(root, "settings.json"); |
| 186 | + yield* fs.writeFileString(filePath, "before"); |
| 187 | + const firstAttempt = yield* Deferred.make<void>(); |
| 188 | + let attempts = 0; |
| 189 | + const lockedOnce = FileSystem.make({ |
| 190 | + ...fs, |
| 191 | + rename: (from, to) => |
| 192 | + Effect.suspend(() => { |
| 193 | + attempts += 1; |
| 194 | + return attempts === 1 |
| 195 | + ? Deferred.succeed(firstAttempt, undefined).pipe( |
| 196 | + Effect.andThen(Effect.fail(renameError("EBUSY", to))), |
| 197 | + ) |
| 198 | + : fs.rename(from, to); |
| 199 | + }), |
| 200 | + }); |
| 201 | + |
| 202 | + const stale = yield* writeFileStringAtomically({ filePath, contents: "stale" }).pipe( |
| 203 | + Effect.provideService(FileSystem.FileSystem, lockedOnce), |
| 204 | + Effect.provideService(HostProcess.Platform, "win32"), |
| 205 | + Effect.exit, |
| 206 | + Effect.forkChild, |
| 207 | + ); |
| 208 | + yield* Deferred.await(firstAttempt); |
| 209 | + // Another process saves once the lock clears, before the waiting write retries. |
| 210 | + yield* writeFileStringAtomically({ filePath, contents: "newer" }); |
| 211 | + |
| 212 | + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(stale))); |
| 213 | + assert.strictEqual(yield* fs.readFileString(filePath), "newer"); |
| 214 | + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]); |
| 215 | + }), |
| 216 | + ); |
| 217 | + |
| 218 | + it.effect("fails at once on a read-only Windows target", () => |
| 219 | + Effect.gen(function* () { |
| 220 | + const fs = yield* FileSystem.FileSystem; |
| 221 | + const path = yield* Path.Path; |
| 222 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" }); |
| 223 | + const filePath = path.join(root, "settings.json"); |
| 224 | + yield* fs.writeFileString(filePath, "before"); |
| 225 | + yield* Effect.acquireRelease(fs.chmod(filePath, 0o444), () => |
| 226 | + fs.chmod(filePath, 0o644).pipe(Effect.ignore), |
| 227 | + ); |
| 228 | + let attempts = 0; |
| 229 | + const denied = FileSystem.make({ |
| 230 | + ...fs, |
| 231 | + rename: (_from, to) => |
| 232 | + Effect.suspend(() => { |
| 233 | + attempts += 1; |
| 234 | + return Effect.fail(renameError("EPERM", to)); |
| 235 | + }), |
| 236 | + }); |
| 237 | + |
| 238 | + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe( |
| 239 | + Effect.provideService(FileSystem.FileSystem, denied), |
| 240 | + Effect.provideService(HostProcess.Platform, "win32"), |
| 241 | + Effect.exit, |
| 242 | + Effect.forkChild, |
| 243 | + ); |
| 244 | + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(writing))); |
| 245 | + assert.strictEqual(attempts, 1); |
| 246 | + assert.strictEqual(yield* fs.readFileString(filePath), "before"); |
| 247 | + }), |
| 248 | + ); |
104 | 249 | }); |
105 | 250 |
|
| 251 | +// Retries stat the real target between waits, and that I/O finishes outside the |
| 252 | +// test clock, so the clock moves one wait at a time until the write settles. |
| 253 | +const advanceUntilDone = <A, E>(fiber: Fiber.Fiber<A, E>) => |
| 254 | + Effect.gen(function* () { |
| 255 | + while (fiber.pollUnsafe() === undefined) { |
| 256 | + yield* TestClock.adjust("100 millis"); |
| 257 | + yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve))); |
| 258 | + } |
| 259 | + return yield* Fiber.join(fiber); |
| 260 | + }); |
| 261 | + |
| 262 | +const renameError = (code: string, path: string) => |
| 263 | + PlatformError.systemError({ |
| 264 | + _tag: "Unknown", |
| 265 | + module: "FileSystem", |
| 266 | + method: "rename", |
| 267 | + pathOrDescriptor: path, |
| 268 | + cause: Object.assign(new Error(`${code}, rename`), { code }), |
| 269 | + }); |
| 270 | + |
106 | 271 | it.effect("surfaces an unreadable link instead of writing over it", () => |
107 | 272 | Effect.gen(function* () { |
108 | 273 | const readLinkFailure = PlatformError.systemError({ |
|
0 commit comments