|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +SRC=$(cd $(dirname "$0"); pwd) |
| 4 | +fixture=$(cd "$SRC/../../fixtures/nub-loader"; pwd) |
| 5 | +export PM2_HOME="$fixture/.pm2" |
| 6 | +source "${SRC}/../include.sh" |
| 7 | + |
| 8 | +log_file="$fixture/loader.log" |
| 9 | + |
| 10 | +cleanup() { |
| 11 | + $pm2 delete all >/dev/null 2>&1 || true |
| 12 | + $pm2 kill >/dev/null 2>&1 || true |
| 13 | +} |
| 14 | +trap cleanup EXIT |
| 15 | + |
| 16 | +pids_for() { |
| 17 | + $pm2 jlist | $node -e ' |
| 18 | + let input = ""; |
| 19 | + process.stdin.on("data", (chunk) => input += chunk); |
| 20 | + process.stdin.on("end", () => { |
| 21 | + console.log(JSON.parse(input) |
| 22 | + .filter((app) => app.name === "nub-loader-cluster") |
| 23 | + .map((app) => app.pid) |
| 24 | + .sort((left, right) => left - right) |
| 25 | + .join(" ")); |
| 26 | + }); |
| 27 | + ' |
| 28 | +} |
| 29 | + |
| 30 | +probe_reload() { |
| 31 | + PM2_BIN="$pm2" APP_NAME="$1" PORT="$2" $node <<'NODE' |
| 32 | +const { spawn } = require('child_process'); |
| 33 | +const http = require('http'); |
| 34 | +
|
| 35 | +let failures = 0; |
| 36 | +let pending = 0; |
| 37 | +
|
| 38 | +function request() { |
| 39 | + pending += 1; |
| 40 | + let settled = false; |
| 41 | + const complete = () => { |
| 42 | + if (settled) |
| 43 | + return; |
| 44 | + settled = true; |
| 45 | + pending -= 1; |
| 46 | + }; |
| 47 | + const request = http.get(`http://127.0.0.1:${process.env.PORT}`, (response) => { |
| 48 | + if (response.statusCode !== 200) |
| 49 | + failures += 1; |
| 50 | + response.resume(); |
| 51 | + response.once('end', complete); |
| 52 | + }); |
| 53 | + request.setTimeout(1000, () => request.destroy()); |
| 54 | + request.once('error', () => { |
| 55 | + failures += 1; |
| 56 | + complete(); |
| 57 | + }); |
| 58 | +} |
| 59 | +
|
| 60 | +request(); |
| 61 | +const reload = spawn(process.env.PM2_BIN, ['reload', process.env.APP_NAME], { stdio: 'inherit' }); |
| 62 | +const probes = setInterval(request, 10); |
| 63 | +
|
| 64 | +reload.once('close', (code) => { |
| 65 | + clearInterval(probes); |
| 66 | + const deadline = Date.now() + 2000; |
| 67 | + const finish = () => { |
| 68 | + if (pending > 0 && Date.now() < deadline) { |
| 69 | + setTimeout(finish, 10); |
| 70 | + return; |
| 71 | + } |
| 72 | + if (code !== 0 || failures > 0 || pending !== 0) { |
| 73 | + console.error({ code, failures, pending }); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + }; |
| 77 | + setTimeout(finish, 50); |
| 78 | +}); |
| 79 | +NODE |
| 80 | +} |
| 81 | + |
| 82 | +cd "$fixture" |
| 83 | +> "$log_file" |
| 84 | + |
| 85 | +$pm2 start ecosystem.config.cjs |
| 86 | +should 'should start the TypeScript fork and cluster apps' 'online' 5 |
| 87 | + |
| 88 | +expected_node=$($node -p 'process.versions.node') |
| 89 | +! grep -F 'TypeScript support unavailable' "$log_file" |
| 90 | +spec 'Should not request ts-node when the loader is preloaded' |
| 91 | +ready_count=$(grep -Fc '"event":"ready"' "$log_file") |
| 92 | +[ "$ready_count" -eq 3 ] |
| 93 | +spec 'Should receive readiness from every fork and cluster worker' |
| 94 | +grep -F '"mode":"pm2"' "$log_file" |
| 95 | +spec 'Should transform non-erasable TypeScript syntax' |
| 96 | +grep -F "\"cwd\":\"$fixture\"" "$log_file" |
| 97 | +spec 'Should resolve the loader from the application cwd' |
| 98 | +grep -F "\"node\":\"$expected_node\"" "$log_file" |
| 99 | +spec 'Should run workers on the daemon Node version' |
| 100 | +grep -E '"sourceMapFrame":".*service\.ts:7:' "$log_file" |
| 101 | +spec 'Should preserve TypeScript source maps' |
| 102 | + |
| 103 | +old_cluster_pids=$(pids_for) |
| 104 | +[ "$(echo "$old_cluster_pids" | wc -w)" -eq 2 ] |
| 105 | +spec 'Should record both cluster worker PIDs before reload' |
| 106 | + |
| 107 | +probe_reload nub-loader-cluster 45679 |
| 108 | +spec 'Should serve requests without interruption during cluster reload' |
| 109 | + |
| 110 | +should 'should keep both TypeScript cluster workers online after reload' 'online' 5 |
| 111 | + |
| 112 | +probe_reload plain-js-cluster 45680 |
| 113 | +spec 'Should keep a plain JavaScript cluster available during reload' |
| 114 | + |
| 115 | +new_cluster_pids=$(pids_for) |
| 116 | +[ "$old_cluster_pids" != "$new_cluster_pids" ] |
| 117 | +spec 'Should replace cluster workers after readiness reload' |
| 118 | +grep -F '"event":"shutdown"' "$log_file" |
| 119 | +spec 'Should deliver graceful shutdown to replaced workers' |
| 120 | + |
| 121 | +$pm2 delete all |
| 122 | +sleep 1 |
| 123 | +remaining_workers=0 |
| 124 | +for pid in $old_cluster_pids $new_cluster_pids; do |
| 125 | + if kill -0 "$pid" 2>/dev/null; then |
| 126 | + remaining_workers=1 |
| 127 | + fi |
| 128 | +done |
| 129 | +[ "$remaining_workers" -eq 0 ] |
| 130 | +spec 'Should not leave replaced cluster workers running' |
0 commit comments