jukebox
All repositories: gitoria
4.1 KB
// #285: exercise real sockets through the run form and a native executable.import { spawn, execFileSync } from 'node:child_process';import { once } from 'node:events';import { mkdtempSync, rmSync, readlinkSync } from 'node:fs';import { tmpdir } from 'node:os';import { resolve, join, dirname } from 'node:path';import { fileURLToPath } from 'node:url';import assert from 'node:assert/strict';const here = dirname(fileURLToPath(import.meta.url));const root = resolve(here, '../../..');const bin = process.env.HL_BIN || join(root, 'native/zig-out/bin/hybriel');const fixture = join(here, 'on-error-answers.hl');const port = Number(process.env.HL_TEST_PORT || 31910);const work = mkdtempSync(join(tmpdir(), 'on-error-answers-'));let checks = 0;const sleep = ms => new Promise(r => setTimeout(r, ms));try {const native = join(work, 'server');execFileSync(bin, ['--native', fixture, '-o', native, '--quiet'], { cwd: root, timeout: 180000 });for (const [name, cmd, args] of [['run', bin, [fixture]], ['native', native, []]]) {const child = spawn(cmd, args, { cwd: root, env: { ...process.env, HL_TEST_PORT: String(port) }, stdio: ['ignore', 'pipe', 'pipe'] });const exited = once(child, 'exit');let log = '';child.stdout.on('data', d => { log += d });child.stderr.on('data', d => { log += d });const request = async path => {const r = await fetch(`http://127.0.0.1:${port}${path}`, { signal: AbortSignal.timeout(3000) });return { status: r.status, body: await r.text(), type: r.headers.get('content-type'), fallback: r.headers.get('x-fallback') };};try {let up = false;for (let i = 0; i < 150; i++) {if (child.exitCode !== null) break;try { if ((await request('/health')).body === 'healthy') { up = true; break } } catch {}await sleep(100);}assert.ok(up, `${name} failed to boot: ${log}`);for (const [path, status, body, type] of [['/text', 200, 'fallback', 'text/plain'],['/json', 200, '{"ok":true,"items":[1,2]}', 'application/json'],['/status', 503, 'unavailable', null],['/bytes', 200, 'raw', 'application/octet-stream'],['/empty', 200, '', 'text/plain'],['/false', 200, 'false', 'application/json'],['/zero', 200, '0', 'application/json'],['/sent', 202, 'sent', null],]) {const r = await request(path);assert.equal(r.status, status, `${name} ${path}`);assert.equal(r.body, body, `${name} ${path}`);if (type) assert.ok(r.type?.startsWith(type), `${name} ${path} content type: ${r.type}`);if (path === '/status') assert.equal(r.fallback, 'yes');checks++;}for (const path of ['/null', '/none']) {const r = await request(path);assert.equal(r.status, 500);assert.match(r.body, /route:/);assert.match(r.body, /on-error-answers.hl:/);checks++;}assert.equal((await request('/health')).body, 'healthy');assert.doesNotMatch(log, /UNREACHABLE/);assert.equal((log.match(/^hook$/gm) || []).length, 10);assert.equal((log.match(/^handled$/gm) || []).length, 10);checks++;await request('/fatal').catch(() => {});const [code] = await Promise.race([exited, sleep(5000).then(() => { throw new Error('fatal handler did not stop') })]);assert.equal(code, 1, log);assert.match(log, /error-handler-fault/);checks++;console.log(`${name}: 12/12`);} finally {if (child.exitCode === null && child.signalCode === null) {// Only our child, and verify its cwd before signalling it.const sockets = execFileSync('ss', ['-ltnp', `sport = :${port}`], { encoding: 'utf8' });if (sockets.includes('LISTEN')) assert.ok(sockets.includes(`pid=${child.pid},`), 'listener belongs to our child');let cwd;try { cwd = readlinkSync(`/proc/${child.pid}/cwd`) }catch (e) { if (e.code !== 'ENOENT') throw e }if (cwd !== undefined) { assert.equal(cwd, root); child.kill('SIGTERM') }await exited;}}}console.log(`on-error-answers: ${checks}/24`);} finally { rmSync(work, { recursive: true, force: true }) }
Branches
Latest commits
- dcca21abjukebox#8 Move copies and queue logic into their topicsmre
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 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