-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathrun-workers.js
More file actions
105 lines (89 loc) · 3.02 KB
/
run-workers.js
File metadata and controls
105 lines (89 loc) · 3.02 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
// For Node version >=10.5.0, have to use experimental flag
import { tryOrDefault } from '../utils.js'
import output from '../output.js'
import store from '../store.js'
import event from '../event.js'
import Workers from '../workers.js'
import Codecept from '../codecept.js'
import { getMachineInfo } from './info.js'
export default async function (workerCount, selectedRuns, options) {
process.env.profile = options.profile
const { config: testConfig, override = '' } = options
const overrideConfigs = tryOrDefault(() => JSON.parse(override), {})
// Determine test split strategy
let by = 'test' // default
if (options.by) {
// Explicit --by option takes precedence
by = options.by
} else if (options.suites) {
// Legacy --suites option
by = 'suite'
}
// Validate the by option
const validStrategies = ['test', 'suite', 'pool']
if (!validStrategies.includes(by)) {
throw new Error(`Invalid --by strategy: ${by}. Valid options are: ${validStrategies.join(', ')}`)
}
delete options.parent
const config = {
by,
testConfig,
options,
selectedRuns,
}
const numberOfWorkers = parseInt(workerCount, 10)
output.print(`CodeceptJS v${Codecept.version()} ${output.standWithUkraine()}`)
output.print(`Running tests in ${output.styles.bold(numberOfWorkers)} workers...`)
store.hasWorkers = true
store.workerMode = true
process.env.RUNS_WITH_WORKERS = 'true'
const workers = new Workers(numberOfWorkers, config)
workers.overrideConfig(overrideConfigs)
// Show test distribution after workers are initialized
await workers.bootstrapAll()
const workerObjects = workers.getWorkers()
output.print()
output.print('Test distribution:')
workerObjects.forEach((worker, index) => {
const testCount = worker.tests.length
output.print(` Worker ${index + 1}: ${testCount} test${testCount !== 1 ? 's' : ''}`)
})
output.print()
workers.on(event.test.failed, test => {
output.test.failed(test)
})
workers.on(event.test.passed, test => {
output.test.passed(test)
})
workers.on(event.test.skipped, test => {
output.test.skipped(test)
})
workers.on(event.all.result, result => {
workers.printResults()
})
try {
if (options.verbose || options.debug) store.debugMode = true
if (options.verbose) {
await getMachineInfo()
}
await workers.run()
} catch (err) {
output.error(err)
process.exitCode = 1
} finally {
await workers.teardownAll()
// Force exit if event loop doesn't clear naturally
// This is needed because worker threads may leave handles open
// even after proper cleanup, preventing natural process termination
if (!options.noExit) {
// Use beforeExit to ensure we run after all other exit handlers
// have set the correct exit code
process.once('beforeExit', (code) => {
// Give cleanup a moment to complete, then force exit with the correct code
setTimeout(() => {
process.exit(code || process.exitCode || 0)
}, 100)
})
}
}
}