gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain0f6d1592Merge branch 'f18b-shell-session' into mainIdentmain/plugins/http1/tests/two-ws-servers.mjs

3.4 KB

  1. // Mirrors ws-handshake-order.mjs: exercise the built plugin over real sockets.
  2. import { spawn, spawnSync } from 'node:child_process';
  3. import { readlinkSync } from 'node:fs';
  4. import { once } from 'node:events';
  5. import assert from 'node:assert/strict';
  6. import { fileURLToPath } from 'node:url';
  7. import { resolve, dirname } from 'node:path';
  8. const HERE = dirname(fileURLToPath(import.meta.url));
  9. const ROOT = resolve(HERE, '../../..');
  10. const PORT = Number(process.env.HL_TEST_PORT || 30090);
  11. const BIN = process.env.HL_BIN || resolve(ROOT, 'native/zig-out/bin/hybriel');
  12. const server = spawn(BIN, [resolve(HERE, 'two-ws-servers.hl')], { cwd: ROOT, env: { ...process.env, HL_TEST_PORT: String(PORT) }, stdio: ['ignore', 'pipe', 'pipe'] });
  13. let log = '', checks = 0;
  14. server.stdout.on('data', d => { log += d; });
  15. server.stderr.on('data', d => { log += d; });
  16. const peers = [];
  17. const sleep = ms => new Promise(r => setTimeout(r, ms));
  18. const get = (port, path = '/') => fetch(`http://127.0.0.1:${port}${path}`).then(r => r.text());
  19. function check(actual, expected) { assert.deepEqual(actual, expected); checks++; }
  20. async function waitFor(fn) {
  21. for (let i = 0; i < 150; i++) { if (await fn()) return; await sleep(20); }
  22. throw new Error('timeout\n' + log);
  23. }
  24. function peer(port) {
  25. const ws = new WebSocket(`ws://127.0.0.1:${port}/`);
  26. ws.messages = [];
  27. ws.addEventListener('message', e => ws.messages.push(String(e.data)));
  28. peers.push(ws);
  29. return ws;
  30. }
  31. try {
  32. await waitFor(async () => { try { return await get(PORT) === '0:0'; } catch { return false; } });
  33. const a1 = peer(PORT), a2 = peer(PORT), b = peer(PORT + 1);
  34. await waitFor(() => peers.every(p => p.messages.length >= 1));
  35. check(a1.messages, ['open:A']); check(a2.messages, ['open:A']); check(b.messages, ['open:B']);
  36. a1.send('one'); a2.send('two'); b.send('three');
  37. await waitFor(() => peers.every(p => p.messages.length >= 2));
  38. check(a1.messages, ['open:A', 'A:one']); check(a2.messages, ['open:A', 'A:two']); check(b.messages, ['open:B', 'B:three']);
  39. check(await get(PORT, '/broadcast'), '2');
  40. await waitFor(() => a1.messages.length === 3 && a2.messages.length === 3);
  41. check(await get(PORT + 1, '/broadcast'), '1');
  42. await waitFor(() => b.messages.length === 3);
  43. await sleep(100);
  44. check(a1.messages, ['open:A', 'A:one', 'broadcast:A']);
  45. check(a2.messages, ['open:A', 'A:two', 'broadcast:A']);
  46. check(b.messages, ['open:B', 'B:three', 'broadcast:B']);
  47. a1.close(); await once(a1, 'close');
  48. await waitFor(async () => await get(PORT) === '1:0'); check(await get(PORT + 1), '1:0');
  49. b.close(); await once(b, 'close');
  50. await waitFor(async () => await get(PORT + 1) === '1:1'); check(await get(PORT), '1:1');
  51. const b2 = peer(PORT + 1); await waitFor(() => b2.messages.length === 1); check(b2.messages, ['open:B']);
  52. await get(PORT, '/stop');
  53. b2.send('survives'); await waitFor(() => b2.messages.length === 2); check(b2.messages, ['open:B', 'B:survives']);
  54. check(await get(PORT + 1, '/broadcast'), '1'); await waitFor(() => b2.messages.length === 3);
  55. check(b2.messages, ['open:B', 'B:survives', 'broadcast:B']);
  56. console.log(`ok two-ws-servers: ${checks} checks`);
  57. } finally {
  58. for (const p of peers) p.close();
  59. if (server.exitCode === null) {
  60. const stopped = once(server, 'exit');
  61. assert.equal(readlinkSync(`/proc/${server.pid}/cwd`), ROOT);
  62. spawnSync('ss', ['-ltnp', `( sport = ${PORT} or sport = ${PORT + 1} )`]);
  63. server.kill('SIGTERM');
  64. await stopped;
  65. }
  66. }

Branches

Latest commits

  • 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre