|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +SRC=$(cd $(dirname "$0"); pwd) |
| 4 | +source "${SRC}/../include.sh" |
| 5 | + |
| 6 | +fixture="$file_path/nub-loader" |
| 7 | +log_file="$fixture/loader.log" |
| 8 | + |
| 9 | +cleanup() { |
| 10 | + $pm2 delete all >/dev/null 2>&1 || true |
| 11 | + $pm2 kill >/dev/null 2>&1 || true |
| 12 | +} |
| 13 | +trap cleanup EXIT |
| 14 | + |
| 15 | +pids_for() { |
| 16 | + $pm2 jlist | $node -e ' |
| 17 | + let input = ""; |
| 18 | + process.stdin.on("data", (chunk) => input += chunk); |
| 19 | + process.stdin.on("end", () => { |
| 20 | + console.log(JSON.parse(input) |
| 21 | + .filter((app) => app.name === "nub-loader-cluster") |
| 22 | + .map((app) => app.pid) |
| 23 | + .sort((left, right) => left - right) |
| 24 | + .join(" ")); |
| 25 | + }); |
| 26 | + ' |
| 27 | +} |
| 28 | + |
| 29 | +cd "$fixture" |
| 30 | +> "$log_file" |
| 31 | + |
| 32 | +$pm2 start ecosystem.config.cjs |
| 33 | +should 'should start the TypeScript fork and cluster apps' 'online' 3 |
| 34 | + |
| 35 | +expected_node=$($node -p 'process.versions.node') |
| 36 | +grep -F '"mode":"pm2"' "$log_file" |
| 37 | +spec 'Should transform non-erasable TypeScript syntax' |
| 38 | +grep -F "\"cwd\":\"$fixture\"" "$log_file" |
| 39 | +spec 'Should resolve the loader from the application cwd' |
| 40 | +grep -F "\"node\":\"$expected_node\"" "$log_file" |
| 41 | +spec 'Should run workers on the daemon Node version' |
| 42 | +grep -F 'service.ts' "$log_file" |
| 43 | +spec 'Should preserve TypeScript source maps' |
| 44 | + |
| 45 | +old_cluster_pids=$(pids_for) |
| 46 | +[ "$(echo "$old_cluster_pids" | wc -w)" -eq 2 ] |
| 47 | +spec 'Should record both cluster worker PIDs before reload' |
| 48 | + |
| 49 | +PM2_BIN="$pm2" $node <<'NODE' |
| 50 | +const { spawn } = require('child_process'); |
| 51 | +const http = require('http'); |
| 52 | +
|
| 53 | +let failures = 0; |
| 54 | +let pending = 0; |
| 55 | +
|
| 56 | +function request() { |
| 57 | + pending += 1; |
| 58 | + const request = http.get('http://127.0.0.1:45678', (response) => { |
| 59 | + if (response.statusCode !== 200) |
| 60 | + failures += 1; |
| 61 | + response.resume(); |
| 62 | + response.once('end', () => pending -= 1); |
| 63 | + }); |
| 64 | + request.setTimeout(1000, () => request.destroy()); |
| 65 | + request.once('error', () => { |
| 66 | + failures += 1; |
| 67 | + pending -= 1; |
| 68 | + }); |
| 69 | +} |
| 70 | +
|
| 71 | +request(); |
| 72 | +const reload = spawn(process.env.PM2_BIN, ['reload', 'nub-loader-cluster'], { stdio: 'inherit' }); |
| 73 | +const probes = setInterval(request, 10); |
| 74 | +
|
| 75 | +reload.once('close', (code) => { |
| 76 | + clearInterval(probes); |
| 77 | + setTimeout(() => { |
| 78 | + if (code !== 0 || failures > 0 || pending !== 0) |
| 79 | + process.exit(1); |
| 80 | + }, 100); |
| 81 | +}); |
| 82 | +NODE |
| 83 | +spec 'Should serve requests without interruption during cluster reload' |
| 84 | + |
| 85 | +should 'should keep both TypeScript cluster workers online after reload' 'online' 3 |
| 86 | + |
| 87 | +new_cluster_pids=$(pids_for) |
| 88 | +[ "$old_cluster_pids" != "$new_cluster_pids" ] |
| 89 | +spec 'Should replace cluster workers after readiness reload' |
| 90 | +grep -F '"event":"shutdown"' "$log_file" |
| 91 | +spec 'Should deliver graceful shutdown to replaced workers' |
| 92 | + |
| 93 | +$pm2 delete all |
| 94 | +sleep 1 |
| 95 | +for pid in $old_cluster_pids $new_cluster_pids; do |
| 96 | + ! kill -0 "$pid" 2>/dev/null |
| 97 | +done |
| 98 | +spec 'Should not leave replaced cluster workers running' |
0 commit comments