gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain5d144817Merge branch 'records-t40' into mainTicketsmain/plugins/http1/tests/sse-close.mjs

3.0 KB

  1. import assert from 'node:assert/strict';
  2. import http from 'node:http';
  3. import { spawn, spawnSync } from 'node:child_process';
  4. import { readlinkSync } from 'node:fs';
  5. import { once } from 'node:events';
  6. import { dirname, resolve } from 'node:path';
  7. import { fileURLToPath } from 'node:url';
  8. const HERE = dirname(fileURLToPath(import.meta.url));
  9. const ROOT = resolve(HERE, '../../..');
  10. const PORT = Number(process.env.HL_TEST_PORT || 32069);
  11. const BIN = process.env.HL_BIN || resolve(ROOT, 'native/zig-out/bin/hybriel');
  12. const child = spawn(BIN, [resolve(HERE, 'sse-close.hl')], { cwd: ROOT, env: process.env });
  13. let log = '', checks = 0;
  14. child.stdout.on('data', d => { log += d; });
  15. child.stderr.on('data', d => { log += d; });
  16. const streams = [];
  17. const sleep = ms => new Promise(r => setTimeout(r, ms));
  18. async function waitFor(fn) {
  19. for (let i = 0; i < 250; i++) { if (await fn()) return; await sleep(20); }
  20. throw new Error('timeout\n' + log);
  21. }
  22. const get = path => fetch(`http://127.0.0.1:${PORT}${path}`).then(r => r.text());
  23. function check(a, b) { assert.deepEqual(a, b); checks++; }
  24. async function stream() {
  25. const p = { data: '', ended: false };
  26. streams.push(p);
  27. p.req = http.get(`http://127.0.0.1:${PORT}/stream`, res => {
  28. p.res = res;
  29. res.on('data', d => { p.data += d; });
  30. res.on('end', () => { p.ended = true; });
  31. res.on('error', e => { p.error = e; });
  32. });
  33. p.req.on('error', e => { p.error = e; });
  34. await waitFor(() => /data: (\d+)\n\n/.test(p.data));
  35. p.id = Number(p.data.match(/data: (\d+)\n\n/)[1]);
  36. return p;
  37. }
  38. try {
  39. await waitFor(async () => { try { return await get('/') === '0'; } catch { return false; } });
  40. const a = await stream(), b = await stream();
  41. check(await get('/'), '2');
  42. check(await get(`/alive?id=${a.id}`), 'true');
  43. check(await get(`/close?id=${a.id}`), 'true');
  44. await waitFor(() => a.ended);
  45. check(a.ended, true);
  46. check(await get('/'), '1');
  47. check(await get(`/alive?id=${a.id}`), 'false');
  48. check(await get(`/send?id=${a.id}`), 'false');
  49. check(await get(`/close?id=${a.id}`), 'false');
  50. check(await get(`/send?id=${b.id}`), 'true');
  51. check(await get('/broadcast'), '1');
  52. await waitFor(() => b.data.includes('data: broadcast\n\n'));
  53. check(b.data, `: connected\n\ndata: ${b.id}\n\ndata: direct\n\ndata: broadcast\n\n`);
  54. const c = await stream();
  55. check(c.id > Math.max(a.id, b.id), true);
  56. check(await get('/'), '2');
  57. check(await get(`/send?id=${a.id}`), 'false');
  58. check(await get(`/send?id=${c.id}`), 'true');
  59. await waitFor(() => c.data.includes('data: direct\n\n'));
  60. check(c.data, `: connected\n\ndata: ${c.id}\n\ndata: direct\n\n`);
  61. check(await get(`/alive?id=${b.id}`), 'true');
  62. console.log(`ok sse-close: ${checks} checks`);
  63. } finally {
  64. for (const p of streams) { p.res?.destroy(); p.req.destroy(); }
  65. if (child.exitCode === null && child.signalCode === null) {
  66. assert.equal(readlinkSync(`/proc/${child.pid}/cwd`), ROOT);
  67. spawnSync('ss', ['-ltnp', `sport = ${PORT}`]);
  68. const stopped = once(child, 'exit'); child.kill('SIGTERM'); await stopped;
  69. }
  70. }

Branches

Latest commits

  • 5d144817Merge branch 'records-t40' into mainTickets
  • 44ee6f06|||PR records: #43 #45 livemre
  • b8fdeee9Merge branch 'p39-routes' into mainTickets
  • a0187a0a|||PR Remove redundant route-order loopmre
  • 369e4b9cMerge branch 'k40-lists' into mainTickets
  • 9af8e780|||PR Use keyed list mutations for live ticket updatesmre
  • 610d8e8aMerge branch 'records-t39' into mainTickets
  • 32bcdf24|||PR records: GO P39 K40mre
  • 4123c6d1Merge branch 'records-t38' into mainTickets
  • cd3641a1|||PR records: day72 deploy live, tickets closedmre
  • 7d44d852Merge branch 'records-t37' into mainTickets
  • 926f7061|||PR records: merges, D36 deploymre
  • bf58534eMerge branch 'l33-final' into mainTickets
  • 759a9ac3|||PR thin login (tickets#46, #45): merge main, route entry cleanupmre
  • 239436d8Merge remote-tracking branch 'origin/main' into l33-finalmre
  • 5822cb3aMerge branch 'o34-clean' into mainTickets
  • 0870653c|||PR offline note from __hlOnline, no polling (tickets#41)mre
  • 14cf96b4Merge branch 'records-t36' into mainTickets
  • 07749fa1|||PR records: day72 + #40 mergedmre
  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre