Skip to content

Commit b0f53b2

Browse files
authored
🧪 Give TD15's census a temporary root it owns (#486) (#487)
TD15 took its census over the host's shared temporary root, where every concurrently running suite mints xmd-tempdir-* directories, so under the composability battery a neighbour's directory was indistinguishable from a leak. The host Files provider now accepts a temporaryRoot, TD15 points it at a directory the test owns, and the final census converges with when() instead of sampling after a fixed sleep(50). Co-authored-by: Taras Mankovski <[email protected]>
1 parent e38220b commit b0f53b2

3 files changed

Lines changed: 49 additions & 14 deletions

File tree

‎packages/core/tests/temp-dir.test.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ function* imported(stream: InMemoryStream): Operation<string[]> {
127127
}
128128

129129
/**
130-
* Every temporary directory currently on disk. The leak this guards against is
131-
* a directory nobody holds a reference to, so the only way to see one is to
132-
* look at the root they are all created under.
130+
* Every temporary directory currently under `root`. The leak this guards
131+
* against is a directory nobody holds a reference to, so the only way to see
132+
* one is to look at the root they are all created under — and that root must
133+
* be one this test owns: the host's shared temporary root holds directories
134+
* every concurrently running suite mints, and a census over it cannot tell a
135+
* leak from a neighbour.
133136
*/
134-
function* temporaries(): Operation<string[]> {
135-
const root = yield* until(realpath(tmpdir()));
137+
function* temporaries(root: string): Operation<string[]> {
136138
const entries = yield* until(readdir(root));
137139
return entries.filter((entry) => entry.startsWith("xmd-tempdir-")).sort();
138140
}
@@ -494,8 +496,10 @@ describe("Tier TD — TempDir", () => {
494496
// one behind — whether it arrives while the directory is in use or before
495497
// the acquiring task has run at all.
496498
it("TD15: a cancelled acquisition leaves no directory behind", function* () {
497-
yield* useHostFiles();
498-
const before = yield* temporaries();
499+
// A root this test owns, so the census below sees only what these
500+
// acquisitions minted.
501+
const root = yield* useFixture();
502+
yield* useHostFiles({ temporaryRoot: root });
499503

500504
// Halted while the directory is live: the path is observed first, so the
501505
// assertion names the directory that actually existed.
@@ -510,15 +514,21 @@ describe("Tier TD — TempDir", () => {
510514
yield* live.halt();
511515
expect(yield* exists(directory)).toBe(false);
512516

513-
// Halted mid-acquisition, then given time to settle. An acquisition that
514-
// suspended on a pending creation would finish here, after the task that
515-
// asked for it is gone, and leave a directory nothing owns.
517+
// Halted mid-acquisition. An acquisition that suspended on a pending
518+
// creation would finish afterwards, after the task that asked for it is
519+
// gone, and leave a directory nothing owns.
516520
const early = yield* spawn(() => useTemporaryDirectory());
517521
yield* early.halt();
518-
yield* sleep(50);
519522

520-
// Whatever either task created, nothing survives it.
521-
expect(yield* temporaries()).toEqual(before);
523+
// Whatever either task created, nothing survives it. Converged on rather
524+
// than sampled after a fixed pause: a removal is complete when the census
525+
// says so, not when a saturated runner's clock has run out.
526+
yield* when(
527+
function* () {
528+
expect(yield* temporaries(root)).toEqual([]);
529+
},
530+
{ timeout: 5000 },
531+
);
522532
});
523533

524534
// TD16: the other durable effect a `<TempDir>` can consume. A nested

‎packages/runtime/host-files.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export type HostFilesObserver = (event: HostFilesEvent) => void;
8585

8686
export interface HostFilesOptions {
8787
readonly observe?: HostFilesObserver;
88+
/**
89+
* The directory temporary directories are minted under, defaulting to the
90+
* host's temporary root. The host's root is shared by every process on the
91+
* machine, so a caller that censuses the minted namespace — a lifetime test
92+
* proving nothing survives a cancellation — points this at a directory it
93+
* owns, where the only entries are the ones this provider created.
94+
*/
95+
readonly temporaryRoot?: string;
8896
}
8997

9098
/**
@@ -473,7 +481,7 @@ export function hostFilesHandler(options: HostFilesOptions = {}): FilesHandler {
473481
let created: string;
474482
try {
475483
// oxlint-disable-next-line local/no-sync-filesystem
476-
created = mkdtempSync(join(tmpdir(), "xmd-tempdir-"));
484+
created = mkdtempSync(join(options.temporaryRoot ?? tmpdir(), "xmd-tempdir-"));
477485
} catch (error) {
478486
yield* provide(nonWriteFailure("temporary-directory", "acquire", reasonOf(error)));
479487
return;

‎packages/runtime/tests/host-files.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,4 +861,21 @@ describe("Tier HF — host Files provider", () => {
861861
expect(parseFileWriteFailure(failed(written))?.reason).toBe("resolved-escape");
862862
expect(yield* entries(fixture.outside)).toEqual([]);
863863
});
864+
865+
// HF17: where a minted directory lives is the caller's to choose. The host's
866+
// shared temporary root holds every process's directories, so a caller that
867+
// censuses the minted namespace supplies a root it owns — and the directory
868+
// still lives and dies with its acquiring scope there.
869+
it("HF17: a temporary directory is minted under the configured root", function* () {
870+
const { root } = yield* useFixture();
871+
const files = hostFilesHandler({ temporaryRoot: root });
872+
873+
let acquired = "";
874+
yield* scoped(function* () {
875+
acquired = value(yield* files.temporaryDirectory());
876+
expect(acquired.startsWith(join(root, "xmd-tempdir-"))).toBe(true);
877+
expect(yield* exists(acquired)).toBe(true);
878+
});
879+
expect(yield* exists(acquired)).toBe(false);
880+
});
864881
});

0 commit comments

Comments
 (0)