test(resources): consolidate scope close and lease checks (#12849)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 14:28:13 +00:00
1 parent 3dacda36f2
commit 6c067ec9af
2 files changed
+28 -93

No files matched your search

+11 -40
View File
@@ -32,44 +32,6 @@ it.each(["before release", "after release"])(
},
);
it("retains uncertainty across a failed retry until the same lease releases", async () => {
const cause = new Error("close still denied");
const error = new ProviderCleanupError("fixture", ["owned resource"], {});
let denied = true;
const lease = new AnalysisCleanupLease(async () => {
if (denied) throw cause;
});
lease.bindCleanupError(error);
await expect(lease.release()).rejects.toBe(cause);
expect(error.cleanupIncomplete).toBe(true);
denied = false;
await lease.release();
expect(error.cleanupIncomplete).toBe(false);
});
it("resolves an aggregate only after every bound owner releases", async () => {
const error = new ProviderCleanupError(
"fixture",
["first resource", "second resource"],
{},
);
const first = new AnalysisCleanupLease(
async () => undefined,
["first resource"],
);
const second = new AnalysisCleanupLease(
async () => undefined,
["second resource"],
);
new AnalysisCleanupLeaseGroup(error, [first, second]);
await first.release();
expect(error.cleanupIncomplete).toBe(true);
expect(error.cleanupResources).toEqual(["second resource"]);
await second.release();
expect(error.cleanupIncomplete).toBe(false);
});
it("lets errors read a group's release instead of having it pushed to them", async () => {
const error = new ProviderCleanupError("fixture", ["owned resource"], {});
const lease = new AnalysisCleanupLease(
@@ -105,7 +67,7 @@ it("does not resolve a cleanup group without exact owners", () => {
expect(error.cleanupIncomplete).toBe(true);
});
it("resolves every live retry error bound to one lease", async () => {
it("keeps every live error unresolved until their shared lease confirms release", async () => {
const firstCause = new Error("first admission retry failed");
const secondCause = new Error("second admission retry failed");
const firstError = new ProviderCleanupError(
@@ -120,10 +82,19 @@ it("resolves every live retry error bound to one lease", async () => {
{},
{ cause: secondCause },
);
const lease = new AnalysisCleanupLease(async () => undefined);
const closeFailure = new Error("close still denied");
let denied = true;
const lease = new AnalysisCleanupLease(async () => {
if (denied) throw closeFailure;
});
lease.bindCleanupError(firstError);
lease.bindCleanupError(secondError);
await expect(lease.release()).rejects.toBe(closeFailure);
expect(firstError.cleanupIncomplete).toBe(true);
expect(secondError.cleanupIncomplete).toBe(true);
denied = false;
await lease.release();
expect(firstError.cleanupIncomplete).toBe(false);
expect(secondError.cleanupIncomplete).toBe(false);
+17 -53
View File
@@ -24,28 +24,24 @@ import { translateArtifactFailure } from "./ArtifactOperationFailure.js";
it("waits for admitted operations and shares concurrent close", async () => {
const scope = new ArtifactResourceScope();
const admitted = phaseGate();
const order: string[] = [];
const operation = scope.run(async () => {
await admitted.hold();
order.push("operation finished");
});
scope.addCloseHook(async () => {
order.push("scope closed");
});
try {
await admitted.reached(operation, "scope admission");
const close = scope.close();
expect(scope.close()).toBe(close);
const close = Promise.all([scope.close(), scope.close()]);
await expect(scope.run(async () => undefined)).rejects.toMatchObject({
reason: "unavailable",
message: expect.stringContaining("closed"),
});
let closed = false;
void close.then(() => {
closed = true;
});
await Promise.resolve();
expect(closed).toBe(false);
admitted.release();
await operation;
await close;
expect(closed).toBe(true);
await Promise.all([operation, close]);
expect(order).toEqual(["operation finished", "scope closed"]);
} finally {
admitted.release();
try {
@@ -108,37 +104,6 @@ it("stops a close hook even when retained cleanup is still failing", async () =>
expect(hookRuns).toBe(2);
});
it("runs later close hooks when an earlier hook fails", async () => {
const scope = new ArtifactResourceScope();
const order: string[] = [];
scope.addCloseHook(async () => {
order.push("first");
throw new Error("first hook failed");
});
scope.addCloseHook(async () => {
order.push("second");
throw new Error("second hook failed");
});
const failure = await scope.close().then(
() => {
throw new Error("close succeeded while hooks failed");
},
(cause: unknown) => cause,
);
expect(order).toEqual(["first", "second"]);
expect(failure).toBeInstanceOf(AggregateError);
expect(
(failure as AggregateError).errors.map((error) =>
error instanceof Error ? error.message : String(error),
),
).toEqual(["first hook failed", "second hook failed"]);
order.length = 0;
await expect(scope.close()).rejects.toBeInstanceOf(AggregateError);
expect(order).toEqual(["first", "second"]);
});
it("rejects a close hook's undefined reason and retries through the same scope", async () => {
const scope = new ArtifactResourceScope();
let denied = true;
@@ -189,7 +154,7 @@ it.each([
}
});
it("preserves undefined alongside other close hook failures", async () => {
it("runs and retries all close hooks in order, preserving their exact failures", async () => {
const scope = new ArtifactResourceScope();
const failure = new Error("third hook failed");
const reasons = [undefined, null, failure];
@@ -202,16 +167,15 @@ it("preserves undefined alongside other close hook failures", async () => {
});
try {
const outcome = await scope.close().then(
() => ok(undefined),
(error: unknown) => err(error),
);
expect(outcome.ok).toBe(false);
if (outcome.ok || !(outcome.error instanceof AggregateError))
const failure = await scope.close().catch((cause: unknown) => cause);
if (!(failure instanceof AggregateError))
throw new Error("Expected all hook failures");
expect(outcome.error.errors).toEqual(reasons);
expect(outcome.error.errors[2]).toBe(failure);
expect(attempted).toEqual(reasons);
expect(failure.errors).toStrictEqual(reasons);
expect(failure.errors[2]).toBe(reasons[2]);
expect(attempted).toStrictEqual(reasons);
await expect(scope.close()).rejects.toMatchObject({ errors: reasons });
expect(attempted).toStrictEqual([...reasons, ...reasons]);
} finally {
denied = false;
await scope.close();