Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 11 additions & 15 deletions tests/test-httpserver-chunked.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import assert from 'tjs:assert';

// Skipped under GC stress: forcing a full GC before every allocation starves
// the event loop, so the per-chunk delays and the read-timeout race this test
// relies on no longer hold.
if (tjs.env.TJS_GC_STRESS) {
tjs.exit(0);
}

const encoder = new TextEncoder();
const decoder = new TextDecoder();

Expand All @@ -24,25 +17,28 @@ async function raw(port, parts) {
await delay(10);
}

try {
await writer.close();
} catch {}

// Don't half-close before reading: the server treats the FIN as a full
// close and drops a response the handler hasn't written yet. Every request
// sends `Connection: close`, so read until EOF, bounded by one deadline.
const reader = readable.getReader();
const chunks = [];
let timer;
const deadline = new Promise(resolve => {
timer = setTimeout(() => resolve({ done: true }), 5000);
});

while (true) {
const read = reader.read();
const timeout = delay(500).then(() => ({ done: true, timeout: true }));
const { value, done, timeout: timedOut } = await Promise.race([ read, timeout ]);
const { value, done } = await Promise.race([ reader.read(), deadline ]);

if (done || timedOut) {
if (done) {
break;
}

chunks.push(decoder.decode(value, { stream: true }));
}

clearTimeout(timer);

try {
con.close();
} catch {}
Expand Down
23 changes: 11 additions & 12 deletions tests/test-httpserver-framing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@ import assert from 'tjs:assert';
const encoder = new TextEncoder();
const decoder = new TextDecoder();

function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function raw(port, request) {
const con = await tjs.connect('tcp', '127.0.0.1', port);
const { readable, writable } = await con.opened;
const writer = writable.getWriter();

await writer.write(encoder.encode(request));

try {
await writer.close();
} catch {}

// Don't half-close before reading: the server treats the FIN as a full
// close and drops a response the handler hasn't written yet. Every request
// sends `Connection: close`, so read until EOF, bounded by one deadline.
const reader = readable.getReader();
const chunks = [];
let timer;
const deadline = new Promise(resolve => {
timer = setTimeout(() => resolve({ done: true }), 5000);
});

while (true) {
const read = reader.read();
const timeout = delay(500).then(() => ({ done: true, timeout: true }));
const { value, done, timeout: timedOut } = await Promise.race([ read, timeout ]);
const { value, done } = await Promise.race([ reader.read(), deadline ]);

if (done || timedOut) {
if (done) {
break;
}

chunks.push(decoder.decode(value, { stream: true }));
}

clearTimeout(timer);

try {
con.close();
} catch {}
Expand Down
Loading