-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrun-java-daemon-sdk-e2e.ts
More file actions
346 lines (334 loc) · 9.67 KB
/
Copy pathrun-java-daemon-sdk-e2e.ts
File metadata and controls
346 lines (334 loc) · 9.67 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import { spawn, type ChildProcess } from 'node:child_process';
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import {
fakeToolCall,
startFakeOpenAIServer,
} from '../integration-tests/fake-openai-server.js';
// This harness executes the root bundle, which `npm run build` does not refresh.
// Run `npm run build && npm run bundle` from the repository root first.
const root = process.cwd();
const cliBundle = path.join(root, 'dist', 'cli.js');
if (!existsSync(cliBundle)) {
throw new Error(
'Java daemon E2E requires dist/cli.js; run `npm run build && npm run bundle` from the repository root first.',
);
}
const temporary = mkdtempSync(path.join(tmpdir(), 'java-daemon-e2e-'));
const workspace = path.join(temporary, 'workspace');
const testHome = path.join(temporary, 'home');
const expected = 'java daemon e2e complete';
const deadlinePrompt = 'java daemon e2e deadline sentinel';
const cancelPrompt = 'java daemon e2e cancel sentinel';
const teardownPrompt = 'java daemon e2e teardown sentinel';
const directPrompt = 'java daemon e2e direct response';
const token = 'java-daemon-e2e-token';
const javaTestTimeoutMs = 5 * 60_000;
mkdirSync(workspace, { recursive: true });
mkdirSync(path.join(testHome, '.qwen'), { recursive: true });
writeFileSync(
path.join(testHome, '.qwen', 'settings.json'),
JSON.stringify({ ui: { enableFollowupSuggestions: false } }),
);
const fake = await startFakeOpenAIServer(({ body }) => {
const messageList = Array.isArray(body['messages']) ? body['messages'] : [];
const latestUser = [...messageList]
.reverse()
.find(
(message) =>
typeof message === 'object' &&
message !== null &&
(message as Record<string, unknown>)['role'] === 'user',
);
const latestUserText = JSON.stringify(latestUser ?? {});
if (latestUserText.includes(directPrompt)) {
return { content: expected };
}
if (latestUserText.includes(teardownPrompt)) {
return {
toolCalls: [
fakeToolCall('write_file', {
file_path: path.join(workspace, 'teardown.txt'),
content: 'must not be written',
}),
],
};
}
if (
latestUserText.includes(deadlinePrompt) ||
latestUserText.includes(cancelPrompt)
) {
return new Promise<never>(() => {});
}
const messages = JSON.stringify(messageList);
if (!messages.includes('"role":"tool"')) {
return {
toolCalls: [
fakeToolCall('write_file', {
file_path: path.join(workspace, 'created.txt'),
content: 'created by Java daemon E2E',
}),
],
};
}
return { content: expected };
});
const cleanEnvironment = Object.fromEntries(
Object.entries(process.env).filter(
([key]) =>
!/^(https?|all)_proxy$/i.test(key) &&
!/^(qwen|dashscope|openai|anthropic|google|gemini|azure|aws|vertex)_/i.test(
key,
) &&
!/(api_?key|token|secret|password|credentials?)$/i.test(key),
),
);
const daemon = spawn(
process.execPath,
[
cliBundle,
'serve',
'--port',
'0',
'--hostname',
'127.0.0.1',
'--require-auth',
'--prompt-deadline-ms',
'60000',
'--workspace',
workspace,
],
{
cwd: root,
detached: process.platform !== 'win32',
env: {
...cleanEnvironment,
HOME: testHome,
QWEN_HOME: path.join(testHome, '.qwen'),
NO_PROXY: '127.0.0.1,localhost',
no_proxy: '127.0.0.1,localhost',
OPENAI_API_KEY: 'fake-key',
OPENAI_BASE_URL: fake.baseUrl,
OPENAI_MODEL: 'fake-model',
QWEN_MODEL: 'fake-model',
QWEN_SERVER_TOKEN: token,
},
stdio: ['ignore', 'pipe', 'pipe'],
},
);
let stderr = '';
let javaTest: ChildProcess | undefined;
let receivedSignal: NodeJS.Signals | undefined;
daemon.stderr?.on('data', (chunk) => {
stderr += chunk.toString();
});
let rejectSignal: (error: Error) => void = () => {};
const signalFailure = new Promise<never>((_resolve, reject) => {
rejectSignal = reject;
});
const handleSignal = (signal: NodeJS.Signals) => {
if (receivedSignal === undefined) {
receivedSignal = signal;
rejectSignal(new Error(`Java daemon E2E interrupted by ${signal}`));
}
};
const handleSigint = () => handleSignal('SIGINT');
const handleSigterm = () => handleSignal('SIGTERM');
process.once('SIGINT', handleSigint);
process.once('SIGTERM', handleSigterm);
async function stopChild(child: ChildProcess, name: string): Promise<void> {
if (!processTreeExists(child)) return;
signalProcessTree(child, 'SIGTERM');
if (await waitForProcessTreeExit(child, 5_000)) return;
signalProcessTree(child, 'SIGKILL');
if (!(await waitForProcessTreeExit(child, 5_000))) {
throw new Error(`${name} process tree did not exit after SIGKILL`);
}
}
function processTreeExists(child: ChildProcess): boolean {
if (child.pid === undefined) return false;
if (process.platform === 'win32') {
return child.exitCode === null && child.signalCode === null;
}
try {
process.kill(-child.pid, 0);
return true;
} catch (error) {
return (error as NodeJS.ErrnoException).code !== 'ESRCH';
}
}
async function waitForProcessTreeExit(
child: ChildProcess,
timeoutMs: number,
): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (processTreeExists(child)) {
if (Date.now() >= deadline) return false;
await new Promise((resolve) => setTimeout(resolve, 50));
}
return true;
}
function signalProcessTree(child: ChildProcess, signal: NodeJS.Signals): void {
if (process.platform !== 'win32' && child.pid !== undefined) {
try {
process.kill(-child.pid, signal);
return;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ESRCH') return;
}
}
child.kill(signal);
}
let runFailure: unknown;
try {
const port = await Promise.race([
new Promise<number>((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error(`daemon startup timed out\n${stderr}`)),
15_000,
);
let output = '';
daemon.stdout?.on('data', (chunk) => {
output += chunk.toString();
const match = output.match(/listening on http:\/\/127\.0\.0\.1:(\d+)/);
if (!match) return;
clearTimeout(timer);
resolve(Number(match[1]));
});
daemon.once('error', (error) => {
clearTimeout(timer);
reject(error);
});
daemon.once('exit', (code) => {
clearTimeout(timer);
reject(new Error(`daemon exited with ${code}\n${stderr}`));
});
}),
signalFailure,
]);
javaTest = spawn(
'mvn',
[
'--batch-mode',
'--no-transfer-progress',
'-Dgpg.skip=true',
'-Dgroups=daemon-integration',
'-Dtest=DaemonServeE2ETest',
'test',
],
{
cwd: path.join(root, 'packages', 'sdk-java', 'qwencode'),
detached: process.platform !== 'win32',
env: {
...cleanEnvironment,
QWEN_DAEMON_E2E_BASE_URL: `http://127.0.0.1:${port}`,
QWEN_DAEMON_E2E_TOKEN: token,
QWEN_DAEMON_E2E_WORKSPACE: workspace,
QWEN_DAEMON_E2E_EXPECTED_TEXT: expected,
QWEN_DAEMON_E2E_DEADLINE_PROMPT: deadlinePrompt,
QWEN_DAEMON_E2E_CANCEL_PROMPT: cancelPrompt,
QWEN_DAEMON_E2E_TEARDOWN_PROMPT: teardownPrompt,
QWEN_DAEMON_E2E_DIRECT_PROMPT: directPrompt,
},
stdio: 'inherit',
},
);
let timeout: NodeJS.Timeout | undefined;
const result = await Promise.race([
new Promise<number | null>((resolve, reject) => {
javaTest?.once('error', reject);
javaTest?.once('exit', resolve);
}),
new Promise<never>((_resolve, reject) => {
timeout = setTimeout(
() =>
reject(
new Error(
`Java daemon E2E Maven test timed out after ${javaTestTimeoutMs}ms`,
),
),
javaTestTimeoutMs,
);
}),
signalFailure,
]).finally(() => {
if (timeout !== undefined) clearTimeout(timeout);
});
if (result !== 0) {
const logPath = path.join(
testHome,
'.qwen',
'debug',
'daemon',
'daemon.log',
);
let daemonLog = '';
try {
daemonLog = readFileSync(logPath, 'utf8');
} catch {
daemonLog = '(daemon log unavailable)';
}
throw new Error(
`Java E2E failed with ${result}; fake requests=${fake.requests.length}\n${stderr}\n${daemonLog}`,
);
}
if (fake.requests.length === 0) {
throw new Error(
'Java E2E completed without exercising the daemon model path; the integration tests may have been skipped',
);
}
} catch (error) {
runFailure = error;
}
const cleanupFailures: unknown[] = [];
if (javaTest !== undefined) {
try {
await stopChild(javaTest, 'Maven test');
} catch (error) {
cleanupFailures.push(error);
}
}
try {
await stopChild(daemon, 'daemon');
} catch (error) {
cleanupFailures.push(error);
}
try {
await fake.close();
} catch (error) {
cleanupFailures.push(error);
}
process.off('SIGINT', handleSigint);
process.off('SIGTERM', handleSigterm);
if (process.env.QWEN_DAEMON_E2E_KEEP_STATE === '1') {
console.error(`Retained Java daemon E2E state at ${temporary}`);
} else {
try {
rmSync(temporary, { recursive: true, force: true });
} catch (error) {
cleanupFailures.push(error);
}
}
if (receivedSignal !== undefined) {
process.kill(process.pid, receivedSignal);
}
if (runFailure !== undefined && cleanupFailures.length > 0) {
throw new AggregateError(
[runFailure, ...cleanupFailures],
'Java daemon E2E and cleanup both failed',
);
}
if (runFailure !== undefined) {
throw runFailure;
}
if (cleanupFailures.length > 0) {
throw new AggregateError(cleanupFailures, 'Java daemon E2E cleanup failed');
}