Skip to content

Commit

Permalink
Add flaky test helper (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Nov 1, 2023
1 parent 519d7bc commit 17ab3e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/miniflare/test/http/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
fetch,
} from "miniflare";
import { WebSocketServer } from "ws";
import { useServer } from "../test-shared";
import { flaky, useServer } from "../test-shared";

const noop = () => {};

Expand Down Expand Up @@ -109,7 +109,7 @@ test("fetch: includes headers from web socket upgrade response", async (t) => {
t.not(res.webSocket, undefined);
t.is(res.headers.get("set-cookie"), "key=value");
});
test("fetch: dispatches close events on client and server close", async (t) => {
const fetchDispatchCloseFlakyTest = flaky(async (t) => {
let clientCloses = 0;
let serverCloses = 0;
const clientClosePromise = new DeferredPromise<void>();
Expand Down Expand Up @@ -185,6 +185,10 @@ test("fetch: dispatches close events on client and server close", async (t) => {
await serverSideClose(true);
await serverClosePromise;
});
test(
"fetch: dispatches close events on client and server close",
fetchDispatchCloseFlakyTest
);
test("fetch: throws on ws(s) protocols", async (t) => {
await t.throwsAsync(
fetch("ws://localhost/", {
Expand Down
20 changes: 20 additions & 0 deletions packages/miniflare/test/test-shared/asserts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "assert";
import { ExecutionContext } from "ava";
import { Awaitable } from "miniflare";

export function isWithin(
t: ExecutionContext,
Expand All @@ -21,3 +22,22 @@ export function escapeRegexp(value: string): RegExp {
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
return new RegExp(value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
}

export function flaky(
impl: (t: ExecutionContext) => Awaitable<void>
): (t: ExecutionContext) => Promise<void> {
const maxAttempts = 3;
return async (t) => {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
const result = await t.try(impl);
if (result.passed || attempt === maxAttempts) {
result.commit();
return;
} else {
result.discard();
t.log(`Attempt #${attempt} failed!`);
t.log(...result.errors);
}
}
};
}

0 comments on commit 17ab3e4

Please sign in to comment.