Skip to content

Commit 3c55c3e

Browse files
Copilotkobenguyent
andcommitted
Fix test stats issue in pool mode - consolidate results properly
Co-authored-by: kobenguyent <[email protected]>
1 parent 727c4aa commit 3c55c3e

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

lib/command/workers/runTests.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ async function runPoolTests() {
8383
initializeListeners()
8484
disablePause()
8585

86+
// Accumulate results across all tests in pool mode
87+
let consolidatedStats = { passes: 0, failures: 0, tests: 0, pending: 0, failedHooks: 0 }
88+
let allTests = []
89+
let allFailures = []
90+
8691
// Keep requesting tests until no more available
8792
while (true) {
8893
// Request a test assignment
@@ -106,6 +111,22 @@ async function runPoolTests() {
106111
if (mocha.suite.total() > 0) {
107112
// Run the test and complete
108113
await codecept.run()
114+
115+
// Accumulate the results from this test run
116+
const result = container.result()
117+
consolidatedStats.passes += result.stats.passes || 0
118+
consolidatedStats.failures += result.stats.failures || 0
119+
consolidatedStats.tests += result.stats.tests || 0
120+
consolidatedStats.pending += result.stats.pending || 0
121+
consolidatedStats.failedHooks += result.stats.failedHooks || 0
122+
123+
// Add tests and failures to consolidated collections
124+
if (result.tests) {
125+
allTests.push(...result.tests)
126+
}
127+
if (result.failures) {
128+
allFailures.push(...result.failures)
129+
}
109130
}
110131

111132
// Signal test completed and request next
@@ -141,6 +162,17 @@ async function runPoolTests() {
141162
console.error('Teardown error:', err)
142163
}
143164

165+
// Send final consolidated results for the entire worker
166+
const finalResult = {
167+
stats: consolidatedStats,
168+
tests: allTests,
169+
failures: allFailures,
170+
hasFailed: consolidatedStats.failures > 0
171+
}
172+
173+
sendToParentThread({ event: event.all.after, workerIndex, data: finalResult })
174+
sendToParentThread({ event: event.all.result, workerIndex, data: finalResult })
175+
144176
// Close worker thread when pool mode is complete
145177
parentPort?.close()
146178
}
@@ -220,15 +252,8 @@ function initializeListeners() {
220252
parentPort?.close()
221253
})
222254
} else {
223-
// In pool mode, don't close worker after individual test completion
224-
// The worker will close when it receives 'NO_MORE_TESTS' message
225-
event.dispatcher.on(event.all.after, () => {
226-
sendToParentThread({ event: event.all.after, workerIndex, data: container.result().simplify() })
227-
})
228-
event.dispatcher.on(event.all.result, () => {
229-
sendToParentThread({ event: event.all.result, workerIndex, data: container.result().simplify() })
230-
// Don't close parentPort in pool mode - let the pool manager handle worker lifecycle
231-
})
255+
// In pool mode, don't send result events for individual tests
256+
// Results will be sent once when the worker completes all tests
232257
}
233258
}
234259

test/unit/worker_test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,39 @@ describe('Workers', function () {
333333
expect('pool').not.equal('test')
334334
expect('pool').not.equal('suite')
335335
})
336+
337+
it('should handle pool mode result accumulation correctly', (done) => {
338+
const workerConfig = {
339+
by: 'pool',
340+
testConfig: './test/data/sandbox/codecept.workers.conf.js',
341+
}
342+
343+
let resultEventCount = 0
344+
const workers = new Workers(2, workerConfig)
345+
346+
// Mock Container.result() to track how many times addStats is called
347+
const originalResult = Container.result()
348+
const mockStats = { passes: 0, failures: 0, tests: 0 }
349+
const originalAddStats = originalResult.addStats.bind(originalResult)
350+
351+
originalResult.addStats = (newStats) => {
352+
resultEventCount++
353+
mockStats.passes += newStats.passes || 0
354+
mockStats.failures += newStats.failures || 0
355+
mockStats.tests += newStats.tests || 0
356+
return originalAddStats(newStats)
357+
}
358+
359+
workers.on(event.all.result, (result) => {
360+
// In pool mode, we should receive consolidated results, not individual test results
361+
// The number of result events should be limited (one per worker, not per test)
362+
expect(resultEventCount).to.be.lessThan(10) // Should be much less than total number of tests
363+
364+
// Restore original method
365+
originalResult.addStats = originalAddStats
366+
done()
367+
})
368+
369+
workers.run()
370+
})
336371
})

0 commit comments

Comments
 (0)