-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
726 lines (667 loc) · 28.1 KB
/
server.js
File metadata and controls
726 lines (667 loc) · 28.1 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
'use strict';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import http from 'node:http';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { qtapClientHead, qtapClientBody } from './client.cjs';
import * as util from './util.js';
import tapFinished from './tap-finished.js';
/** @import events from 'node:events' */
/** @import { Logger, Browser } from './qtap.js' */
class ControlServer {
static nextServerId = 1;
static nextClientId = 1;
/**
* @param {string} testFile File path or URL
* @param {events.EventEmitter} eventbus
* @param {Logger} logger
* @param {Object} options
* @param {string} options.cwd
* @param {number} options.idleTimeout
* @param {number} options.connectTimeout
* @param {boolean} options.debugMode
*/
constructor (testFile, eventbus, logger, options) {
this.serverId = ControlServer.nextServerId++;
this.logger = logger.channel('qtap_server_S' + this.serverId);
// For `qtap <url>`, default root to cwd (unused).
// For `qtap test/index.html`, default root to cwd.
let root = options.cwd;
let testFileAbsolute;
let testFileQueryString = '';
if (util.isURL(testFile)) {
testFileAbsolute = testFile;
} else {
// For `qtap ../foobar/test/index.html`, default root to ../foobar.
//
// For `qtap /tmp/foobar/test/index.html`, default root to nearest
// common parent dir (i.e. longest common path between file and cwd).
//
testFileAbsolute = path.resolve(root, testFile);
const relPath = path.relative(root, testFileAbsolute);
const parent = relPath.match(/^[./\\]+/)?.[0];
if (parent) {
root = path.join(root, parent);
}
// Support passing "test/index.html?module=foo" as a way to serve index.html,
// with the query string preserved in the URL used client-side, but not
// seen as part of the file name server-side.
//
// TODO: Add test case to confirm we favor foo, even if foo?bar exists as file.
// TODO: Add test case to confirm we favor foo?bar if it exists but foo does not.
// TODO: Add test case to confirm we mention foo in error message if neither exists.
if (testFileAbsolute.includes('?')) {
const withoutQuery = testFileAbsolute.split('?')[0];
if (fs.existsSync(withoutQuery) || !fs.existsSync(testFileAbsolute)) {
testFileQueryString = testFileAbsolute.replace(/^[^?]+/, '');
this.logger.debug('server_testfile_querystring', 'Preserving ' + testFileQueryString);
testFileAbsolute = withoutQuery;
}
}
// Normalize testFile to "test/index.html".
testFile = path.relative(root, testFileAbsolute);
if (!testFile || testFile.startsWith('..')) {
throw new Error(`Cannot serve ${testFile} from ${root}`);
}
// Normalize \backslash to POSIX slash, but only on Windows
// * To use as-is in URLs (launchBrowser).
// * Stable values in reporter output text.
// * Stable values in event data.
// * Only on Windows (pathToFileURL chooses automatically),
// because on POSIX, backslash is a valid character to use in
// in a file name, which we must not replace with forward slash.
const rootUrlPathname = pathToFileURL(root).pathname;
const fileUrlPathname = pathToFileURL(testFileAbsolute).pathname;
testFile = fileUrlPathname
.replace(rootUrlPathname, '')
.replace(/^\/+/, '');
this.logger.debug('server_testfile_normalized', testFile);
}
this.root = root;
this.testFile = testFile;
this.testFileQueryString = testFileQueryString;
this.eventbus = eventbus;
this.idleTimeout = options.idleTimeout;
this.connectTimeout = options.connectTimeout;
this.debugMode = options.debugMode;
this.launchingBrowsers = new Set();
this.browsers = new Map();
// Optimization: Prefetch test file in parallel with server creation and browser launching.
//
// To prevent a Node.js error (unhandledRejection), we add a no-op catch() handler here.
// Once launchBrowser is called, we will await this in handleRequest/getTestFile,
// which is then propertly caught by server.on('request') below, which emits it
// as 'error' event.
//
// The reason we don't emit 'error' directly here, is that that would cause
// qtap.runWaitFor() to return too early, while stuff is still running in the background.
this.testFilePromise = this.fetchTestFile(testFileAbsolute);
this.testFilePromise.catch(() => {
// No-op
});
// Optimization: Don't wait for server to start. Let qtap.js proceed to start other servers,
// and load user config files (which in turn may import browsers, and other plugins, which can
// take a while). We await this later, before calling launchBrowser().
const server = http.createServer();
this.proxyBase = '';
this.proxyBasePromise = new Promise((resolve) => {
server.on('listening', () => {
// @ts-ignore - Not null after listen()
this.proxyBase = 'http://localhost:' + server.address().port;
this.logger.debug('server_listening', `Serving ${root} at ${this.proxyBase}`);
resolve(this.proxyBase);
});
});
/**
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} resp
*/
server.on('request', async (req, resp) => {
try {
const url = new URL(this.proxyBase + req.url);
switch (url.pathname) {
case '/.qtap/tap/':
this.handleTap(req, url, resp);
break;
default:
await this.handleRequest(req, url, resp);
}
} catch (e) {
this.logger.warning('server_respond_uncaught', e);
this.serveError(resp, 500,
'HTTP 500: QTap server_respond_uncaught'
// @ts-ignore - TypeScript @types/node lacks Error.stack
+ '\n\n' + (e.stack || String(e))
);
// At this point, qtap.run() is awaiting ControlServer#launchBrowser
// (as browerPromise). Make sure we don't get stuck there.
// That way:
// - browser.stop() makes launchBrowser() throw/return,
// - qtap.run() sees runPromise get rejected and emits error/finish,
// - qtap.runWaitFor() will throw/return.
this.stopBrowsers(e);
}
});
// Start the server in the background on a random available port
server.listen();
this.stopBrowsers = (e) => {
for (const browser of this.browsers.values()) {
browser.stop(e);
}
};
this.close = async () => {
if (this.closeCalled) {
throw new Error('ControlServer.close must only be called once');
}
this.closeCalled = true;
this.logger.debug('server_close');
server.close();
server.closeAllConnections();
};
}
/** @return {Promise<Object>} Headers and HTML document */
async fetchTestFile (file) {
let headers, body;
// fetch() does not support file URLs (as of writing, in Node.js 22).
if (util.isURL(file)) {
this.logger.debug('testfile_fetch', `Requesting ${file}`);
const resp = await fetch(file);
if (!resp.ok) {
// @ts-ignore - TypeScript @types/node lacks `Error(,options)`
throw new util.QTapError(`Received HTTP ${resp.status} error from ${file}`);
}
headers = resp.headers;
body = await resp.text();
} else {
this.logger.debug('testfile_readfile', `Reading file ${file}`);
headers = new Headers();
try {
body = (await fsPromises.readFile(file)).toString();
} catch (e) {
// @ts-ignore - TypeScript @types/node lacks `Error(,options)`
throw new Error('Could not open ' + this.testFile, { cause: e });
}
}
this.logger.debug('testfile_ready', `Finished fetching ${file}`);
return { headers, body };
}
async getTestFile (clientId) {
const proxyBase = this.getProxyBase();
const qtapTapUrl = proxyBase + '/.qtap/tap/?qtap_clientId=' + clientId;
let headInjectHtml = `<script>(${util.fnToStr(qtapClientHead, qtapTapUrl)})();</script>`;
// Add <base> tag so that URL-based files can fetch their resources directly from the
// original server. Prepend as early as possible. If the file has its own <base>, theirs
// will be after ours and correctly "win" by applying last.
if (util.isURL(this.testFile)) {
headInjectHtml = `<base href="${util.escapeHTML(this.testFile)}"/>` + headInjectHtml;
}
let bodyInjectHtml = '<script>(' + util.fnToStr(qtapClientBody, qtapTapUrl) + ')();</script>';
if (this.debugMode) {
bodyInjectHtml = '<pre id="__qtap_debug_element" style="display: block; max-width: 90%; overflow-x: hidden; white-space: pre-wrap; word-wrap: break-word; background: #fff; color: #000;"></pre>' + bodyInjectHtml;
}
const resp = await this.testFilePromise;
let html = resp.body;
// Head injection
// * Use a callback, to avoid corruption if "$1" appears in the user input.
// * The headInjectHtml string must be one line without any line breaks,
// so that line numbers in stack traces presented in QTap output remain
// transparent and correct.
// * Ignore <heading> and <head-thing>.
// * Support <head x=y...>, including with tabs or newlines before ">".
html = util.replaceOnce(html,
[
/<head(?:\s[^>]*)?>/i,
/<html(?:\s[^>]*)?>/i,
/<!doctype[^>]*>/i,
/^/
],
(m) => m + headInjectHtml
);
html = util.replaceOnce(html,
[
/<\/body>(?![\s\S]*<\/body>)/i,
/<\/html>(?![\s\S]*<\/html>)/i,
/$/
],
(m) => bodyInjectHtml + m
);
return {
headers: resp.headers,
body: html
};
}
async handleRequest (req, url, resp) {
const filePath = path.join(this.root, url.pathname);
const ext = path.extname(url.pathname).slice(1);
if (!filePath.startsWith(this.root)) {
// Disallow outside directory traversal
this.logger.debug('respond_static_deny', url.pathname);
return this.serveError(resp, 403, 'HTTP 403: QTap respond_static_deny');
}
const clientId = url.searchParams.get('qtap_clientId');
if (clientId !== null) {
// If the query parameter is present, serve the testfile, regardless of URL path.
// The URL path is chosen by launchBrowser().
if (this.launchingBrowsers.has(clientId)) {
this.launchingBrowsers.delete(clientId);
this.eventbus.emit('clientonline', { clientId });
} else if (this.debugMode) {
// Allow users to reload the page when in --debug mode.
// Note that the results of this reload will not be reported, because
// we already received and wrote a complete TAP report for this client.
this.logger.debug('browser_reload_debug', clientId);
} else {
this.logger.debug('browser_unknown_clientId', clientId);
return this.serveError(resp, 403, 'HTTP 403: QTap browser_unknown_clientId.\n\nThis clientId was likely already served and cannot be repeated. Run qtap with --debug to bypass this restriction.');
}
const testFileResp = await this.getTestFile(clientId);
for (const [name, value] of testFileResp.headers) {
// Ignore these incompatible headers from the original response,
// as otherwise the browser may truncate the amended test file.
if (!['content-length', 'transfer-encoding', 'content-encoding'].includes(name.toLowerCase())) {
resp.setHeader(name, value);
}
}
if (!testFileResp.headers.get('Content-Type')) {
resp.setHeader('Content-Type', util.MIME_TYPES.html);
}
resp.writeHead(200);
resp.write(testFileResp.body);
resp.end();
return;
}
if (!fs.existsSync(filePath)) {
this.logger.debug('respond_static_notfound', filePath);
return this.serveError(resp, 404, 'HTTP 404: QTap respond_static_notfound');
}
this.logger.debug('respond_static_pipe', filePath);
resp.writeHead(200, { 'Content-Type': util.MIME_TYPES[ext] || util.MIME_TYPES.bin });
fs.createReadStream(filePath)
.on('error', (err) => {
this.logger.warning('respond_static_pipe_error', err);
resp.end();
})
.pipe(resp);
}
handleTap (req, url, resp) {
let body = '';
req.on('data', (data) => {
body += data;
});
req.on('end', () => {
// Support QUnit 2.16 - 2.23: Strip escape sequences for tap-parser compatibility.
// Fixed in QUnit 2.23.1 with https://github.com/qunitjs/qunit/pull/1801.
//
// Strip anyway, to avoid double or conflicting color formatting in test names
body = util.stripAsciEscapes(body);
// Serve information as transparently as possible
//
// - Strip the prefix we added in /src/client.js
// - Strip the proxyBase and qtap_clientId param we added
//
// Firefox: "@http://localhost/test.html:1:2"
// Chrome: " at foo (http://localhost/test.html:1:2)"
//
// We do this here rather than in tapParser.on('comment')
// so that it applies to URLs in both assertion failure stack traces,
// and in "clientconsole" lines.
body = body.replace(/( {2}at |@|\()(http:\S+):(\S+)(?=\n|$)/gm, (m, at, frameUrlStr, suffix) => {
const frameUrl = new URL(frameUrlStr);
if (frameUrl.origin === this.proxyBase) {
return at + frameUrl.pathname + ':' + suffix;
}
return m;
});
const clientId = url.searchParams.get('qtap_clientId');
const browser = this.browsers.get(clientId);
if (browser) {
const now = performance.now();
browser.logger.debug('browser_tap_received',
`+${util.humanSeconds(now - browser.lastReceived)}s`,
body
);
browser.lastReceived = now;
browser.tapParser.write(body);
} else {
this.logger.debug('browser_tap_unhandled', clientId, body);
}
});
resp.writeHead(204);
resp.end();
}
/**
* @param {http.ServerResponse} resp
* @param {number} statusCode
* @param {string} err
*/
serveError (resp, statusCode, err) {
if (!resp.headersSent) {
resp.writeHead(statusCode, { 'Content-Type': util.MIME_TYPES.txt });
resp.write(err + '\n');
}
resp.end();
}
async launchBrowser (browserFn, browserName, globalSignal) {
const maxTries = (browserFn.allowRetries === false || this.debugMode) ? 1 : 3;
let i = 1;
const clientId = 'client_S' + this.serverId + '_C' + ControlServer.nextClientId++;
const logger = this.logger.channel(`qtap_browser_${clientId}_${browserName}`);
let controller = new AbortController();
// TODO: rename browser=>client
// TODO: remove browserName or rename to browserId.
// TODO: rename getDisplayName to getBrowserName.
const browser = {
clientId,
logger,
browserName,
getDisplayName () {
return browserFn.displayName || browserName;
},
browserProcessPromise: null,
tapParser: null,
lastReceived: performance.now(),
/**
* Reasons to stop a browser, whichever comes first:
* 1. tap-finished (client has sent us the test results).
* 2. tap-parser 'bailout' event (client knows it crashed).
* 3. connect timeout
* 4. idle timeout (client idle and presumed lost, or a silent crash).
*
* @param {any} reason
*/
stop: async (reason) => {
if (!this.browsers.has(clientId)) {
// Ignore any duplicate or late reasons to stop
return;
}
this.browsers.delete(clientId);
logger.debug('browser_launch_stopping', String(reason));
controller.abort(reason);
}
};
this.browsers.set(clientId, browser);
while (true) {
const signals = {
// NOTE: The browser signal tracks both "browser" and "global" controllers,
// so that if qtap.run() bails out (e.g. uncaught error from a reporter, or
// test server fails in fetchTestFile due to file not found), and if for
// some reason the natural shutdown fails (i.e. we don't call
// server.stopBrowsers or don't await browerPromise), then we have one
// last chance during shared_cleanup to stop dangling browser processes.
browser: AbortSignal.any([controller.signal, globalSignal]),
global: globalSignal
};
if (this.debugMode) {
// Keep the browser open by replacing with a dummy signal that we never invoke.
//
// TODO: Refactor reporting and process mgmt so that results are reported
// on the CLI as normal. Right now, once the test finishes, the CLI doesn't
// report it because it waits to report until the browser exits. We need
// to create a way to keep report the results yet keep the process open.
signals.browser = (new AbortController()).signal;
signals.browser.addEventListener('abort', () => {
logger.warning('browser_signal_debugging', 'Keeping browser open for debugging');
});
}
try {
await this.launchBrowserAndConnect(browserFn, browser, signals);
logger.debug('browser_connected', `${browser.getDisplayName()} connected! Serving test file.`);
} catch (e) {
// Handle util.BrowserConnectTimeout from launchBrowserAndConnect
//
// We only retry BrowserConnectTimeout, as everything else should be deterministic.
// Uncaught errors from browserFn are generally mistakes in code or configuration.
// Client-side bailouts should be deterministic once a test has begun, and if
// we allowed retries after that reporting gets messy as a client would appear to
// go back in time.
if (e instanceof util.BrowserConnectTimeout && i < maxTries) {
i++;
this.logger.debug('browser_connect_retry', `Retrying, attempt ${i} of ${maxTries}`);
// Give up on the timed-out attempt and reset controller for the next attempt
controller.abort(e);
controller = new AbortController();
continue;
}
if (e instanceof util.QTapError) {
e.qtapClient = {
browser: browserFn.displayName,
testFile: this.testFile
};
}
throw e;
}
try {
await this.getClientResult(browser, signals);
break;
} catch (e) {
if (e instanceof util.QTapError) {
e.qtapClient = {
browser: browserFn.displayName,
testFile: this.testFile
};
}
throw e;
}
}
}
/**
* Launch a browser and ensure it has connected.
*
* @param {Browser} browserFn
* @param {Object} browser
* @param {Object<string,AbortSignal>} signals
*/
async launchBrowserAndConnect (browserFn, browser, signals) {
const proxyBase = this.getProxyBase();
// Serve the a test file from URL that looks like the original path when possible.
//
// - For static files, serve it from a URL that matches were it would be among the
// other static files (even though it is treated special).
// "foo/bar" => "/foo/bar"
// "/tmp/foo/bar" => "/tmp/foo/bar"
// - For external URLs, match the URL path, including query params, so that these
// can be seen both server-side and client-side.
//
// NOTE: This is entirely cosmetic. For how the actual fetch, see fetchTestFile().
// For how resources are requested client side, we use <base href> to ensure correctness.
//
// Example: WordPress password-strength-meter.js inspects the hostname and path
// (e.g. www.mysite.test/mysite/). That test case depends on the real path.
// https://github.com/WordPress/wordpress-develop/blob/6.7.1/tests/qunit/wp-admin/js/password-strength-meter.js#L100
const tmpUrl = new URL(this.testFile + this.testFileQueryString, proxyBase);
tmpUrl.searchParams.set('qtap_clientId', browser.clientId);
const url = proxyBase + tmpUrl.pathname + tmpUrl.search;
// We don't await browserFn() because we first need to handle connectTimeout and retries.
// Then, hand off the process to getClientResult() where we can then cleanly consume TAP
// stream without risking that a retry might produce a second result from the next attempt.
// This way, we consume only from clients that make it to the 'clientonline' event,
// and we don't retry once past that.
//
// We have to attach then()/catch() here instead of later in getClientResult(), as otherwise
// early errors leave browserProcessPromise without error handler, causing a global unhandled
// rejection. E.g. when throwing in a reporter from on('clients').
this.launchingBrowsers.add(browser.clientId);
browser.logger.debug('browser_launch_call');
browser.browserProcessPromise = browserFn(url, signals, browser.logger, this.debugMode)
.then(() => {
// Usually browserFn() will return because we asked via browser.stop() when tests finished
// or timed out. If the browser ended by itself, report is as an error.
// We re-use browser.stop() here because it also invokes the abort signal. That seems
// redundant after the process has exited, but it ensures all resources created by
// browserFn are cleaned up.
if (signals.browser.aborted) {
browser.logger.debug('browser_launch_exit');
} else {
throw new util.BrowserStopSignal('Browser ended unexpectedly');
}
})
.catch(/** @type {Error|Object|string} */ e => {
// Silence any errors from browserFn that happen after we called browser.stop().
if (!signals.browser.aborted) {
browser.logger.warning('browser_launch_error', e);
browser.stop(e);
throw e;
}
});
// The Promise.race call takes care of three things:
//
// * Avoid global uncaught error or global unhandled rejection from browserProcessPromise.
//
// If qtap.js throws between `launchBrowser()` and `await browserPromises`, then
// launchBrowser will be discarded and not proceed to getClientResult(),
// which means no catch is attached to browserProcessPromise.
//
// * Ensure fast cancelling if another browser has raised an error.
//
return Promise.race([
new Promise((resolve, reject) => {
const connectTimeoutTimer = setTimeout(() => {
const reason = `Browser did not start within ${this.connectTimeout}s`;
this.launchingBrowsers.delete(browser.clientId);
const err = new util.BrowserConnectTimeout(reason);
browser.logger.warning('browser_connect_timeout', reason);
reject(err);
}, this.connectTimeout * 1000);
this.eventbus.on('error', () => {
clearTimeout(connectTimeoutTimer);
});
this.eventbus.on('clientonline', (event) => {
if (event.clientId === browser.clientId) {
clearTimeout(connectTimeoutTimer);
resolve(null);
}
});
}),
browser.browserProcessPromise
]);
}
/**
* Consume results from a connected browser process.
*
* @param {Object} browser
* @param {Object<string,AbortSignal>} signals
* @return {Promise<Object>} clientresult
*/
async getClientResult (browser, signals) {
let clientIdleTimer;
let result;
const tapParser = tapFinished({ wait: 0 }, (finalResult) => {
clearTimeout(clientIdleTimer);
result = {
clientId: browser.clientId,
ok: finalResult.ok,
total: finalResult.count,
// We want to reflect how a summary would describe the result,
// based on how the failures are presented.
//
// In tap-parser, an expected-failing todo is detailed in `finalResult.todos`,
// but counts towards both `finalResult.fail` and `finalResult.todo`,
// and not `finalResult.pass`.
//
// In tap-parser, an unexpectedly-passing todo is detailed in `finalResult.todos`,
// but counts towards both `finalResult.pass` and `finalResult.todo`
// (see fixtures/todo-done.html).
//
// If we do nothing, we'd create an odd gap (total 4, passing 3, failed 0).
// If we use pass+todo, we'd double-count unexpectedly-passing todos.
// For now, infer as total-failures.
passed: finalResult.count - finalResult.failures.length,
// Any unexpectedly-passing todo may or may not be counted here.
// It is up to the test framework to decide whether or not to enforce
// that passing todos are corrected to regular tests or not.
//
// Tape reports passing todo as "ok". QUnit reports them as "not ok".
//
// The number here is consistent with the 'failures' array and thus
// consistent with how failures are presented by the reporter.
failed: finalResult.failures.length,
skips: finalResult.skips,
todos: finalResult.todos,
failures: finalResult.failures,
bailout: finalResult.bailout,
};
browser.logger.debug('browser_tap_finished', 'Test run finished', {
ok: result.ok,
total: result.total,
failed: result.failed,
});
browser.stop(new util.BrowserStopSignal('browser_tap_finished'));
});
tapParser.on('bailout', (reason) => {
browser.logger.warning('browser_tap_bailout', 'Test run bailed');
browser.stop(new util.BrowserStopSignal(reason));
});
tapParser.on('comment', (comment) => {
if (!comment.startsWith('# console: ')) {
return;
}
const message = comment
.replace('# console: ', '')
.replace(/\n$/, '');
this.eventbus.emit('clientconsole', {
clientId: browser.clientId,
message
});
});
tapParser.on('assert', (result) => {
this.eventbus.emit('assert', {
clientId: browser.clientId,
ok: result.ok,
fullname: result.fullname,
diag: result.diag
});
});
// Debugging
// tapParser.on('line', logger.debug.bind(logger, 'browser_tap_line'));
// tapParser.on('fail', logger.debug.bind(logger, 'browser_tap_fail'));
// tapParser.on('plan', logger.debug.bind(logger, 'browser_tap_plan'));
browser.tapParser = tapParser;
// It is valid for connectTimeout to be more or equal than idleTimeout.
// These two should be treated separately and not overlapping, as otherwise
// a slow launch or retried launch would eat from the first test's idleTimeout
// and the browser could timeout before it begins.
//
// We treat 'clientonline' as the first message received from the browser,
// and start counting idleTimeout only after that. .
browser.lastReceived = performance.now();
// Optimization: The naive approach would be to clearTimeout+setTimeout on every tap line,
// in `handleTap()` or `tapParser.on('line')`. But that adds significant overhead from
// Node.js/V8 natively allocating many timers when processing large batches of test results.
// Instead, merely store performance.now() and check that periodically.
const TIMEOUT_CHECK_MS = 100;
const qtapCheckTimeout = () => {
if ((performance.now() - browser.lastReceived) > (this.idleTimeout * 1000)) {
const reason = `Test timed out after ${this.idleTimeout}s`;
browser.logger.warning('browser_idle_timeout', reason);
browser.stop(new util.BrowserStopSignal(reason));
return;
}
clientIdleTimer = setTimeout(qtapCheckTimeout, TIMEOUT_CHECK_MS);
};
clientIdleTimer = setTimeout(qtapCheckTimeout, TIMEOUT_CHECK_MS);
try {
await browser.browserProcessPromise;
} finally {
clearTimeout(clientIdleTimer);
}
if (!result) {
if (signals.browser.aborted) {
throw signals.browser.reason;
} else {
throw new Error('Browser exited without error yet results not found.');
}
}
browser.logger.debug('event_clientresult', result);
this.eventbus.emit('clientresult', result);
return result;
}
/** @return {string} */
getProxyBase () {
if (!this.proxyBase) {
throw new Error('Called server.getProxyBase before server was ready');
}
return this.proxyBase;
}
}
export { ControlServer };