-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.js
executable file
·1266 lines (1005 loc) · 55.5 KB
/
async.js
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NOTE: Comments and general structure of this file are WIP
const { importAll } = require('./lib/importAll.js');
importAll().from('./common.js');
const fs = require('fs-extra');
const { execSync, spawn } = require('child_process');
const killSync = require('kill-sync');
const kill = (pid) => killSync(pid, 'SIGINT', true);
const path = require('path');
const noConsoleColours = String(process.env.NO_COLOR) === '1';
/**
* Recursively create a path if it doesn't exist
*
* @param {string} str the path to check and create
*/
const mkdirSyncIfMissing = (s) => !fs.existsSync(s) && fs.mkdirSync(s, { recursive: true });
//for console logs
const green = (s) => noConsoleColours ? s : `\x1b[32m${s}\x1b[0m`;
const orange = (s) => noConsoleColours ? s : `\x1b[33m${s}\x1b[0m`;
const red = (s) => noConsoleColours ? s : `\x1b[31m${s}\x1b[0m`;
/**
* Write to a file. Will create the file & destination folder if they don't exist
*
* BASED ON https://gist.github.com/drodsou/de2ba6291aea67ffc5bc4b52d8c32abd?permalink_comment_id=4137595#gistcomment-4137595
*
* @param {string} filepath the path of the file to write to
* @param {string} content the contents to write to the file
*/
const writeFileSyncRecursive = (filepath, content = '') => {
mkdirSyncIfMissing(path.dirname(filepath));
fs.writeFileSync(filepath, content, { flag: 'w' });
};
const getDirectories = (srcpath) => (
fs.existsSync(srcpath) ? fs.readdirSync(srcpath) : []
).map((file) => path.join(srcpath, file))
.filter((filepath) => fs.statSync(filepath).isDirectory());
const getFiles = (srcpath) => (
fs.existsSync(srcpath) ? fs.readdirSync(srcpath) : []
).map((file) => path.join(srcpath, file))
.filter((filepath) => !fs.statSync(filepath).isDirectory());
const arrToNaturalStr = (arr) => {
const str = arr.join(', ').replace(/,\s([^,]+)$/, ' and $1');
if (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
return str;
}
const strArrayTransformer = (data, lowercase = true) => {
if (!data) return null;
if (typeof data === 'string') {
return [lowercase ? data.toLowerCase() : data]
}
data = data.filter(str => str);
if (data.length) return data.map(str => str.toLowerCase())
return null
}
const getFilesRecursive = (srcpath, arrayOfFiles = []) => {
if (!fs.statSync(srcpath).isDirectory()) {
return [srcpath];
}
fs.readdirSync(srcpath).forEach(function (file) {
if (fs.statSync(path.join(srcpath, file)).isDirectory()) {
arrayOfFiles = getFilesRecursive(path.join(srcpath, file), arrayOfFiles)
} else {
arrayOfFiles.push(path.join(srcpath, file))
}
})
return arrayOfFiles;
}
let SuperTout = class {
#_tout;
#_callback;
setTimeout = (callback, time) => {
clearTimeout(this.#_tout);
this.#_tout = setTimeout(callback, time);
this.#_callback = callback;
};
clearTimeout = () => {
clearTimeout(this.#_tout);
if (this.#_callback) this.#_callback();
};
};
// Remove falsy values from an object
const filterObj = (obj) => {
return Object.keys(obj).reduce((acc, key) => {
if (obj[key]) {
acc[key] = obj[key]
}
return acc
}, {})
}
module.exports = async (config = {}) => {
const startTime = performance.now();
const fullConfig = getFullConfig(config);
const reportDir = fullConfig.reportDir ? path.join(fullConfig.reportDir) : null;
const maxThreadRestarts = fullConfig.maxThreadRestarts ?? 5;
const waitForFileExistTimeout = fullConfig.waitForFileExist?.timeout ?? 60;
const waitForFileMinimumSize = fullConfig.waitForFileExist?.minSize ?? 2;
const waitForFileExistFilepath = fullConfig.waitForFileExist?.filepath;
const stopWaitingForFileWhenFirstThreadCompletes = fullConfig.waitForFileExist?.stopWaitingWhenFirstThreadCompletes ?? true;
const threadInactivityTimeout = fullConfig.threadInactivityTimeout ?? 60 * 10;
const threadTimeLimit = fullConfig.threadTimeLimit ?? 60 * 30;
const threadDelay = (fullConfig.threadDelay ?? 30) * 1000;
const logMode = fullConfig.logMode || 1;
const allureReportHeading = fullConfig.allureReportHeading ? `: ${fullConfig.allureReportHeading}` : '';
const threadMode = fullConfig.threadMode || 1;
const orderThreadsByBenchmark = fullConfig.orderThreadsByBenchmark ?? true;
const saveThreadBenchmark = fullConfig.saveThreadBenchmark ?? true;
const threadBenchmarkFilepath = fullConfig.threadBenchmarkFilepath || 'cmr-benchmarks.json';
const benchmarkDescription = fullConfig.benchmarkDescription ?? null;
const specFiles = fullConfig.specFiles ?? null;
const generateAllure = fullConfig.generateAllure ?? true;
const endProcessIfTestsFail = fullConfig.endProcessIfTestsFail ?? true;
const repeat = fullConfig.repeat || 1;
const reportHeadNotes = [];
const maxConcurrentThreads = fullConfig.maxConcurrentThreads || Math.ceil(require('os').cpus().length / 2);
let noOfThreadsInUse = 0;
const onlyPhaseNo = fullConfig.onlyPhaseNo;
const startingPhaseNo = fullConfig.startingPhaseNo;
const endingPhaseNo = fullConfig.endingPhaseNo;
const userSkippingPhase = (phaseNo) => {
return onlyPhaseNo && onlyPhaseNo !== phaseNo
|| startingPhaseNo > phaseNo
|| endingPhaseNo < phaseNo
}
const phases = fullConfig.phases.map(phase => {
return {
...fullConfig.phaseDefaults,
...phase,
}
}).map(phase => {
return {
...phase,
specsDir: phase.specsDir ? path.join(phase.specsDir) : null,
cypressConfigFilepath: phase.cypressConfig?.filepath ? path.join(phase.cypressConfig.filepath) : null,
cypressConfigObject: phase.cypressConfig?.object,
onlyRunSpecFilesIncludingAnyText: strArrayTransformer(phase.onlyRunSpecFilesIncludingAnyText),
onlyRunSpecFilesIncludingAllText: strArrayTransformer(phase.onlyRunSpecFilesIncludingAllText),
additionalCypressEnvArgs: (() => {
const grepTags = phase.grepTags ? `grepTags="${phase.grepTags}"` : null;
const grep = phase.grep ? `grep="${phase.grep}"` : null;
const grepUntagged = phase.grepUntagged ? `grepUntagged="${phase.grepUntagged}"` : null;
const passthroughEnvArgs = phase.passthroughEnvArgs || null;
const arrStr = [grepTags, grep, grepUntagged, passthroughEnvArgs].filter(str => str).join(',');
if (!arrStr) return '';
return `,${arrStr}`
})(),
}
});
const benchmarkObj = filterObj({
benchmarkDescription,
threadMode,
specFiles,
onlyPhaseNo,
startingPhaseNo,
endingPhaseNo,
phases: phases.map(phase => {
return filterObj({
cypressConfigFilepath: phase.cypressConfigFilepath,
specsDir: phase.specsDir,
onlyRunSpecFilesIncludingAnyText: phase.onlyRunSpecFilesIncludingAnyText,
onlyRunSpecFilesIncludingAllText: phase.onlyRunSpecFilesIncludingAllText,
additionalCypressEnvArgs: phase.additionalCypressEnvArgs,
})
}),
});
const benchmarkId = btoa(JSON.stringify(benchmarkObj));
const openAllure = fullConfig.openAllure || false;
const combineAllure = fullConfig.combineAllure || false;
const hostAllure = fullConfig.hostAllure || false;
const defaultAllureReportDir = path.join(reportDir, 'allure-report');
const allureReportDir = fullConfig.allureReportDir ? path.join(fullConfig.allureReportDir) : defaultAllureReportDir;
const jUnitReportDir = path.join(reportDir, 'junit');
const threadLogsDir = path.join(reportDir, 'cypress-logs');
const runShellCommand = (cmd, cwd = path.resolve(reportDir)) => {
execSync(cmd, {
cwd,
stdio: [null, process.stdout, process.stderr]
});
};
let thread2ExtraLog = '';
let exitCode = 0;
if (fullConfig.maxConcurrentThreadsExperiment) {
console.warn(orange('WARNING: maxConcurrentThreadsExperiment will not work when run through the async mode'));
}
const cypressConfigPhasesUnsorted = phases.map(phase => {
const specs = (() => {
if (threadMode === 2 && !specFiles) {
const arr = getDirectories(phase.specsDir);
if (getFiles(phase.specsDir).length) {
console.warn(orange(`WARNING: One or more files have been placed at the root of ${phase.specsDir}. All spec files must be in subdirectories, otherwise they will not get tested when run with threadMode 2:\n${getFiles(phase.specsDir).join('\n')}\n`));
}
return arr;
} else {
return getFilesRecursive(phase.specsDir);
}
})();
return specs.map((spec) => {
return {
...phase.cypressConfigObject, // add a custom config to every thread to pass into the shell script
specPattern: (() => {
const specsList = phase.onlyRunSpecFilesIncludingAnyText || phase.onlyRunSpecFilesIncludingAllText
? getFilesRecursive(spec).filter(file => {
const fileStr = fs.readFileSync(file).toString('utf8').toLowerCase();
return (phase.onlyRunSpecFilesIncludingAnyText || ['']).some(text => fileStr.includes(text))
&& (phase.onlyRunSpecFilesIncludingAllText || ['']).every(text => fileStr.includes(text))
})
: [spec];
if (specFiles) {
return specsList.filter(spec => specFiles.some(
specFile => (spec.endsWith(specFile) && path.basename(specFile) === path.basename(spec))
|| (path.resolve(specFile) === path.resolve(spec))
));
}
return specsList;
})()
}
}).filter((thread) => thread.specPattern.length)
}).filter((phase) => phase)
if (!cypressConfigPhasesUnsorted.length) {
console.error(red('CRITICAL ERROR: No test phases were found!'));
process.exit(1);
}
const cypressConfigPhasesSorted = [];
// raw test results are saved to this directory, which are then used to create the Allure report
const allureResultsPath = path.resolve(path.join(reportDir, 'allure-results'));
const savedThreadBenchmark = fs.existsSync(threadBenchmarkFilepath) ? (() => {
try {
return JSON.parse(fs.readFileSync(threadBenchmarkFilepath));
} catch (err) {
return {};
}
})() : {};
if (orderThreadsByBenchmark && savedThreadBenchmark[benchmarkId]) {
cypressConfigPhasesUnsorted.forEach((phase, phaseIndex) => {
cypressConfigPhasesSorted[phaseIndex] = [];
const benchmarkOrder = [...new Set(savedThreadBenchmark[benchmarkId].order)];
phase.forEach(thread => {// bring any threads not present in the benchmark to the front
if (!benchmarkOrder.includes(String(thread.specPattern))) {
cypressConfigPhasesSorted[phaseIndex].push(thread);
}
});
benchmarkOrder.forEach(threadPath => {
cypressConfigPhasesSorted[phaseIndex].push(
phase.find(thread => String(thread.specPattern) === threadPath)
)
});
cypressConfigPhasesSorted[phaseIndex] = cypressConfigPhasesSorted[phaseIndex].filter((thread) => thread);
});
} else {
cypressConfigPhasesUnsorted.forEach((phase) => {
cypressConfigPhasesSorted.push(phase);
});
}
const threadsMeta = {};
const initLogs = [];
cypressConfigPhasesSorted.forEach((phase, phaseIndex) => {
const phaseNo = Number(phaseIndex) + 1;
initLogs.push(`[PHASE ${phaseNo}]`)
if (!phase.length) {
initLogs[initLogs.length - 1] += ' (No spec files found, skipping)';
return;
}
if (userSkippingPhase(phaseNo)) {
initLogs[initLogs.length - 1] += ' (Skipping phase)';
return;
}
for (let i = 0; i < repeat; i++) {
phase.forEach((thread) => {
const threadNo = Number(Object.values(threadsMeta).length) + 1;
const threadPath = String(thread.specPattern);
threadsMeta[threadNo] = {
cypressConfigFilepath: phases[phaseIndex].cypressConfigFilepath,
cypressConfig: JSON.stringify(thread),
browser: phases[phaseIndex].browser,
path: threadPath,
phaseNo,
threadNo,
status: 'success',
retries: 0,
perfResults: {},
logs: '',
printedLogs: false,
additionalCypressEnvArgs: phases[phaseIndex].additionalCypressEnvArgs,
mochaFile: fullConfig.jUnitReport?.enabled ? path.join(jUnitReportDir, `${String(threadNo)}.xml`) : null,
}
initLogs[initLogs.length - 1] += `\nThread ${threadNo}: "${threadPath}"${!savedThreadBenchmark[benchmarkId]?.order.includes(threadPath) ? ' (not found in benchmark)' : ''}`;
});
}
if (phases[phaseIndex].onlyRunSpecFilesIncludingAnyText) {
const note = `NOTE: For phase #${phaseNo}, onlyRunSpecFilesIncludingAnyText is set to ["${phases[phaseIndex].onlyRunSpecFilesIncludingAnyText.join('", "')}"]. Therefore, only spec files that contain any strings from this array will be processed.`;
initLogs.push(note);
reportHeadNotes.push(note);
}
if (phases[phaseIndex].onlyRunSpecFilesIncludingAllText) {
const note = `NOTE: For phase #${phaseNo}, onlyRunSpecFilesIncludingAllText is set to ["${phases[phaseIndex].onlyRunSpecFilesIncludingAllText.join('", "')}"]. Therefore, only spec files that contain all strings from this array will be processed.`;
initLogs.push(note);
reportHeadNotes.push(note);
}
});
initLogs.unshift(`${Object.values(threadsMeta).length} thread${Object.values(threadsMeta).length !== 1 ? 's' : ''} will be created to test the following spec files in the following order:`)
if (cypressConfigPhasesSorted.length > 1) {
initLogs.push('Should tests in any phase fail, all threads from subsequent phases will stop immediately.');
}
initLogs.push(`A maximum of ${Object.values(threadsMeta).length < maxConcurrentThreads ? Object.values(threadsMeta).length : maxConcurrentThreads} threads will be used at any one time.\nThis code is executing on a machine with ${require('os').cpus().length} logical CPU threads.`);
if (repeat > 1) {
initLogs.push(green(`Repeat is set to ${repeat}. Therefore, all threads in all phases will run ${repeat} times, regardless of whether the tests pass or fail.`));
}
if (!saveThreadBenchmark) {
initLogs.push(orange('saveThreadBenchmark is set to false, therefore the benchmark file will not be updated when this run completes.'));
}
console.log(`${initLogs.join('\n\n')}\n`)
if (!Object.values(threadsMeta).length) {
console.error(red('CRITICAL ERROR: No spec files were found!'));
process.exit(1);
}
// needed if a Cypress instance doesn't log anything but the shellscript still wants to write to the report directory
mkdirSyncIfMissing(reportDir);
const threadDelayTout = (new SuperTout);
let logQueue = [];
let phaseLock;
async function spawnThread(threadNo, logs = '', restartAttempts = 0, threadStarted = false) {
const cypressProcess = spawn('bash', [
path.resolve(__dirname, 'shell.sh'),
// arguments for the shell script
allureResultsPath || '',// $1
threadsMeta[threadNo].cypressConfigFilepath || '',// $2
threadsMeta[threadNo].cypressConfig,// $3
threadNo,// $4
threadsMeta[threadNo].browser || '',// $5
threadsMeta[threadNo].mochaFile || '',// $6
threadsMeta[threadNo].additionalCypressEnvArgs,// $7
]);
threadsMeta[threadNo].pid = cypressProcess.pid;
if (!threadsMeta[threadNo].retries) noOfThreadsInUse++;
let restartTests = false;
if (phaseLock && phaseLock < threadsMeta[threadNo].phaseNo) {//hack this is daft but it works. Prevent thread from running if a thread from a previous phase has already failed
kill(cypressProcess.pid);
return;
}
if (noOfThreadsInUse > 1 && logMode < 3 && !threadsMeta[threadNo].printedLogs) {
if (logMode === 1) {
console.log(`Thread #${threadNo} is now starting, and its logs will be printed when all preceding threads have completed.`);
} else if (logMode === 2) {
const fullQueue = [Object.values(threadsMeta).filter(thread => thread.printedLogs === 'some')].concat(logQueue);
console.log(`Thread #${threadNo} is now starting, and its logs will be printed once thread${fullQueue.length !== 1 ? 's' : ''}${fullQueue.map(thread => thread.threadNo)} have printed their logs.`);
}
} else {
console.log(`Thread #${threadNo} is now starting...`);
}
if (!threadsMeta[threadNo].retries) threadsMeta[threadNo].perfResults.startTime = performance.now();
const customWarning = (str) => {
console.warn(orange(str));
logs += `${str}\n`;
}
const setThreadInactivityTimer = () => {
if (!threadInactivityTimeout) return;
clearTimeout(threadsMeta[threadNo].threadInactivityTimer);
threadsMeta[threadNo].threadInactivityTimer = setTimeout(() => {
customWarning(`The Cypress instance in thread #${threadNo} hasn\'t responded for ${secondsToNaturalString(threadInactivityTimeout)} and will be considered a crash.`);
restartTests = true;
kill(threadsMeta[threadNo].pid);
}, threadInactivityTimeout * 1000);
};
setThreadInactivityTimer();
const setThreadTimeLimitTimer = () => {
if (!threadTimeLimit || threadsMeta[threadNo].threadTimeLimitTimer) return;
threadsMeta[threadNo].threadTimeLimitTimer = setTimeout(() => {
customWarning(`CRITICAL ERROR: The Cypress instance in thread #${threadNo} has failed to complete within the maximum time limit of ${secondsToNaturalString(threadTimeLimit)}. It will now stop running immediately.`);
threadsMeta[threadNo].status = 'error';
threadsMeta[threadNo].errorType = 'timeout';
exitCode = 1;
restartTests = false;
kill(threadsMeta[threadNo].pid);
}, threadTimeLimit * 1000);
};
setThreadTimeLimitTimer();
// detect known internal errors and restart the tests when they occur!
const logCheck = async (log) => {
threadsMeta[threadNo].logs += log;
if (logMode > 2) {
process.stdout.write(log);
} else {
if (threadsMeta[threadNo].printedLogs) {
process.stdout.write(log);
} else if (!Object.values(threadsMeta).some(thread => thread.printedLogs === 'some' || thread.printedLogs === 'queue')) {
threadsMeta[threadNo].printedLogs = 'some';
process.stdout.write(log);
}
}
setThreadInactivityTimer();
logs += log;
if (
log.includes('uncaught error was detected outside of a test')
|| log.includes('we are skipping the remaining tests in the current suite')
|| log.includes('Cypress could not associate this error to any specific test.')
|| log.includes('Cypress: Fatal IO error')
|| log.includes('Webpack Compilation Error')
) {
restartTests = true;
kill(cypressProcess.pid);
} else if (log.includes('no spec files were found')) {
threadsMeta[threadNo].status = 'error';
threadsMeta[threadNo].errorType = 'no-spec-files';
kill(cypressProcess.pid);
} else if (!threadStarted && log.includes('(Run Starting)')) {
threadStarted = true;
threadDelayTout.clearTimeout();
}
};
cypressProcess.stdout.on('data', (data) => logCheck(data.toString()));
cypressProcess.stderr.on('data', (data) => logCheck(data.toString()));
const printAllLogs = () => {
if (logMode > 2) {
return;
}
if (threadsMeta[threadNo].printedLogs) {
threadsMeta[threadNo].printedLogs = 'all';
logQueue = Object.values(threadsMeta).filter(thread => thread.printedLogs === 'queue');
const nextThreadToLog = Object.values(threadsMeta).find(thread => !thread.printedLogs);
for (let index = 0; index < logQueue.length; index++) {
if (logMode === 1 && logQueue[index].threadNo > nextThreadToLog?.threadNo) {//leave queued to maintain chronology
break;
} else {
threadsMeta[logQueue[index].threadNo].printedLogs = 'all';
process.stdout.write(logQueue[index].logs);
}
}
if (nextThreadToLog) {
threadsMeta[nextThreadToLog.threadNo].printedLogs = 'some';
process.stdout.write(nextThreadToLog.logs);
}
} else {
threadsMeta[threadNo].printedLogs = 'queue';
}
}
logCheck(`Start of thread #${threadNo}:\n`);
const threadCompleteFunc = () => {
printAllLogs();
if (
!threadsMeta[threadNo].logs.includes('All specs passed')
&& (!phaseLock || threadsMeta[threadNo].phaseNo < phaseLock)
&& threadsMeta[threadNo].phaseNo < Object.keys(cypressConfigPhasesSorted).length
) {
phaseLock = threadsMeta[threadNo].phaseNo;
const threadsFromPhasesAfterCurrent = Object.values(threadsMeta).filter(thread => thread.phaseNo > phaseLock);
if (threadsFromPhasesAfterCurrent.length) {
customWarning(orange(`Thread #${threadNo} failed, and as it's from phase #${phaseLock}, all threads from following phases will be stopped immediately. Any remaining threads from phase ${phaseLock} will continue running.`))
threadsFromPhasesAfterCurrent.forEach((thread) => {
if (!threadsMeta[thread.threadNo].logs.includes('All specs passed')) {
threadsMeta[thread.threadNo].status = 'error';
threadsMeta[thread.threadNo].errorType = 'critical';
threadsMeta[thread.threadNo].perfResults.secs = undefined;
threadsMeta[thread.threadNo].perfResults.startTime = undefined;
}
if (thread.pid) kill(thread.pid);
});
}
}
writeFileSyncRecursive(path.join(threadLogsDir, `thread_${threadNo}.txt`), logs);
// the thread might not have been marked as started if it never started properly!
if (!threadStarted) threadStarted = true;
noOfThreadsInUse--;
threadDelayTout.clearTimeout();// todo this doesn't consider whether another thread is currently restarting and so may end a timeout early, but it doesn't really matter...
threadsMeta[threadNo].complete = true;
threadsMeta[threadNo].pid = false;
threadsMeta[threadNo].perfResults.secs = timeDifference(performance.now(), threadsMeta[threadNo].perfResults.startTime);
}
return new Promise((resolve) => {
cypressProcess.on('close', async () => {
clearTimeout(threadsMeta[threadNo].threadInactivityTimer);
clearTimeout(threadsMeta[threadNo].threadTimeLimitTimer);
if (restartTests) {
restartAttempts++;
threadsMeta[threadNo].retries++;
if (restartAttempts < maxThreadRestarts) {
threadsMeta[threadNo].status = 'warn';
customWarning(`WARNING: Internal Cypress error in thread #${threadNo}! Will retry a maximum of ${maxThreadRestarts - restartAttempts} more time${(maxThreadRestarts - restartAttempts) !== 1 ? 's' : ''}.`);
// delete any test results as they'll interfere with the next run
if (fs.pathExistsSync(path.join(allureResultsPath, String(threadNo)))) {
fs.rmSync(path.join(allureResultsPath, String(threadNo)), { recursive: true, force: true });
}
await spawnThread(threadNo, logs, restartAttempts, threadStarted);
} else {
threadsMeta[threadNo].status = 'error';
threadsMeta[threadNo].errorType = 'critical';
exitCode = 1;
customWarning(`CRITICAL ERROR: Too many internal Cypress errors in thread #${threadNo}. Giving up after ${maxThreadRestarts} attempts!`);
threadCompleteFunc();
}
resolve();
} else {
threadCompleteFunc();
resolve();
}
});
});
}
const waitForFileExist = () => {
let waitForFileExistRemainingTime = waitForFileExistTimeout;
return new Promise(resolve => {
const interv = setInterval(() => {
if (
fs.existsSync(waitForFileExistFilepath)
&& Buffer.byteLength(fs.readFileSync(waitForFileExistFilepath)) >= waitForFileMinimumSize
) {// the file exists and has an acceptable filesize
clearInterval(interv);
resolve();
} else if (
stopWaitingForFileWhenFirstThreadCompletes
&& Object.values(threadsMeta).find(thread => thread.complete)
) {
//TODO not the best solution, too bad!
thread2ExtraLog = `WARNING: There may be an issue as the first thread has completed but the "${waitForFileExistFilepath}" file doesn't exist... will continue anyway!`;
console.warn(orange(thread2ExtraLog));
clearInterval(interv);
resolve();
} else {
waitForFileExistRemainingTime -= 0.5;
if (waitForFileExistRemainingTime <= 0) {
//TODO not the best solution, too bad!
thread2ExtraLog = `WARNING: There may be an issue as the "${waitForFileExistFilepath}" file doesn't exist after ${waitForFileExistTimeout} seconds... will continue anyway!`;
console.warn(orange(thread2ExtraLog));
clearInterval(interv);
resolve();
}
}
}, 500);
});
}
// the maximum delay before starting the next thread
// the delay will be interrupted as soon as the current Cypress has started running
const delay = (ms) => {
return new Promise(resolve => threadDelayTout.setTimeout(() => {
if (noOfThreadsInUse < maxConcurrentThreads) {
resolve();
}
}, ms));
}
async function runCypressTests() {
let threadsArr = []
threadsArr.push(spawnThread(1));
if (Object.values(threadsMeta).length > 1) {
if (waitForFileExistFilepath) {
// decrease total delay by .5s as waitForFileExist will check for the file in intervals of .5s
await delay(threadDelay - 500 > 0 ? threadDelay - 500 : 0);
await waitForFileExist();
} else {
await delay(threadDelay);
}
threadsArr.push(spawnThread(2));
for (const [_, thread] of Object.values(threadsMeta).slice(2).entries()) {
await delay(threadDelay);
if (!phaseLock || thread.phaseNo <= phaseLock) {//hack this is daft but it works
threadsArr.push(spawnThread(thread.threadNo));
} else {
break;
}
}
}
await Promise.all(threadsArr)
}
await runCypressTests();
if (logMode === 4) {
Object.values(threadsMeta).forEach(thread => {
process.stdout.write(thread.logs);
})
}
console.log(`All Cypress testing threads have ended after ${secondsToNaturalString(timeDifference(performance.now(), startTime))}.`);
let reportText = '';
// bulk of multithread report logic
{
let longestThread = { secs: -1 };
Object.values(threadsMeta).forEach((thread) => {
const failedTests = {};
let mostFailsForOneTest = 0;
// record how many tests failed, and how many times!
getFiles(
path.join(allureResultsPath, String(thread.threadNo)),
).forEach((file) => {
if (file.endsWith('.json')) {
try {// try catch as not all JSON files will be parseable
const { status, labels, fullName } = fs.readJsonSync(file);
if (
status
&& labels
&& fullName
&& !['passed', 'skipped', 'unknown'].includes(status)
) {
// ensure the key is wholly unique (in case two tests have the same title)
const key = `${fullName}${JSON.stringify(labels)}`;
if (!failedTests[key]) failedTests[key] = 1;
else failedTests[key] += 1;
if (failedTests[key] > mostFailsForOneTest) {
mostFailsForOneTest = failedTests[key];
}
}
} catch (err) { }
}
// files from all threads need to be in the same directory to construct the Allure report
fs.moveSync(
file,
path.resolve(allureResultsPath, path.basename(file)),
{ overwrite: true },
);
});
threadsMeta[thread.threadNo].perfResults = {
...thread.perfResults,
failedTests,
mostFailsForOneTest
};
// the longest thread is used as the basis of comparison with the others
if (thread.perfResults.secs > longestThread.secs) {
longestThread = {
threadNo: thread.threadNo,
naturalString: secondsToNaturalString(thread.perfResults.secs),
secs: thread.perfResults.secs
};
}
});
if (saveThreadBenchmark) {
benchmarkObj.order = [...new Set(Object.values(threadsMeta).filter(thread => thread.perfResults.secs).sort((a, b) => b.perfResults.secs - a.perfResults.secs).map(threadsMeta => threadsMeta.path))];
if (benchmarkObj.order.length < 2) {
console.log(orange('The thread benchmark will not be updated because two or more threads with different files are required to determine the optimal order!'));
} else if (Object.values(threadsMeta).length !== benchmarkObj.order.length) {
console.log(orange('The thread benchmark will not be updated because one or more phases did not complete!'));
} else {
if (JSON.stringify(savedThreadBenchmark[benchmarkId]) !== JSON.stringify(benchmarkObj)) {
console.log(`Updating thread order:\n["${benchmarkObj.order.join('", "')}"]`);
fs.writeFileSync(threadBenchmarkFilepath, `${JSON.stringify({
...savedThreadBenchmark,
[benchmarkId]: benchmarkObj,
}, null, 4)}\n`);
} else {
console.log('The results of the thread benchmark are identical to the records already saved, so the thread order doesn\'t need changing!');
}
}
}
// a visual representation of how each thread performed, to show where any bottlenecks lie
const generateThreadBars = (timeOfLongestThread) => {
let str = '';
cypressConfigPhasesSorted.forEach((_, phaseIndex) => {
const phaseNo = Number(phaseIndex) + 1;
str += `[PHASE ${phaseNo}]\n\n`;
const phaseThreads = Object.values(threadsMeta).filter(thread => thread.phaseNo === phaseNo);
if (userSkippingPhase(phaseNo)) {
str += 'Phase skipped!\n\n';
} else if (!phaseThreads.length) {
str += 'No spec files were found!\n\n';
}
phaseThreads.forEach((thread) => {
const threadPath = thread.path;
const threadId = Object.values(threadsMeta).length > 9 ? String(thread.threadNo).padStart(2, '0') : thread.threadNo;
const shortThreadPath = threadPath.length > 60
? `...${threadPath.substring(threadPath.length - 57).match(/\/(.*)$/)?.[0] || threadPath.substring(threadPath.length - 57)}`
: threadPath;
threadsMeta[thread.threadNo].heading = [`Thread #${threadId} [${shortThreadPath}]`];
str += `Thread #${threadId} [${threadPath}]\n`;
if (threadsMeta[thread.threadNo].errorType === 'no-spec-files') {
const err = 'ERROR: No spec files found!';
threadsMeta[thread.threadNo].status = 'error';
threadsMeta[thread.threadNo].heading.push(err);
threadsMeta[thread.threadNo].summary = `ERROR: No spec files were found for thread #${thread.threadNo}`;
str += `${err}\n\n`;
exitCode = 1;
} else if (threadsMeta[thread.threadNo].errorType === 'critical') {
const err = 'CRITICAL ERROR: Thread did not complete!';
threadsMeta[thread.threadNo].status = 'error';
threadsMeta[thread.threadNo].heading.push(err);
str += `${err}\n\n`;
exitCode = 1;
} else if (threadsMeta[thread.threadNo].errorType === 'timeout') {
const err = `CRITICAL ERROR: Thread did not complete as it went over the time limit of ${secondsToNaturalString(threadTimeLimit)}`;
threadsMeta[thread.threadNo].status = 'error';
threadsMeta[thread.threadNo].heading.push(err);
str += `${err}\n\n`;
exitCode = 1;
}
const percentageOfTotal = (
threadsMeta[thread.threadNo].perfResults.secs / timeOfLongestThread
) * 100;
if (isNaN(percentageOfTotal)) {
return;
}
// log if any tests in the thread failed/retried
const reportFailedTests = (obj) => {
const result = [];
if (Object.entries(obj).length) {
if (threadsMeta[thread.threadNo].status !== 'error') threadsMeta[thread.threadNo].status = 'warn';
const counts = Object.values(obj).reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
Object.keys(counts).sort((a, b) => b - a).forEach((num) => {
result.push(`${counts[num]} test${counts[num] > 1 ? 's' : ''} failed ${num} time${num > 1 ? 's' : ''}`);
});
}
if (threadsMeta[thread.threadNo].retries) {
if (threadsMeta[thread.threadNo].retries === maxThreadRestarts) {
result.push(`the thread crashed ${maxThreadRestarts} time${maxThreadRestarts === 1 ? '' : 's'}, never recovering`);
} else {
if (threadsMeta[thread.threadNo].status !== 'error') threadsMeta[thread.threadNo].status = 'warn';
result.push(`the thread needed restarting ${threadsMeta[thread.threadNo].retries} time${threadsMeta[thread.threadNo].retries === 1 ? '' : 's'}`);
}
}
if (!result.length) {
return '';
}
threadsMeta[thread.threadNo].heading.push(`WARNING: ${arrToNaturalStr(result)}`);
threadsMeta[thread.threadNo].summary = `WARNING: Thread #${thread.threadNo}: ${arrToNaturalStr(result)}`;
return ` (WARNING: ${arrToNaturalStr(result)})`;
};
str += `${generatePercentageBar(percentageOfTotal, threadsMeta[thread.threadNo].perfResults.secs)}${reportFailedTests(threadsMeta[thread.threadNo].perfResults.failedTests)}\n\n`;
});
});
return str;
};
reportText = `See below to compare how each thread performed.\n\n${generateThreadBars(longestThread.secs)}The percentages given above represent how much time individual threads took to complete relative to thread #${longestThread.threadNo}, which was the longest at ${longestThread.naturalString}. Any thread that takes significantly longer than others will be a bottleneck, so the closer the percentages are to one another, the better. A wide range in percentages indicates that the threads could be balanced more efficiently. Be alert as to whether any thread/test needed retrying, because that will skew the results for the affected threads.`;
console.log(`\n\n${reportText}\n\n`);
// save these logs for future reference!
fs.writeFileSync(path.resolve(reportDir, 'thread-performance-report.txt'), reportText);
}
// Overwrite the historyIds in the Allure results, as the Allure plugin's method is flawed.
// Doing this ensures tests across identical names are included in the report
// NOTE: Tests with identical names must be in different describe blocks and/or spec files
getFiles(allureResultsPath).forEach((file) => {
if (!file.endsWith('.json')) return;
try {// try catch as not all JSON files will be parseable
const data = fs.readJsonSync(file);
const { labels, fullName } = data;
if (data.historyId && labels.length) {
data.historyId = Buffer.from(JSON.stringify(labels.concat(fullName))).toString('base64');
fs.writeFileSync(file, JSON.stringify(data));
}
} catch (err) { }
});
// generate the Allure report from the most recent run
runShellCommand('allure generate allure-results --clean -o allure-report');
// make sure none of the tests failed!
{
const testResultsObj = fs.readJsonSync(
path.resolve(reportDir, 'allure-report', 'history', 'history-trend.json'),
'utf-8',
)[0].data;
if (
testResultsObj.failed
+ testResultsObj.broken
+ testResultsObj.unknown !== 0
) {
let mostFailsForOneTestAcrossAllThreads = 1;
Object.entries(threadsMeta).forEach((thread) => {// determine how many times a test is allowed to fail until it gives up
if (thread[1].perfResults.mostFailsForOneTest > mostFailsForOneTestAcrossAllThreads) {
mostFailsForOneTestAcrossAllThreads = thread[1].perfResults.mostFailsForOneTest;
}
});
Object.keys(threadsMeta).forEach((threadNo) => {
if (threadsMeta[threadNo].perfResults.mostFailsForOneTest === mostFailsForOneTestAcrossAllThreads) {
threadsMeta[threadNo].status = 'error';
}
});
exitCode = 1;
}
}
if (fullConfig.jUnitReport?.enabled) {
const { mergeFiles } = require('junit-report-merger');
const outputFile = path.join(reportDir, 'combined-junit-report.xml');
try {
await mergeFiles(outputFile, [path.join(jUnitReportDir, '*.xml')]);
fs.writeFileSync(
outputFile,
fs.readFileSync(outputFile)
.toString('utf8')
.replace(/<\/>/gm, '')// hack to remove a dodgy tag that breaks the report
);
} catch (err) {
console.error(red(`ERROR: JUnit report merge failed:`), err);
}
const browserStackObservability = fullConfig.jUnitReport?.browserStackTestObservabilityUpload;
if (browserStackObservability) {
const outputFileBSO = path.join(path.dirname(outputFile), path.basename(outputFile).replace('.xml', '-bso.xml'));
const endpoint = browserStackObservability.endpoint || 'https://upload-observability.browserStack.com/upload';
try {
fs.writeFileSync(
outputFileBSO,
fs.readFileSync(outputFile)