Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 committed Jan 10, 2023
1 parent 3499b99 commit 5cd4962
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"no-implied-eval": 2,
"no-labels": 2,
"no-with": 2,
"no-loop-func": 1,
"no-loop-func": 0,
"no-native-reassign": 2,
"no-redeclare": [2, {"builtinGlobals": true}],
"no-delete-var": 2,
Expand All @@ -39,7 +39,7 @@
"no-console": 0,
"require-yield": 0,
"no-constant-condition": 1,
"max-len": ["error", { "code": 120 }]
"max-len": ["error", { "code": 120 }],
},
"env": {
"es6": true,
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/cases/limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');
const { filterTestCaseByPlatform } = require('../utils');

const exitFatalErrorScriptPath = path.join(__dirname, '../scripts/fatal_error.js');

exports = module.exports = function () {
const list = [
{
title: 'limit hook is valid',
subTitle: 'auto increase heap limit is ok.',
jspath: exitFatalErrorScriptPath,
}
];

return filterTestCaseByPlatform(list);
};
4 changes: 2 additions & 2 deletions test/fixtures/scripts/fatal_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ process.send({ type: utils.clientConst.xprofilerDone });
const array = [];

setInterval(() => {
array.push(new Array(10e6).fill('*'));
console.log('now rss:', process.memoryUsage().rss / 1024 / 1024 + ' Mb');
array.push(new Array(0.5 * 10e6).fill('*'));
console.log('now rss:', process.memoryUsage().rss / 1024 / 1024 + ' MB');
}, Number(process.env.XPROFILER_FATAL_ERROR_INTERVAL) || 1);
70 changes: 70 additions & 0 deletions test/limit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const os = require('os');
const fs = require('fs');
const cp = require('child_process');
const path = require('path');
const expect = require('expect.js');
const promisify = require('util').promisify;
const readdir = promisify(fs.readdir);
const unlink = promisify(fs.unlink);
const utils = require('./fixtures/utils');
const cases = require('./fixtures/cases/limit')();

const currentPlatform = os.platform();

const logdir = utils.createLogDir('logdir_limit');

const casesLength = cases.length;

for (const cse of cases) {
const ospt = cse.platform || currentPlatform;
describe(`[${ospt}] ${cse.title}`, function () {
const initialHeapLimit = 128;
const autoIncreaseHeapLimitSize = 128;
const MB = 1024 * 1024;

let stdout = '';
let subprocess = null;
before(async function () {
subprocess = cp.fork(cse.jspath, {
execArgv: [`--max-old-space-size=${initialHeapLimit}`],
env: Object.assign({}, process.env, {
XPROFILER_LOG_DIR: logdir,
XPROFILER_LOG_LEVEL: 2,
XPROFILER_LOG_TYPE: 1,
XPROFILER_ENABLE_AUTO_INCR_HEAP_LIMIT: 'YES',
XPROFILER_AUTO_INCR_HEAP_LIMIT_SIZE: autoIncreaseHeapLimitSize,
XPROFILER_FATAL_ERROR_INTERVAL: 500,
}, cse.env),
stdio: [0, 'pipe', 'pipe', 'ipc'],
});
subprocess.stdout.on('data', chunk => stdout += chunk.toString());
await utils.sleep(5000);
subprocess.kill();
console.log('========= stdout =========\n\n', stdout, '\n========= end =========');
});
after(async function () {
const files = await readdir(logdir);
for (const file of files) {
await unlink(path.join(logdir, file));
}
if (cse === cases[casesLength - 1]) {
utils.cleanDir(logdir);
}

subprocess.kill();
});

for (let i = 1; i < 3; i++) {
it(`${cse.subTitle} with ${i} times heap increase factor`, function () {
const increaseLog = `current_heap_limit is ${(initialHeapLimit + (i - 1) * autoIncreaseHeapLimitSize) * MB}, `
+ `initial_heap_limit is ${initialHeapLimit * MB}, `
+ `auto_incr_heap_limit_size is ${autoIncreaseHeapLimitSize}, `
+ `increased_heap is ${initialHeapLimit * MB + i * autoIncreaseHeapLimitSize * MB}`;
console.log('increaseLog:', increaseLog);
expect(stdout).to.contain(increaseLog);
});
}
});
}

0 comments on commit 5cd4962

Please sign in to comment.