Skip to content

Commit bb9bb7c

Browse files
committed
test: join asset import cancellation before releasing native copy
1 parent 0b48453 commit bb9bb7c

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

‎packages/cli/tests/repl-asset-import.test.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ import type { ReplElicitations, ReplQuestion } from "../src/repl/elicitation.ts"
2626

2727
const BINARY = new Uint8Array([0, 255, 128, 195, 40, 13, 10, 65]);
2828

29+
function nativeGate() {
30+
let release = () => {};
31+
const promise = new Promise<void>((resolve) => {
32+
release = resolve;
33+
});
34+
return { promise, resolve: () => release() };
35+
}
36+
2937
function* question(questions: ReplElicitations): Operation<ReplQuestion> {
3038
const changes = yield* questions.changes;
3139
while (questions.pending === undefined) {
@@ -311,17 +319,32 @@ describe("mounted asset import", () => {
311319
it(`A7: ${origin} joins a held actual native copy before staging removal`, function* () {
312320
const { source } = yield* fixture();
313321
const entered = withResolvers<void>();
314-
const gate = Promise.withResolvers<void>();
322+
const gate = nativeGate();
323+
const joining = withResolvers<void>();
315324
const events: string[] = [];
316325
let temporary: string | undefined;
317326
yield* useNodeReplAssetImport({
318327
copyFile: (from, to, mode) => {
319328
entered.resolve();
320-
return gate.promise
329+
const transfer = gate.promise
321330
.then(() => copyFile(from, to, mode))
322331
.then(() => {
323332
events.push("copy complete");
324333
});
334+
const adopt = transfer.then.bind(transfer);
335+
transfer.then = (fulfilled, rejected) => {
336+
const retained = adopt(fulfilled, rejected);
337+
const then = retained.then.bind(retained);
338+
retained.then = (value, error) => {
339+
// The native owner's teardown joins both settlements of its retained request.
340+
if (value !== undefined && error !== undefined) {
341+
joining.resolve();
342+
}
343+
return then(value, error);
344+
};
345+
return retained;
346+
};
347+
return transfer;
325348
},
326349
link,
327350
mkdir,
@@ -356,6 +379,7 @@ describe("mounted asset import", () => {
356379
} else {
357380
yield* page.press("[exit]");
358381
}
382+
yield* joining.operation;
359383
expect(repl.outcome()).toBeUndefined();
360384
expect(events).toEqual([]);
361385
expect(yield* until(readdir(temporary ?? ""))).toBeDefined();
@@ -388,7 +412,7 @@ describe("native import ownership and uncertain completion", () => {
388412
const { source } = yield* fixture();
389413
const held = yield* useHarness();
390414
const entered = withResolvers<void>();
391-
const release = Promise.withResolvers<void>();
415+
const release = nativeGate();
392416
let temporary: string | undefined;
393417
const events: string[] = [];
394418
yield* useNodeReplAssetImport({

0 commit comments

Comments
 (0)