Skip to content

Commit

Permalink
chore: make test more reliable (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored Oct 21, 2022
1 parent c574ca2 commit 333593e
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 61 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ demo.js

client.js

logdir

release

logdir*

tmphome*

21 changes: 14 additions & 7 deletions test/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const promisify = require('util').promisify;
const readdir = promisify(fs.readdir);
const unlink = promisify(fs.unlink);
const exec = promisify(cp.exec);
const mm = require('mm');
const expect = require('expect.js');
Expand All @@ -14,22 +16,22 @@ const xctl = require('../lib/xctl');
const utils = require('./fixtures/utils');

const currentPlatform = os.platform();
const commandTestFixture = require('./fixtures/command.test');
const commandTestFixture = require('./fixtures/cases/command');
const { checkProfile } = commandTestFixture;
const logdir = utils.createLogDir('logdir_command');
const tmphome = utils.createLogDir('tmphome_command');
const testConfig = commandTestFixture(logdir);
const testFiles = [
{
jspath: path.join(__dirname, './fixtures/blocking.js'),
desc: 'when js main thread blocking'
jspath: path.join(__dirname, './fixtures/scripts/process_normal.js'),
desc: 'when js main thread normal execute'
},
{
jspath: path.join(__dirname, './fixtures/non-blocking.js'),
desc: 'when js main thread non blocking'
jspath: path.join(__dirname, './fixtures/scripts/process_blocking.js'),
desc: 'when js main thread blocking'
},
{
jspath: path.join(__dirname, './fixtures/worker_blocking.js'),
jspath: path.join(__dirname, './fixtures/scripts/worker_blocking.js'),
desc: 'when js worker thread blocking',
threadId: 1,
},
Expand Down Expand Up @@ -78,6 +80,7 @@ describe('commands', () => {
// wait for xprofiler to start
await new Promise(resolve => p.on('message', msg =>
msg.type === utils.clientConst.xprofilerDone && resolve()));
await utils.sleep(500);

// send cmd with xctl (function)
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, 'send xctl cmd.');
Expand All @@ -102,8 +105,12 @@ describe('commands', () => {
exitInfo = await utils.getChildProcessExitInfo(p);
});

after(function () {
after(async function () {
mm.restore();
const files = await readdir(logdir);
for (const file of files) {
await unlink(path.join(logdir, file));
}
if (i === testConfig.length - 1 && j === testFiles.length - 1) {
utils.cleanDir(logdir);
utils.cleanDir(tmphome);
Expand Down
2 changes: 1 addition & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');
const mm = require('mm');
const expect = require('expect.js');
const xprofiler = require('../xprofiler');
const testXprofilerConfigKeys = require('./fixtures/config.test');
const testXprofilerConfigKeys = require('./fixtures/cases/config');
const utils = require('./fixtures/utils');
const testKeys = Object.keys(testXprofilerConfigKeys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const os = require('os');
const cp = require('child_process');
const moment = require('moment');
const expect = require('expect.js');
const { filterTestCaseByPlatform } = require('./utils');
const pkg = require('../../package.json');
const { filterTestCaseByPlatform } = require('../utils');
const pkg = require('../../../package.json');

const currentPlatform = os.platform();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const os = require('os');
const path = require('path');
const utils = require('./utils');
const utils = require('../utils');

function getExtra(key, expected, ext) {
return ext.map(item => {
Expand Down
14 changes: 8 additions & 6 deletions test/fixtures/hooks.test.js → test/fixtures/cases/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ const fs = require('fs');

const path = require('path');
const expect = require('expect.js');
const { profileRule: { diag }, checkProfile, checkCoreDump } = require('./command.test');
const { filterTestCaseByPlatform } = require('./utils');
const { profileRule: { diag }, checkProfile, checkCoreDump } = require('./command');
const { filterTestCaseByPlatform } = require('../utils');

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

exports = module.exports = function () {
const list = [
{
title: 'fatal error hook is valid',
subTitle: 'x-fatal-error.diag is created when fatal error occured.',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
regexp: /x-fatal-error-(\d+)-(\d+)-(\d+).diag/,
check(file) {
const content = fs.readFileSync(file, 'utf8').trim();
Expand All @@ -24,7 +26,7 @@ exports = module.exports = function () {
platform: 'linux',
title: 'fatal error hook is valid',
subTitle: 'x-fatal-error.core is created when fatal error occured.',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
regexp: /x-fatal-error-(\d+)-(\d+)-(\d+).core/,
check(file) {
checkCoreDump(file, 'fatal error core elf information');
Expand All @@ -38,7 +40,7 @@ exports = module.exports = function () {
platform: 'win32',
title: 'fatal error hook is valid',
subTitle: 'x-fatal-error.core is created when fatal error occured.',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
regexp: /x-fatal-error-(\d+)-(\d+)-(\d+).core/,
check(file) {
const content = fs.readFileSync(file, 'utf8').trim();
Expand All @@ -54,7 +56,7 @@ exports = module.exports = function () {
platform: 'darwin',
title: 'fatal error hook is valid',
subTitle: 'x-fatal-error.core is created when fatal error occured.',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
regexp: /x-fatal-error-(\d+)-(\d+)-(\d+).core/,
check(file) {
const content = fs.readFileSync(file, 'utf8').trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const path = require('path');
const moment = require('moment');
const utils = require('./utils');
const blocking = path.join(__dirname, 'blocking.js');
const nonBlocking = path.join(__dirname, 'non-blocking.js');
const utils = require('../utils');
const blocking = path.join(__dirname, '../scripts/process_blocking.js');
const nonBlocking = path.join(__dirname, '../scripts/process_normal.js');

function setRules(list, alinode, { alinodeRule, xprofilerRule }) {
const rules = {};
Expand Down Expand Up @@ -113,7 +113,7 @@ function getTestCases(title, logdirBlocking, logdirNonBlocking, envConfig, struc
env: { XPROFILER_LOG_DIR: logdirBlocking }
};
const nonBlockingTarget = {
title: `${subtitle} non-blocking`,
title: `${subtitle} normal`,
file: nonBlocking,
env: { XPROFILER_LOG_DIR: logdirNonBlocking }
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

const fs = require('fs');
const path = require('path');
const { profileRule: { cpuprofile, heapprofile, gcprofile }, checkProfile } = require('./command.test');
const { profileRule: { cpuprofile, heapprofile, gcprofile }, checkProfile } = require('./command');

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

exports = module.exports = function () {
const list = [
// fatal error
{
title: '<fatal_error> cpu profiling',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
tid: 0,
cmd: 'start_cpu_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -21,7 +24,7 @@ exports = module.exports = function () {
},
{
title: '<fatal_error> heap profiling',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
tid: 0,
cmd: 'start_heap_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -33,7 +36,7 @@ exports = module.exports = function () {
},
{
title: '<fatal_error> gc profiling',
jspath: path.join(__dirname, 'fatal-error.js'),
jspath: exitFatalErrorScriptPath,
tid: 0,
cmd: 'start_gc_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -47,7 +50,7 @@ exports = module.exports = function () {
// exit 0 with profiling time
{
title: '<at_exit> cpu profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_cpu_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -59,7 +62,7 @@ exports = module.exports = function () {
},
{
title: '<at_exit> heap profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_heap_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -71,7 +74,7 @@ exports = module.exports = function () {
},
{
title: '<at_exit> gc profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_gc_profiling',
options: { profiling_time: 5 * 60 * 1000 },
Expand All @@ -85,7 +88,7 @@ exports = module.exports = function () {
// exit 0 with no profiling time
{
title: '<at_exit> cpu profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_cpu_profiling',
options: undefined,
Expand All @@ -97,7 +100,7 @@ exports = module.exports = function () {
},
{
title: '<at_exit> heap profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_heap_profiling',
options: undefined,
Expand All @@ -109,7 +112,7 @@ exports = module.exports = function () {
},
{
title: '<at_exit> gc profiling',
jspath: path.join(__dirname, 'exit.js'),
jspath: exitNormalScriptPath,
tid: 0,
cmd: 'start_gc_profiling',
options: undefined,
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/exit.js → test/fixtures/scripts/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const os = require('os');
const mm = require('mm');
const xprofiler = require('../../');
const utils = require('./utils');
const xprofiler = require('../../../');
const utils = require('../utils');

if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
Expand All @@ -15,4 +15,4 @@ process.send({ type: utils.clientConst.xprofilerDone });

setTimeout(() => {
// empty exit
}, 2000);
}, 5000);
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const os = require('os');
const mm = require('mm');
const xprofiler = require('../../');
const utils = require('./utils');
const xprofiler = require('../../../');
const utils = require('../utils');

if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const mm = require('mm');
const moment = require('moment');
const { v4: uuid } = require('uuid');
const traceid = uuid();
const utils = require('./utils');
const utils = require('../utils');
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, traceid, 'blocking start.');

const xprofiler = require('../../');
const xprofiler = require('../../../');

if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
Expand Down Expand Up @@ -38,7 +38,7 @@ process.send({ type: utils.clientConst.xprofilerDone });

/*eslint no-empty: "off"*/
const start = Date.now();
while (Date.now() - start < 8000) {
while (Date.now() - start < 10000) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const mm = require('mm');
const os = require('os');
const http = require('http');
const utils = require('./utils');
const xprofiler = require('../../');
const utils = require('../utils');
const xprofiler = require('../../../');

if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
Expand Down Expand Up @@ -35,7 +35,7 @@ server.unref();

function sendRequest(abort) {
const req = http.request('http://localhost:8445');
req.on('error', err => console.error('non-blocking', err.message));
req.on('error', err => console.error('normal process', err.message));
req.end();

if (abort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const os = require('os');
const mm = require('mm');
const moment = require('moment');
const { v4: uuid } = require('uuid');
const utils = require('./utils');
const utils = require('../utils');

const traceid = uuid();

if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
}

const xprofiler = require('../../xprofiler');
const xprofiler = require('../../../');
xprofiler.start({ check_throw: false });

if (workerThreads.isMainThread) {
Expand All @@ -30,7 +30,7 @@ if (workerThreads.isMainThread) {
console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, traceid, 'blocking start.');

const start = Date.now();
while (Date.now() - start < 8000) { /** ignore */ }
while (Date.now() - start < 10000) { /** ignore */ }

console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, traceid, 'blocking done.');
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR) {
mm(os, 'homedir', () => process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR);
}

const xprofiler = require('../../xprofiler');
const xprofiler = require('../../../');
xprofiler.start();

if (workerThreads.isMainThread) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const workerThreads = require('worker_threads');
require('../../xprofiler');
require('../../../').start();

if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename);
w.on('exit', code => {
console.log('worker exited', code);
});
} else {
setTimeout(() => {}, 1000);
setTimeout(() => { }, 1000);
}
3 changes: 3 additions & 0 deletions test/fixtures/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ exports.arrayEqual = function (arr1, arr2) {

exports.getChildProcessExitInfo = function (proc) {
return new Promise(resolve => {
if (!proc.connected) {
return resolve({ code: proc.exitCode, signal: proc.signalCode });
}
if (proc.exitCode !== null) {
return resolve({ code: proc.exitCode, signal: proc.signalCode });
}
Expand Down
Loading

0 comments on commit 333593e

Please sign in to comment.