Skip to content

Commit

Permalink
test: check child process exit code
Browse files Browse the repository at this point in the history
PR-URL: #136
Reviewed-BY: legendecas <[email protected]>
  • Loading branch information
hyj1991 authored Feb 11, 2022
1 parent 7184d68 commit 5a39e3f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/environment_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ EnvironmentData::EnvironmentData(v8::Isolate* isolate, uv_loop_t* loop)

void EnvironmentData::AtExit(void* arg) {
// TODO(legendecas): environment registry.
per_process::environment_data.reset();
// TODO(hyj1991): avoid 0xC0000005 on windows
// per_process::environment_data.reset();
}

void EnvironmentData::SendCollectStatistics() {
Expand Down
8 changes: 7 additions & 1 deletion test/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ for (let i = 0; i < testConfig.length; i++) {
let resByXctl = '';
let resByXprofctl = '';
let pid = 0;
let exitInfo = { code: null, signal: null };
before(async function () {
mm(os, 'homedir', () => tmphome);
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, 'start fork.');
Expand Down Expand Up @@ -72,7 +73,7 @@ for (let i = 0; i < testConfig.length; i++) {
});
resByXprofctl = resByXprofctl.stderr.trim() + resByXprofctl.stdout.trim();
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, 'wait for child process done.');
await new Promise(resolve => p.on('close', resolve));
exitInfo = await utils.getChildProcessExitInfo(p);
});

after(function () {
Expand All @@ -83,6 +84,11 @@ for (let i = 0; i < testConfig.length; i++) {
}
});

it(`child process should be exited with code 0`, function () {
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, `exit info: ${JSON.stringify(exitInfo)}`);
utils.checkChildProcessExitInfo(expect, exitInfo);
});

it(`response with xctl should be ok`, function () {
console.log('xtcl:', JSON.stringify(resByXctl));
expect(resByXctl).to.be.ok();
Expand Down
13 changes: 12 additions & 1 deletion test/fixtures/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ exports.getNestingValue = function (origin, key) {
return origin;
};

exports.arrayEqual = function arrayEqual(arr1, arr2) {
exports.arrayEqual = function (arr1, arr2) {
return arr1.every(a1 => arr2.includes(a1)) && arr2.every(a2 => arr1.includes(a2));
};

exports.getChildProcessExitInfo = function (proc) {
return new Promise(resolve => proc.on('close', (code, signal) => resolve({ code, signal })));
};

exports.checkChildProcessExitInfo = function (expect, exitInfo) {
const { code, signal } = exitInfo;
// One of the code | signal will always be non-null.
expect(code === 0).to.be.ok();
expect(signal === null).to.be.ok();
};
9 changes: 8 additions & 1 deletion test/logbypass.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs');
const cp = require('child_process');
const expect = require('expect.js');
const moment = require('moment');
const pack = require('../package.json');
const utils = require('./fixtures/utils');
const getTestCases = require('./fixtures/logbypass.test');
Expand Down Expand Up @@ -77,10 +78,11 @@ for (const testCase of cases) {
/*eslint no-loop-func: "off" */
let logContent;
let pid;
let exitInfo = { code: null, signal: null };
before(async function () {
const p = cp.fork(target.file, { env: Object.assign({ XPROFILER_LOG_TYPE: 1 }, testCase.env, target.env) });
pid = p.pid;
await new Promise(resolve => p.on('close', resolve));
exitInfo = await utils.getChildProcessExitInfo(p);
logContent = fs.readFileSync(target.logfile, 'utf8');
});

Expand All @@ -98,6 +100,11 @@ for (const testCase of cases) {
}
});

it(`child process should be exited with code 0`, function () {
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, `exit info: ${JSON.stringify(exitInfo)}`);
utils.checkChildProcessExitInfo(expect, exitInfo);
});

const types = Object.keys(testCase.struct);
for (const type of types) {
describe(`parse log type [${type}] with content`, function () {
Expand Down

0 comments on commit 5a39e3f

Please sign in to comment.