Repository navigation
Expand file tree
/
Copy pathffiRuntimeHost.test.ts
More file actions
246 lines (213 loc) · 9.26 KB
/
Copy pathffiRuntimeHost.test.ts
File metadata and controls
246 lines (213 loc) · 9.26 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const ffi = vi.hoisted(() => {
let registeredCallback:
| ((userData: unknown, bytesPtr: unknown, bytesLen: number) => void)
| undefined;
const callbackToken = {};
const hostStart = Object.assign(vi.fn(), {
async: vi.fn(
(
_argv: Buffer,
_argvLength: number,
_env: Buffer | null,
_envLength: number,
callback: (error: Error | null, result: number) => void
) => callback(null, 11)
),
});
const hostShutdown = vi.fn(() => true);
const connectionOpen = vi.fn(() => 21);
const connectionWrite = vi.fn(() => true);
const connectionClose = Object.assign(vi.fn<() => boolean>(), {
async: vi.fn<
(connectionId: number, callback: (error: Error | null, result: boolean) => void) => void
>(),
});
const register = vi.fn(
(callback: (userData: unknown, bytesPtr: unknown, bytesLen: number) => void) => {
registeredCallback = callback;
return callbackToken;
}
);
const unregister = vi.fn();
return {
callbackToken,
connectionClose,
connectionOpen,
connectionWrite,
getRegisteredCallback: () => registeredCallback,
hostShutdown,
hostStart,
register,
unregister,
};
});
vi.mock("node:fs", () => ({
existsSync: vi.fn(() => true),
}));
vi.mock("koffi", () => ({
default: {
array: vi.fn(() => ({})),
decode: vi.fn(() => new Uint8Array([1])),
load: vi.fn(() => ({
func: vi.fn((name: string) => {
if (name.endsWith("host_start")) return ffi.hostStart;
if (name.endsWith("host_shutdown")) return ffi.hostShutdown;
if (name.endsWith("connection_open")) return ffi.connectionOpen;
if (name.endsWith("connection_write")) return ffi.connectionWrite;
if (name.endsWith("connection_close")) return ffi.connectionClose;
throw new Error(`Unexpected FFI symbol: ${name}`);
}),
})),
pointer: vi.fn(() => ({})),
proto: vi.fn(() => ({})),
register: ffi.register,
unregister: ffi.unregister,
},
}));
import { FfiRuntimeHost } from "../src/ffiRuntimeHost.js";
describe("FfiRuntimeHost callback cleanup", () => {
beforeEach(() => {
vi.useFakeTimers();
ffi.connectionClose.mockReset();
ffi.connectionClose.async
.mockReset()
.mockImplementation((_id, callback) => callback(null, true));
ffi.connectionOpen.mockClear();
ffi.hostShutdown.mockClear();
ffi.hostStart.mockClear();
ffi.hostStart.async.mockClear();
ffi.register.mockClear();
ffi.unregister.mockClear();
});
afterEach(() => {
expect(ffi.connectionClose).not.toHaveBeenCalled();
vi.clearAllTimers();
vi.useRealTimers();
});
it("finishes disposal after a false close while retaining resources for the detached retry", async () => {
ffi.connectionClose.async.mockImplementationOnce((_id, callback) => callback(null, false));
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
await host.dispose();
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(1);
expect(ffi.unregister).not.toHaveBeenCalled();
expect(ffi.hostShutdown).not.toHaveBeenCalled();
expect((host as any).outboundCallback).toBe(ffi.callbackToken);
expect((host as any).keepAliveTimer).toBeDefined();
await vi.advanceTimersByTimeAsync(100);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(2);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
expect((host as any).outboundCallback).toBeUndefined();
expect((host as any).keepAliveTimer).toBeUndefined();
expect(vi.getTimerCount()).toBe(0);
await host.dispose();
await vi.advanceTimersByTimeAsync(100);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(2);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
});
it("waits for successful initial cleanup without overlapping close calls", async () => {
let finishClose!: (error: Error | null, result: boolean) => void;
ffi.connectionClose.async.mockImplementationOnce((_id, callback) => {
finishClose = callback;
});
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
let disposed = false;
const cleanup = host.dispose().then(() => {
disposed = true;
});
await host.dispose();
await vi.advanceTimersByTimeAsync(1000);
expect(disposed).toBe(false);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(1);
expect(ffi.unregister).not.toHaveBeenCalled();
expect(ffi.hostShutdown).not.toHaveBeenCalled();
expect((host as any).outboundCallback).toBe(ffi.callbackToken);
expect((host as any).keepAliveTimer).toBeDefined();
finishClose(null, true);
await cleanup;
expect(disposed).toBe(true);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
});
it("defers reclamation when dispose is called from the outbound callback", async () => {
ffi.connectionClose.async.mockImplementationOnce((_id, callback) => {
setImmediate(() => callback(null, true));
});
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
host.receiveStream.once("data", () => {
void host.dispose();
});
ffi.getRegisteredCallback()?.(null, {}, 1);
expect(ffi.unregister).not.toHaveBeenCalled();
expect((host as any).outboundCallback).toBe(ffi.callbackToken);
await vi.advanceTimersByTimeAsync(100);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
});
it.each(["callback", "throw"])(
"quarantines the callback on a close %s error",
async (failure) => {
const closeError = new Error("close failed");
ffi.connectionClose.async.mockImplementationOnce((_id, callback) => {
if (failure === "throw") {
throw closeError;
}
callback(closeError, false);
});
const error = vi.spyOn(console, "error").mockImplementation(() => {});
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
await host.dispose();
await vi.advanceTimersByTimeAsync(500);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(1);
expect(ffi.unregister).not.toHaveBeenCalled();
expect(ffi.hostShutdown).not.toHaveBeenCalled();
expect((host as any).outboundCallback).toBe(ffi.callbackToken);
expect((FfiRuntimeHost as any).quarantinedHosts.has(host)).toBe(true);
expect(error).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
error.mockRestore();
}
);
it("does not retry a terminal host shutdown failure", async () => {
ffi.hostShutdown.mockReturnValueOnce(false);
const error = vi.spyOn(console, "error").mockImplementation(() => {});
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
await host.dispose();
await vi.advanceTimersByTimeAsync(500);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(1);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
error.mockRestore();
});
it("retains the callback token when Koffi unregistration fails", async () => {
ffi.unregister.mockImplementationOnce(() => {
throw new Error("unregister failed");
});
const error = vi.spyOn(console, "error").mockImplementation(() => {});
const host = FfiRuntimeHost.create("runtime.node", undefined, undefined, []);
await host.start();
await host.dispose();
await vi.advanceTimersByTimeAsync(500);
expect(ffi.connectionClose.async).toHaveBeenCalledTimes(1);
expect(ffi.unregister).toHaveBeenCalledTimes(1);
expect(ffi.hostShutdown).toHaveBeenCalledTimes(1);
expect((host as any).outboundCallback).toBe(ffi.callbackToken);
expect((FfiRuntimeHost as any).quarantinedHosts.has(host)).toBe(true);
expect((host as any).keepAliveTimer).toBeUndefined();
expect(vi.getTimerCount()).toBe(0);
await host.dispose();
expect(ffi.unregister).toHaveBeenCalledTimes(1);
error.mockRestore();
});
});