Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: mark two unit tests as flaky #18344

Merged
merged 4 commits into from
Mar 22, 2023
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
73 changes: 48 additions & 25 deletions cli/tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,30 @@ function findClosedPortInRange(
);
}

function flakyTest(
fn: (t: Deno.TestContext) => Promise<void>,
) {
async function wrapperFn(t: Deno.TestContext) {
let lastError;

for (let i = 0; i < 3; i++) {
try {
await fn(t);
return;
} catch (e) {
lastError = e;
}
}

throw lastError;
}
Object.defineProperty(wrapperFn, "name", { value: fn.name });
return wrapperFn;
}

Deno.test(
{ permissions: { net: true } },
async function fetchConnectionError() {
flakyTest(async function fetchConnectionError() {
const port = findClosedPortInRange(4000, 9999);
await assertRejects(
async () => {
Expand All @@ -65,7 +86,7 @@ Deno.test(
TypeError,
"error trying to connect",
);
},
}),
);

Deno.test(
Expand Down Expand Up @@ -1676,32 +1697,34 @@ function invalidServer(addr: string, body: Uint8Array): Deno.Listener {

Deno.test(
{ permissions: { net: true } },
async function fetchWithInvalidContentLengthAndTransferEncoding(): Promise<
void
> {
const addr = "127.0.0.1:4516";
const data = "a".repeat(10 << 10);

const body = new TextEncoder().encode(
`HTTP/1.1 200 OK\r\nContent-Length: ${
Math.round(data.length * 2)
}\r\nTransfer-Encoding: chunked\r\n\r\n${
data.length.toString(16)
}\r\n${data}\r\n0\r\n\r\n`,
);
flakyTest(
async function fetchWithInvalidContentLengthAndTransferEncoding(): Promise<
void
> {
const addr = "127.0.0.1:4516";
const data = "a".repeat(10 << 10);

const body = new TextEncoder().encode(
`HTTP/1.1 200 OK\r\nContent-Length: ${
Math.round(data.length * 2)
}\r\nTransfer-Encoding: chunked\r\n\r\n${
data.length.toString(16)
}\r\n${data}\r\n0\r\n\r\n`,
);

// if transfer-encoding is sent, content-length is ignored
// even if it has an invalid value (content-length > totalLength)
const listener = invalidServer(addr, body);
const response = await fetch(`http:https://${addr}/`);
// if transfer-encoding is sent, content-length is ignored
// even if it has an invalid value (content-length > totalLength)
const listener = invalidServer(addr, body);
const response = await fetch(`http:https://${addr}/`);

const res = await response.arrayBuffer();
const buf = new TextEncoder().encode(data);
assertEquals(res.byteLength, buf.byteLength);
assertEquals(new Uint8Array(res), buf);
const res = await response.arrayBuffer();
const buf = new TextEncoder().encode(data);
assertEquals(res.byteLength, buf.byteLength);
assertEquals(new Uint8Array(res), buf);

listener.close();
},
listener.close();
},
),
);

Deno.test(
Expand Down