|
| 1 | +/** |
| 2 | + * Chaos test — server randomly drops sockets, sends RST_STREAM, GOAWAY, or |
| 3 | + * delays headers. Asserts the client survives. |
| 4 | + * |
| 5 | + * Run: `npx tsx loadtest/h2-chaos.ts [durationSeconds=60]` |
| 6 | + */ |
| 7 | +import http2 from 'node:http2'; |
| 8 | +import { execFileSync } from 'node:child_process'; |
| 9 | +import fs from 'node:fs'; |
| 10 | +import os from 'node:os'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { createH2Fetch } from '../src/lib/h2-transport/index'; |
| 13 | + |
| 14 | +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
| 15 | + |
| 16 | +function makeCerts() { |
| 17 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'h2-chaos-')); |
| 18 | + const key = path.join(tmp, 'key.pem'); |
| 19 | + const cert = path.join(tmp, 'cert.pem'); |
| 20 | + execFileSync( |
| 21 | + 'openssl', |
| 22 | + [ |
| 23 | + 'req', |
| 24 | + '-x509', |
| 25 | + '-newkey', |
| 26 | + 'rsa:2048', |
| 27 | + '-keyout', |
| 28 | + key, |
| 29 | + '-out', |
| 30 | + cert, |
| 31 | + '-days', |
| 32 | + '1', |
| 33 | + '-nodes', |
| 34 | + '-subj', |
| 35 | + '/CN=localhost', |
| 36 | + ], |
| 37 | + { stdio: ['ignore', 'ignore', 'ignore'] }, |
| 38 | + ); |
| 39 | + return { key: fs.readFileSync(key), cert: fs.readFileSync(cert), tmp }; |
| 40 | +} |
| 41 | + |
| 42 | +async function startServer(seed: number) { |
| 43 | + const { key, cert, tmp } = makeCerts(); |
| 44 | + let rng = seed >>> 0; |
| 45 | + const next = () => { |
| 46 | + rng = (rng * 1664525 + 1013904223) >>> 0; |
| 47 | + return rng / 0xffffffff; |
| 48 | + }; |
| 49 | + const server = http2.createSecureServer({ key, cert }); |
| 50 | + server.on('stream', (stream) => { |
| 51 | + stream.on('error', () => {}); |
| 52 | + const r = next(); |
| 53 | + if (r < 0.1) { |
| 54 | + try { |
| 55 | + stream.session?.socket?.destroy(); |
| 56 | + } catch {} |
| 57 | + return; |
| 58 | + } |
| 59 | + if (r < 0.2) { |
| 60 | + try { |
| 61 | + stream.close(0x8); |
| 62 | + } catch {} |
| 63 | + return; |
| 64 | + } |
| 65 | + if (r < 0.21) { |
| 66 | + stream.respond({ ':status': 200 }); |
| 67 | + stream.end('ok'); |
| 68 | + try { |
| 69 | + stream.session?.goaway(); |
| 70 | + } catch {} |
| 71 | + return; |
| 72 | + } |
| 73 | + const delay = r < 0.4 ? Math.floor(next() * 200) : 0; |
| 74 | + setTimeout(() => { |
| 75 | + if (stream.destroyed) return; |
| 76 | + stream.respond({ ':status': 200 }); |
| 77 | + stream.end('ok'); |
| 78 | + }, delay); |
| 79 | + }); |
| 80 | + return new Promise<{ port: number; close: () => void }>((resolve) => { |
| 81 | + server.listen(0, () => { |
| 82 | + resolve({ |
| 83 | + port: (server.address() as any).port, |
| 84 | + close: () => { |
| 85 | + server.close(); |
| 86 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 87 | + }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +async function main() { |
| 94 | + const durationSec = Number(process.argv[2] ?? 60); |
| 95 | + if (!Number.isFinite(durationSec) || durationSec <= 0) { |
| 96 | + console.error(`invalid duration: ${process.argv[2]} (must be a positive number of seconds)`); |
| 97 | + process.exit(2); |
| 98 | + } |
| 99 | + const server = await startServer(42); |
| 100 | + const fetch = createH2Fetch({ |
| 101 | + minConnections: 4, |
| 102 | + maxConnections: 20, |
| 103 | + connectTimeout: 5_000, |
| 104 | + tlsOptions: { rejectUnauthorized: false }, |
| 105 | + }); |
| 106 | + |
| 107 | + const end = Date.now() + durationSec * 1000; |
| 108 | + let getOk = 0, |
| 109 | + getFail = 0, |
| 110 | + postOk = 0, |
| 111 | + postFail = 0; |
| 112 | + |
| 113 | + async function get() { |
| 114 | + try { |
| 115 | + const r = (await fetch(`https://localhost:${server.port}/x`, { method: 'GET' } as any)) as any; |
| 116 | + await r.text(); |
| 117 | + getOk++; |
| 118 | + } catch { |
| 119 | + getFail++; |
| 120 | + } |
| 121 | + } |
| 122 | + async function post() { |
| 123 | + try { |
| 124 | + const r = (await fetch(`https://localhost:${server.port}/x`, { |
| 125 | + method: 'POST', |
| 126 | + body: 'b', |
| 127 | + } as any)) as any; |
| 128 | + await r.text(); |
| 129 | + postOk++; |
| 130 | + } catch { |
| 131 | + postFail++; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + while (Date.now() < end) { |
| 136 | + await Promise.all([...Array.from({ length: 10 }, get), ...Array.from({ length: 5 }, post)]); |
| 137 | + } |
| 138 | + |
| 139 | + await fetch.close(); |
| 140 | + server.close(); |
| 141 | + |
| 142 | + const getTotal = getOk + getFail; |
| 143 | + const postTotal = postOk + postFail; |
| 144 | + if (getTotal === 0) { |
| 145 | + console.error('no GET requests were issued — load harness produced zero coverage'); |
| 146 | + process.exit(1); |
| 147 | + } |
| 148 | + const getRate = getOk / getTotal; |
| 149 | + const postClean = postFail === 0 || postOk > 0; |
| 150 | + |
| 151 | + console.log( |
| 152 | + JSON.stringify( |
| 153 | + { |
| 154 | + getOk, |
| 155 | + getFail, |
| 156 | + getTotal, |
| 157 | + getSuccessRate: Number(getRate.toFixed(3)), |
| 158 | + postOk, |
| 159 | + postFail, |
| 160 | + postTotal, |
| 161 | + durationSec, |
| 162 | + }, |
| 163 | + null, |
| 164 | + 2, |
| 165 | + ), |
| 166 | + ); |
| 167 | + |
| 168 | + if (getRate < 0.5) { |
| 169 | + console.error('GET success rate too low'); |
| 170 | + process.exit(1); |
| 171 | + } |
| 172 | + if (!postClean) { |
| 173 | + console.error('POST never succeeded'); |
| 174 | + process.exit(1); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +main().catch((err) => { |
| 179 | + console.error(err); |
| 180 | + process.exit(1); |
| 181 | +}); |
0 commit comments