Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/miniflare/test/plugins/do/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
kUnsafeEphemeralUniqueKey,
Miniflare,
} from "miniflare";
import { describe, onTestFinished, test } from "vitest";
import { describe, onTestFinished, test, vi } from "vitest";
import {
disposeWithRetry,
singleModuleManifest,
Expand Down Expand Up @@ -908,10 +908,12 @@ export class BlockingDO extends DurableObject {

release(lock) {
const releaseFn = this.locks.get(lock);
if (releaseFn) {
releaseFn();
this.locks.delete(lock);
if (!releaseFn) {
return false;
}
releaseFn();
this.locks.delete(lock);
return true;
}
}

Expand Down Expand Up @@ -951,7 +953,6 @@ test("Durable Object RPC calls do not block Node.js event loop", async ({
const stubId = namespace.idFromName("test");
const stub = namespace.get(stubId) as unknown as {
blockedOp: (n: number, lock: string) => Promise<number>;
release: (lock: string) => Promise<void>;
};

const blockedPromise = stub.blockedOp(5, "lock-1");
Expand Down Expand Up @@ -994,21 +995,18 @@ test("Durable Object RPC calls complete when unblocked", async ({ expect }) => {
const stubId = namespace.idFromName("test");
const stub = namespace.get(stubId) as unknown as {
blockedOp: (n: number, lock: string) => Promise<number>;
release: (lock: string) => Promise<void>;
release: (lock: string) => Promise<boolean>;
};

const blockedPromise = stub.blockedOp(10, "lock-2");

// Race the blocked operation against a timeout, releasing the lock as part of the race.
// The release should cause `blockedPromise` to resolve before the timeout.
// Use a generous timeout (5s) to avoid flakiness in CI environments.
const raced = await Promise.race([
blockedPromise.then((result) => ({ type: "resolved", result })),
stub
.release("lock-2")
.then(() => setTimeout(5_000))
.then(() => ({ type: "timeout" })),
await Promise.all([
expect(blockedPromise).resolves.toBe(12),
vi.waitFor(
async () => {
expect(await stub.release("lock-2")).toBe(true);
},
{ timeout: 5_000 }
),
]);
Comment thread
petebacondarwin marked this conversation as resolved.
Resolved

expect(raced).toEqual({ type: "resolved", result: 12 });
});
Loading