fix: let's try to refactor shutdown again, again

This commit is contained in:
Andy Burke 2025-07-24 12:00:22 -07:00
parent 0b5f0c5d5e
commit a9b20fea40
4 changed files with 196 additions and 9 deletions

View file

@ -0,0 +1,94 @@
import * as asserts from '@std/assert';
import { EPHEMERAL_SERVER, get_ephemeral_listen_server } from './helpers.ts';
Deno.test({
name: 'test that event handlers are cleaned up properly',
permissions: {
env: true,
read: true,
write: true,
net: true
},
sanitizeResources: false,
sanitizeOps: false,
// TODO: why does this leak an event handler necessitating the above settings??? spent almost a day on this, cannot figure it out yet.
fn: async () => {
let test_server_info: EPHEMERAL_SERVER | null = null;
try {
test_server_info = await get_ephemeral_listen_server({
root: './tests/www'
});
const NUM_INITIAL_EVENTS = 5;
const events_initial_batch: any[] = [];
for (let i = 0; i < NUM_INITIAL_EVENTS; ++i) {
const event_response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/api/events`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
value: `${i}`
})
});
const event = await event_response.json();
asserts.assert(event);
events_initial_batch.push(event);
}
asserts.assertEquals(events_initial_batch.length, NUM_INITIAL_EVENTS);
const events_from_server_initial_batch =
await (await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/api/events`, {
method: 'GET'
})).json();
asserts.assertEquals(events_from_server_initial_batch.length, NUM_INITIAL_EVENTS);
const latest_event = events_from_server_initial_batch.at(-1);
asserts.assert(latest_event);
asserts.assertEquals(latest_event, events_from_server_initial_batch[events_from_server_initial_batch.length - 1]);
const long_poll_request_promise = fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/api/events?wait=true&after=${latest_event.timestamp}`,
{
method: 'GET'
}
);
const wait_and_then_create_an_event = new Promise((resolve) => {
setTimeout(async () => {
const event = await (await fetch(`http://${test_server_info?.hostname}:${test_server_info?.port}/api/events`, {
method: 'POST',
body: JSON.stringify({
value: 'new latest'
})
})).json();
resolve(event);
}, 2_000);
});
await (Promise.all([wait_and_then_create_an_event, long_poll_request_promise]).then(async (values) => {
const new_event = values.shift();
asserts.assert(new_event);
const long_poll_response: Response | undefined = values.shift() as Response;
asserts.assert(long_poll_response);
const long_polled_events = await long_poll_response.json();
asserts.assert(Array.isArray(long_polled_events));
asserts.assertEquals(long_polled_events, [new_event]);
}));
} finally {
if (test_server_info) {
await test_server_info.server.stop();
}
}
}
});