-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a memory leak that keeps required files in-memory between test runs. Properly clear the require cache by also clearing [the mocha's module children](https://nodejs.org/api/modules.html#modules_module_children). This makes sure the required files by mocha can be garbage collected.
- Loading branch information
Showing
8 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/mocha-runner/test/integration/MemoryLeak.it.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import path = require('path'); | ||
|
||
import execa = require('execa'); | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { MochaTestRunner } from '../../src'; | ||
|
||
describe(MochaTestRunner.name, () => { | ||
it('should not leak memory when running multiple times (#2461)', async () => { | ||
const childProcess = await execa.node(path.resolve(__dirname, 'MemoryLeak.worker.js'), [], { | ||
stdio: 'pipe', | ||
nodeOptions: ['--max-old-space-size=32', '--max-semi-space-size=1'], | ||
}); | ||
expect(childProcess.exitCode).eq(0); | ||
expect(childProcess.stdout).contains('Iterator count 1'); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/mocha-runner/test/integration/MemoryLeak.worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import path = require('path'); | ||
|
||
import { testInjector } from '@stryker-mutator/test-helpers'; | ||
|
||
import { expect } from 'chai'; | ||
import { DryRunStatus, TestStatus } from '@stryker-mutator/api/test_runner'; | ||
|
||
import { createMochaTestRunner } from '../../src'; | ||
|
||
/** | ||
* This file will run the mocha runner a number of times in a test suite that is designed | ||
* to result in an OutOfMemory error when the mocha test runner does not clean it's memory | ||
* Start this process with `--max-old-space-size=32 --max-semi-space-size=1` to get a fast OutOfMemory error (if there is a memory leak) | ||
* | ||
* @see https://github.com/stryker-mutator/stryker/issues/2461 | ||
* @see https://nodejs.org/api/modules.html#modules_accessing_the_main_module | ||
* @see https://stackoverflow.com/questions/30252905/nodejs-decrease-v8-garbage-collector-memory-usage | ||
*/ | ||
|
||
if (require.main === module) { | ||
main().catch((err) => { | ||
console.error(err); | ||
process.exitCode = 1; | ||
}); | ||
} | ||
|
||
async function main() { | ||
process.chdir(path.resolve(__dirname, '..', '..', 'testResources', 'big-project')); | ||
const mochaRunner = testInjector.injector.injectFunction(createMochaTestRunner); | ||
|
||
await mochaRunner.init(); | ||
await doDryRun(); | ||
|
||
async function doDryRun(n = 20) { | ||
if (n > 0) { | ||
console.log(`Iterator count ${n}`); | ||
const result = await mochaRunner.dryRun({ coverageAnalysis: 'off', timeout: 3000 }); | ||
if (result.status === DryRunStatus.Complete) { | ||
expect(result.tests).lengthOf(1); | ||
expect(result.tests[0].status).eq(TestStatus.Success); | ||
} | ||
await doDryRun(n - 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/mocha-runner/testResources/big-project/src/big-text.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
// Change the text each iteration to prevent nodejs from optimizing it into one value on the heap | ||
if (!global.n) { | ||
global.n = 500000; | ||
} | ||
module.exports.text = new Array(global.n++).fill('i').join(','); |
9 changes: 9 additions & 0 deletions
9
packages/mocha-runner/testResources/big-project/test/big-text.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { text } = require('../src/big-text'); | ||
|
||
describe('Big text', () => { | ||
it('should contain i', () => { | ||
if (text.indexOf('i') === -1) { | ||
throw new Error(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters