process-guard.test.js
6.24 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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { EventEmitter } from 'events';
const { ProcessGuard, FRESH_DEFAULTS, RESUME_DEFAULTS } = require('../../engine/process-guard');
/** 构造一个假的 ChildProcess */
function createMockProc() {
const proc = new EventEmitter();
proc.killed = false;
proc.kill = vi.fn((signal) => {
if (signal === 'SIGKILL') proc.killed = true;
});
proc.pid = 12345;
return proc;
}
describe('ProcessGuard', () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
describe('Overall Timeout', () => {
it('should kill process when overall timeout is reached', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, { overallTimeoutMs: 5000 });
const timeoutPromise = new Promise((resolve) => {
guard.on('timeout', resolve);
});
await vi.advanceTimersByTimeAsync(5000);
const event = await timeoutPromise;
expect(event.reason).toBe('overall-timeout');
expect(guard.wasKilled).toBe(true);
expect(proc.kill).toHaveBeenCalledWith('SIGTERM');
});
it('should not trigger if cancelled before timeout', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, { overallTimeoutMs: 5000 });
await vi.advanceTimersByTimeAsync(3000);
guard.cancel();
await vi.advanceTimersByTimeAsync(5000);
expect(guard.wasKilled).toBe(false);
expect(proc.kill).not.toHaveBeenCalled();
});
});
describe('Watchdog (No-Output) Timeout', () => {
it('should kill process when no output for watchdog duration', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
noOutputTimeoutMs: 3000,
});
const timeoutPromise = new Promise((resolve) => {
guard.on('timeout', resolve);
});
await vi.advanceTimersByTimeAsync(3000);
const event = await timeoutPromise;
expect(event.reason).toBe('no-output-timeout');
expect(guard.wasKilled).toBe(true);
});
it('should reset watchdog on touch()', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
noOutputTimeoutMs: 3000,
});
// 2s 后 touch → 从这里重新倒计 3s
await vi.advanceTimersByTimeAsync(2000);
guard.touch();
// 再过 2s(总共 4s)→ 还没超时
await vi.advanceTimersByTimeAsync(2000);
expect(guard.wasKilled).toBe(false);
// 再过 1.5s(总共 5.5s, touch 后 3.5s)→ 超时
const timeoutPromise = new Promise((resolve) => {
guard.on('timeout', resolve);
});
await vi.advanceTimersByTimeAsync(1500);
const event = await timeoutPromise;
expect(event.reason).toBe('no-output-timeout');
});
it('should not trigger watchdog if continuously touched', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
noOutputTimeoutMs: 3000,
});
// 模拟每秒收到输出
for (let i = 0; i < 10; i++) {
await vi.advanceTimersByTimeAsync(1000);
guard.touch();
}
expect(guard.wasKilled).toBe(false);
guard.cancel();
});
});
describe('Watchdog Profile Computation', () => {
it('should use FRESH_DEFAULTS for new sessions', () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
isResume: false,
});
// 600k * 0.8 = 480k, clamped by max 600k → 480k
expect(guard.noOutputTimeoutMs).toBe(480_000);
guard.cancel();
});
it('should use RESUME_DEFAULTS for resume sessions', () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
isResume: true,
});
// 600k * 0.3 = 180k, clamped [60k, 180k] → 180k
expect(guard.noOutputTimeoutMs).toBe(180_000);
guard.cancel();
});
it('should clamp to minMs for short overall timeout', () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 60_000,
isResume: false,
});
// 60k * 0.8 = 48k, below minMs 180k → 180k
expect(guard.noOutputTimeoutMs).toBe(180_000);
guard.cancel();
});
it('should allow explicit noOutputTimeoutMs override', () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
noOutputTimeoutMs: 10_000,
});
expect(guard.noOutputTimeoutMs).toBe(10_000);
guard.cancel();
});
});
describe('Process Kill Sequence', () => {
it('should SIGTERM first, then SIGKILL after 2s', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 1000,
});
await vi.advanceTimersByTimeAsync(1000);
// SIGTERM 已发送
expect(proc.kill).toHaveBeenCalledWith('SIGTERM');
expect(proc.kill).not.toHaveBeenCalledWith('SIGKILL');
// 2s 后 SIGKILL
await vi.advanceTimersByTimeAsync(2000);
expect(proc.kill).toHaveBeenCalledWith('SIGKILL');
});
});
describe('Cleanup on Process Exit', () => {
it('should clear all timers when process closes', async () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, {
overallTimeoutMs: 600_000,
noOutputTimeoutMs: 3000,
});
// 进程提前退出
proc.emit('close', 0);
// 推进超过所有超时 → 不应触发任何 kill
await vi.advanceTimersByTimeAsync(700_000);
expect(guard.wasKilled).toBe(false);
expect(proc.kill).not.toHaveBeenCalled();
});
});
describe('Manual Terminate', () => {
it('should allow manual termination', () => {
const proc = createMockProc();
const guard = new ProcessGuard(proc, { overallTimeoutMs: 600_000 });
guard.terminate();
expect(guard.wasKilled).toBe(true);
expect(guard.terminationReason).toBe('manual-cancel');
expect(proc.kill).toHaveBeenCalledWith('SIGTERM');
});
});
});