Repository navigation
Expand file tree
/
Copy pathcoordinator.ts
More file actions
316 lines (284 loc) · 10.4 KB
/
Copy pathcoordinator.ts
File metadata and controls
316 lines (284 loc) · 10.4 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
import { parse as flatParse, stringify as flatStringify } from 'flatted';
import {
DEFAULT_NATIVE_SCRIPT_VITEST_PORT,
NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION,
isNativeScriptVitestWireMessage,
type NativeScriptTestEvent,
type NativeScriptTestEventListener,
type NativeScriptTestEventSource,
type NativeScriptVitestWireMessage,
} from '../protocol.js';
import { createMainThreadWorkerHandle } from './loopback.js';
import type { NativeScriptTestModuleRegistry } from './registry.js';
interface MessageEventLike {
data: unknown;
}
interface ErrorEventLike {
message?: string;
}
type EventHandler<Event> = {
bivarianceHack(event: Event): void;
}['bivarianceHack'];
export interface NativeScriptWorkerHandle {
onmessage: EventHandler<MessageEventLike> | null;
onerror: EventHandler<ErrorEventLike> | null;
postMessage(message: unknown): void;
terminate(): void;
}
export interface NativeScriptWebSocketHandle {
readonly readyState?: number;
onopen: (() => void) | null;
onmessage: ((event: MessageEventLike) => void) | null;
onerror: ((event: ErrorEventLike) => void) | null;
onclose: (() => void) | null;
send(message: string): void;
close(): void;
}
export interface NativeScriptVitestCoordinatorOptions {
/** Required to execute main-thread (UI-capable) specs — slot 0. */
registry?: NativeScriptTestModuleRegistry;
/** Required to execute specs in parallel NativeScript Worker runtimes. */
createWorker?(slot: number): NativeScriptWorkerHandle;
url?: string;
port?: number;
createSocket?: (url: string) => NativeScriptWebSocketHandle;
/** Delay between connection retry cycles across all candidate URLs. */
connectRetryDelay?: number;
maxConnectCycles?: number;
/** How long a single candidate URL may take to open before trying the next. */
candidateTimeout?: number;
}
/** Injected by the webpack helper via DefinePlugin for `--env.unitTesting` builds. */
declare const __NS_TEST_CONFIG__: { port?: number } | undefined;
function injectedTestConfig(): { port?: number } | undefined {
return typeof __NS_TEST_CONFIG__ !== 'undefined'
? __NS_TEST_CONFIG__
: undefined;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
function delay(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
/**
* Candidate order matters: Android emulators reach the host only via
* 10.0.2.2, while physical Android devices reach 127.0.0.1 through the
* `adb reverse` mapping the host session establishes. iOS and visionOS
* simulators share the host loopback; physical Apple devices need an
* explicit `url`.
*/
export function defaultNativeScriptVitestUrls(
port = DEFAULT_NATIVE_SCRIPT_VITEST_PORT,
): string[] {
const isAndroidRuntime = 'android' in globalThis;
return isAndroidRuntime
? [`ws://10.0.2.2:${port}`, `ws://127.0.0.1:${port}`]
: [`ws://127.0.0.1:${port}`];
}
function defaultSocketFactory(url: string): NativeScriptWebSocketHandle {
const WebSocketConstructor = (
globalThis as unknown as {
WebSocket?: new (address: string) => NativeScriptWebSocketHandle;
}
).WebSocket;
if (!WebSocketConstructor) {
throw new Error('NativeScript WebSocket global is unavailable');
}
return new WebSocketConstructor(url);
}
export class NativeScriptVitestCoordinator
implements NativeScriptTestEventSource
{
private readonly listeners = new Set<NativeScriptTestEventListener>();
private readonly workers = new Map<number, NativeScriptWorkerHandle>();
private socket: NativeScriptWebSocketHandle | undefined;
private startPromise: Promise<void> | undefined;
private stopped = false;
constructor(private readonly options: NativeScriptVitestCoordinatorOptions) {}
subscribe(listener: NativeScriptTestEventListener): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
start(): Promise<void> {
this.startPromise ??= this.connect();
return this.startPromise;
}
stop(): void {
this.stopped = true;
this.workers.forEach((worker) => {
worker.postMessage({ kind: 'stop' });
worker.terminate();
});
this.workers.clear();
this.socket?.close();
this.socket = undefined;
this.startPromise = undefined;
}
private async connect(): Promise<void> {
const port =
this.options.port ??
injectedTestConfig()?.port ??
DEFAULT_NATIVE_SCRIPT_VITEST_PORT;
const candidates = this.options.url
? [this.options.url]
: defaultNativeScriptVitestUrls(port);
const retryDelay = this.options.connectRetryDelay ?? 2_000;
const maxCycles = this.options.maxConnectCycles ?? 60;
let lastError = new Error('NativeScript Vitest socket failed');
for (let cycle = 0; cycle < maxCycles && !this.stopped; cycle += 1) {
for (const url of candidates) {
if (this.stopped) return;
try {
await this.attemptConnection(url);
return;
} catch (error) {
lastError =
error instanceof Error ? error : new Error(String(error));
}
}
await delay(retryDelay);
}
if (this.stopped) return;
this.emit({ type: 'worker-error', worker: 0, message: lastError.message });
throw lastError;
}
private attemptConnection(url: string): Promise<void> {
const socket = (this.options.createSocket ?? defaultSocketFactory)(url);
const candidateTimeout = this.options.candidateTimeout ?? 4_000;
return new Promise<void>((resolve, reject) => {
let opened = false;
let openPoll: ReturnType<typeof setInterval> | undefined;
const openTimer = setTimeout(() => {
if (opened) return;
clearOpenPoll();
// Reject before close(): a polyfill may dispatch onclose synchronously,
// and its generic rejection would otherwise mask this URL-bearing one.
reject(new Error(`NativeScript Vitest socket timed out: ${url}`));
socket.close();
}, candidateTimeout);
const clearOpenPoll = (): void => {
if (openPoll !== undefined) clearInterval(openPoll);
openPoll = undefined;
clearTimeout(openTimer);
};
const handleOpen = (): void => {
if (opened) return;
opened = true;
clearOpenPoll();
this.socket = socket;
this.sendWire({
kind: 'hello',
protocol: NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION,
});
resolve();
};
socket.onopen = handleOpen;
socket.onmessage = (event) => this.onSocketMessage(event.data);
socket.onerror = (event) => {
clearOpenPoll();
const message = event.message ?? 'NativeScript Vitest socket failed';
if (!opened) {
reject(new Error(message));
return;
}
this.emit({ type: 'worker-error', worker: 0, message });
};
socket.onclose = () => {
clearOpenPoll();
if (this.socket === socket) this.socket = undefined;
if (!opened) reject(new Error('NativeScript Vitest socket closed'));
};
// Browser WebSockets always dispatch `open` asynchronously, but some
// NativeScript polyfills can finish a loopback connection in their
// constructor before the coordinator assigns `onopen`.
if (socket.readyState === 1) handleOpen();
else if (socket.readyState !== undefined) {
openPoll = setInterval(() => {
if (socket.readyState === 1) handleOpen();
}, 25);
}
});
}
private onSocketMessage(raw: unknown): void {
if (typeof raw !== 'string') return;
const message = flatParse(raw);
if (!isNativeScriptVitestWireMessage(message)) return;
if (message.kind === 'configure') {
this.configureWorkers(message.main, message.workers);
return;
}
if (message.kind === 'worker-message') {
this.workers.get(message.slot)?.postMessage({
kind: 'pool-message',
frame: message.frame,
});
return;
}
if (message.kind === 'error') {
this.emit({ type: 'worker-error', worker: 0, message: message.message });
}
}
private configureWorkers(main: boolean, workerCount: number): void {
if (main && !this.workers.has(0)) {
if (this.options.registry) {
this.attachWorker(0, createMainThreadWorkerHandle(this.options.registry));
} else {
this.reportConfigurationError(
'NativeScript Vitest coordinator needs a `registry` to run main-thread specs',
);
}
}
const firstWorkerSlot = main ? 1 : 0;
for (let index = 0; index < workerCount; index += 1) {
const slot = firstWorkerSlot + index;
if (this.workers.has(slot)) continue;
if (!this.options.createWorker) {
this.reportConfigurationError(
'NativeScript Vitest coordinator needs `createWorker` to run Worker specs',
);
return;
}
this.attachWorker(slot, this.options.createWorker(slot));
}
}
private attachWorker(slot: number, worker: NativeScriptWorkerHandle): void {
worker.onmessage = (event) => this.onWorkerMessage(slot, event.data);
worker.onerror = (event) => {
this.emit({
type: 'worker-error',
worker: slot,
message: event.message ?? `NativeScript worker ${slot} failed`,
});
};
this.workers.set(slot, worker);
}
private reportConfigurationError(message: string): void {
this.sendWire({ kind: 'error', message });
this.emit({ type: 'worker-error', worker: 0, message });
}
private onWorkerMessage(slot: number, message: unknown): void {
if (!isRecord(message) || typeof message.kind !== 'string') return;
if (message.kind === 'runtime-ready') {
this.workers.get(slot)?.postMessage({ kind: 'start', slot });
return;
}
if (message.kind === 'worker-ready') {
this.sendWire({ kind: 'worker-ready', slot });
return;
}
if (message.kind === 'pool-message' && typeof message.frame === 'string') {
this.sendWire({ kind: 'worker-message', slot, frame: message.frame });
return;
}
if (message.kind === 'test-event' && isRecord(message.event)) {
this.emit(message.event as unknown as NativeScriptTestEvent);
}
}
private sendWire(message: NativeScriptVitestWireMessage): void {
this.socket?.send(flatStringify(message));
}
private emit(event: NativeScriptTestEvent): void {
this.listeners.forEach((listener) => listener(event));
}
}