-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest06ComputeStartupFromStore.js
More file actions
184 lines (163 loc) · 5.48 KB
/
Copy pathtest06ComputeStartupFromStore.js
File metadata and controls
184 lines (163 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { spawn } from 'child_process';
import fs from 'fs';
import net from 'net';
import path from 'path';
import { fileURLToPath } from 'url';
import fetch from 'node-fetch';
import { buildEnvelope } from '../shared/envelope.js';
import { buildDummyRtmsWebhook, getStopEvent } from './dummyRtms.js';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const dataDir = path.join(repoRoot, '.data', `test-compute-startup-${process.pid}`);
const children = [];
let shuttingDown = false;
try {
const storePort = await getFreePort();
const computePort = await getFreePort();
const storeUrl = `http://127.0.0.1:${storePort}`;
const streamId = `startup-load-${Date.now()}`;
const rtmsId = `startup-rtms-${Date.now()}`;
children.push(spawnService('05-control-store/server.js', {
STORE_ROLE: 'regional',
STORE_REGION: 'amer-east',
CENTRAL_PORT: String(storePort),
CONTROL_DATA_DIR: dataDir
}));
await waitForJson(`${storeUrl}/health`, 'regional store health');
console.log(`PASS regional_store_ready port=${storePort}`);
const startWebhook = buildDummyRtmsWebhook({ region: 'IAD', streamId, rtmsId });
const startEnvelope = buildEnvelope(
startWebhook.event,
startWebhook.payload,
'test06-compute-startup',
startWebhook
);
await putJson(`${storeUrl}/streams/${encodeURIComponent(streamId)}/route`, {
regionCode: 'amer-east',
selectedRegionCode: 'IAD',
productType: startEnvelope.productType,
rtmsId: startEnvelope.rtmsId,
envelope: startEnvelope,
webhook: startWebhook
});
console.log(`PASS start_envelope_saved stream=${streamId}`);
children.push(spawnService('04-regional-compute-job/server.js', {
SPOKE_REGION: 'amer-east',
SPOKE_NODE_ID: 'test-startup-compute',
COMPUTE_PORT: String(computePort),
REGIONAL_STORE_URL: storeUrl,
CENTRAL_STORE_URL: storeUrl,
RTMS_STREAM_ID: streamId,
RTMS_ENVELOPE_REF: `regional-store:/streams/${streamId}`,
LEASE_RENEW_INTERVAL_MS: '500',
LEASE_TTL_MS: '5000',
DRY_RUN: 'true'
}));
await waitForCondition(async () => {
const stream = await fetchStream(storeUrl, streamId);
return stream?.state === 'dry_run_connected' && stream?.ownerNodeId === 'test-startup-compute';
}, 'compute loaded start envelope');
console.log(`PASS compute_loaded_full_start_webhook stream=${streamId}`);
const stopWebhook = buildDummyRtmsWebhook({
event: getStopEvent(startWebhook.event),
streamId,
rtmsId
});
const stopEnvelope = buildEnvelope(
stopWebhook.event,
stopWebhook.payload,
'test06-compute-startup',
stopWebhook
);
await postJson(`${storeUrl}/streams/${encodeURIComponent(streamId)}/state`, {
state: 'stop_requested',
stopEnvelope
});
console.log(`PASS stop_envelope_saved stream=${streamId}`);
await waitForCondition(async () => {
const stream = await fetchStream(storeUrl, streamId);
return stream?.state === 'stopped';
}, 'compute handled stored stop envelope');
console.log(`PASS compute_handled_full_stop_webhook stream=${streamId}`);
console.log('06 compute startup from store tester passed: 5/5');
} finally {
shuttingDown = true;
for (const child of children.reverse()) {
child.kill('SIGTERM');
}
fs.rmSync(dataDir, { recursive: true, force: true });
}
function spawnService(script, env) {
const child = spawn(process.execPath, [path.join(repoRoot, script)], {
cwd: repoRoot,
env: {
...process.env,
...env
},
stdio: ['ignore', 'pipe', 'pipe']
});
child.stdout.on('data', (chunk) => {
if (process.env.TEST_VERBOSE) process.stdout.write(chunk);
});
child.stderr.on('data', (chunk) => {
if (process.env.TEST_VERBOSE) process.stderr.write(chunk);
});
child.on('exit', (code, signal) => {
if (!shuttingDown && code !== 0) {
console.error(`[test06] ${script} exited code=${code} signal=${signal || ''}`);
}
});
return child;
}
async function getFreePort() {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.once('error', reject);
server.listen(0, '127.0.0.1', () => {
const { port } = server.address();
server.close(() => resolve(port));
});
});
}
async function waitForJson(url, label) {
return waitForCondition(async () => {
try {
const response = await fetch(url);
if (!response.ok) return null;
return response.json();
} catch (_error) {
return null;
}
}, label);
}
async function waitForCondition(check, label, attempts = 80) {
for (let i = 0; i < attempts; i += 1) {
const value = await check();
if (value) return value;
await sleep(100);
}
throw new Error(`Timed out waiting for ${label}`);
}
async function fetchStream(storeUrl, streamId) {
const response = await fetch(`${storeUrl}/streams/${encodeURIComponent(streamId)}`);
if (!response.ok) return null;
return response.json();
}
async function putJson(url, body) {
const response = await fetch(url, {
method: 'PUT',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body)
});
if (!response.ok) throw new Error(`PUT ${url} failed status=${response.status}`);
}
async function postJson(url, body) {
const response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body)
});
if (!response.ok) throw new Error(`POST ${url} failed status=${response.status}`);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}