gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit95f5faae95f5faae|||PR jukebox#7 Drop needless let declarationsmre95f5faae/plugins/http1/tests/on-error-answers.mjs

4.1 KB

  1. // #285: exercise real sockets through the run form and a native executable.
  2. import { spawn, execFileSync } from 'node:child_process';
  3. import { once } from 'node:events';
  4. import { mkdtempSync, rmSync, readlinkSync } from 'node:fs';
  5. import { tmpdir } from 'node:os';
  6. import { resolve, join, dirname } from 'node:path';
  7. import { fileURLToPath } from 'node:url';
  8. import assert from 'node:assert/strict';
  9. const here = dirname(fileURLToPath(import.meta.url));
  10. const root = resolve(here, '../../..');
  11. const bin = process.env.HL_BIN || join(root, 'native/zig-out/bin/hybriel');
  12. const fixture = join(here, 'on-error-answers.hl');
  13. const port = Number(process.env.HL_TEST_PORT || 31910);
  14. const work = mkdtempSync(join(tmpdir(), 'on-error-answers-'));
  15. let checks = 0;
  16. const sleep = ms => new Promise(r => setTimeout(r, ms));
  17. try {
  18. const native = join(work, 'server');
  19. execFileSync(bin, ['--native', fixture, '-o', native, '--quiet'], { cwd: root, timeout: 180000 });
  20. for (const [name, cmd, args] of [['run', bin, [fixture]], ['native', native, []]]) {
  21. const child = spawn(cmd, args, { cwd: root, env: { ...process.env, HL_TEST_PORT: String(port) }, stdio: ['ignore', 'pipe', 'pipe'] });
  22. const exited = once(child, 'exit');
  23. let log = '';
  24. child.stdout.on('data', d => { log += d });
  25. child.stderr.on('data', d => { log += d });
  26. const request = async path => {
  27. const r = await fetch(`http://127.0.0.1:${port}${path}`, { signal: AbortSignal.timeout(3000) });
  28. return { status: r.status, body: await r.text(), type: r.headers.get('content-type'), fallback: r.headers.get('x-fallback') };
  29. };
  30. try {
  31. let up = false;
  32. for (let i = 0; i < 150; i++) {
  33. if (child.exitCode !== null) break;
  34. try { if ((await request('/health')).body === 'healthy') { up = true; break } } catch {}
  35. await sleep(100);
  36. }
  37. assert.ok(up, `${name} failed to boot: ${log}`);
  38. for (const [path, status, body, type] of [
  39. ['/text', 200, 'fallback', 'text/plain'],
  40. ['/json', 200, '{"ok":true,"items":[1,2]}', 'application/json'],
  41. ['/status', 503, 'unavailable', null],
  42. ['/bytes', 200, 'raw', 'application/octet-stream'],
  43. ['/empty', 200, '', 'text/plain'],
  44. ['/false', 200, 'false', 'application/json'],
  45. ['/zero', 200, '0', 'application/json'],
  46. ['/sent', 202, 'sent', null],
  47. ]) {
  48. const r = await request(path);
  49. assert.equal(r.status, status, `${name} ${path}`);
  50. assert.equal(r.body, body, `${name} ${path}`);
  51. if (type) assert.ok(r.type?.startsWith(type), `${name} ${path} content type: ${r.type}`);
  52. if (path === '/status') assert.equal(r.fallback, 'yes');
  53. checks++;
  54. }
  55. for (const path of ['/null', '/none']) {
  56. const r = await request(path);
  57. assert.equal(r.status, 500);
  58. assert.match(r.body, /route:/);
  59. assert.match(r.body, /on-error-answers.hl:/);
  60. checks++;
  61. }
  62. assert.equal((await request('/health')).body, 'healthy');
  63. assert.doesNotMatch(log, /UNREACHABLE/);
  64. assert.equal((log.match(/^hook$/gm) || []).length, 10);
  65. assert.equal((log.match(/^handled$/gm) || []).length, 10);
  66. checks++;
  67. await request('/fatal').catch(() => {});
  68. const [code] = await Promise.race([exited, sleep(5000).then(() => { throw new Error('fatal handler did not stop') })]);
  69. assert.equal(code, 1, log);
  70. assert.match(log, /error-handler-fault/);
  71. checks++;
  72. console.log(`${name}: 12/12`);
  73. } finally {
  74. if (child.exitCode === null && child.signalCode === null) {
  75. // Only our child, and verify its cwd before signalling it.
  76. const sockets = execFileSync('ss', ['-ltnp', `sport = :${port}`], { encoding: 'utf8' });
  77. if (sockets.includes('LISTEN')) assert.ok(sockets.includes(`pid=${child.pid},`), 'listener belongs to our child');
  78. let cwd;
  79. try { cwd = readlinkSync(`/proc/${child.pid}/cwd`) }
  80. catch (e) { if (e.code !== 'ENOENT') throw e }
  81. if (cwd !== undefined) { assert.equal(cwd, root); child.kill('SIGTERM') }
  82. await exited;
  83. }
  84. }
  85. }
  86. console.log(`on-error-answers: ${checks}/24`);
  87. } finally { rmSync(work, { recursive: true, force: true }) }

Branches

Latest commits

  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox