From d6e99aa91999c398809445e29ce5c945e96697c4 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 8 Dec 2022 21:38:42 +0800 Subject: [PATCH] test: add test --- test/commands.test.js | 15 ++++++++++++--- test/fixtures/cases/command.js | 22 +++++++++++++++++++++- test/fixtures/scripts/process_normal.js | 7 +++++-- test/patch/http.test.js | 15 ++++++++++++++- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/test/commands.test.js b/test/commands.test.js index 34ea3f5..fbc99ca 100644 --- a/test/commands.test.js +++ b/test/commands.test.js @@ -51,12 +51,16 @@ function convertOptions(options) { describe('commands', () => { for (let i = 0; i < testConfig.length; i++) { - const { cmd, options = {}, profileRules, errored = false, xctlRules, xprofctlRules, platform } = testConfig[i]; + const { cmd, options = {}, profileRules, profileCheck, + errored = false, xctlRules, xprofctlRules, platform, env = {} } = testConfig[i]; for (let j = 0; j < testFiles.length; j++) { const { jspath, desc, threadId = 0 } = testFiles[j]; const ospt = platform || currentPlatform; const title = - `[${ospt}] execute [${cmd}] on thread(${threadId}) with options: ${JSON.stringify(options)} ${desc}`; + `[${ospt}] execute [${cmd}] on thread(${threadId}) with ` + + `options: ${JSON.stringify(options)}, ` + + `env: ${JSON.stringify(env)} ` + + desc; describe(title, function () { const commandExpiredTime = 5000; let resByXctl = ''; @@ -73,7 +77,7 @@ describe('commands', () => { XPROFILER_UNIT_TEST_TMP_HOMEDIR: tmphome, XPROFILER_LOG_LEVEL: 2, XPROFILER_LOG_TYPE: 1 - }) + }, env) }); pid = p.pid; @@ -153,6 +157,11 @@ describe('commands', () => { // check dump file if (profileRules && typeof profileRules === 'object') { const profile = JSON.parse(fs.readFileSync(resByXctl.data.filepath, 'utf8')); + if (typeof profileCheck === 'function' && jspath.includes('normal')) { + it(`profile content check should be ok`, function () { + expect(profileCheck(profile)).to.be.ok(); + }); + } checkProfile(profileRules, profile); } diff --git a/test/fixtures/cases/command.js b/test/fixtures/cases/command.js index b29eb93..ba9af7f 100644 --- a/test/fixtures/cases/command.js +++ b/test/fixtures/cases/command.js @@ -90,7 +90,8 @@ const cpuprofile = { startTime: REGEXP_NUMBER, endTime: REGEXP_NUMBER, samples: [REGEXP_NUMBER], - timeDeltas: [REGEXP_NUMBER] + timeDeltas: [REGEXP_NUMBER], + httpDetail: [/(\d+),(\S+),(GET|POST),(1|0),(\d+),(\d+)/] }; const heapsnapshot = { @@ -292,6 +293,25 @@ exports = module.exports = function (logdir) { cmd: 'start_cpu_profiling', options: { profiling_time: 1000 }, profileRules: cpuprofile, + profileCheck(profile) { + return profile.httpDetail.length === 0; + }, + xctlRules(data) { + return [{ + key: 'data.filepath', rule: new RegExp(escape(data.logdir + sep) + + `x-cpuprofile-${data.pid}-${moment().format('YYYYMMDD')}-(\\d+).cpuprofile`) + }]; + }, + xprofctlRules() { return []; } + }, + { + cmd: 'start_cpu_profiling', + options: { profiling_time: 1000 }, + env: { XPROFILER_ENABLE_HTTP_PROFILING: 'YES' }, + profileRules: cpuprofile, + profileCheck(profile) { + return profile.httpDetail.length !== 0; + }, xctlRules(data) { return [{ key: 'data.filepath', rule: new RegExp(escape(data.logdir + sep) + diff --git a/test/fixtures/scripts/process_normal.js b/test/fixtures/scripts/process_normal.js index 95b0104..3554841 100644 --- a/test/fixtures/scripts/process_normal.js +++ b/test/fixtures/scripts/process_normal.js @@ -33,9 +33,12 @@ const server = http.createServer(function (req, res) { server.listen(8445, () => console.log('http server listen at 8445...')); server.unref(); +let logHttpRequestErrorOnce = false; + function sendRequest(abort) { const req = http.request('http://localhost:8445'); - req.on('error', err => console.error('normal process', err.message)); + req.on('error', err => !logHttpRequestErrorOnce && (logHttpRequestErrorOnce = true) + && console.error('normal process', err.message)); req.end(); if (abort) { @@ -53,7 +56,7 @@ const interval = setInterval(() => { sendRequest(); } times++; -}, 150); +}, 10); interval.unref(); setTimeout(() => { diff --git a/test/patch/http.test.js b/test/patch/http.test.js index c435cc3..6808680 100644 --- a/test/patch/http.test.js +++ b/test/patch/http.test.js @@ -12,10 +12,15 @@ describe(`patch http.createServer(cb)`, function () { const requestTimes = 5; let triggerTimes = 0; + let httpConfig = {}; let liveRequest = 0; let closeRequest = 0; let sentRequest = 0; + function setHttpConfig(config) { + httpConfig = config; + } + function addLiveRequest() { liveRequest++; } @@ -59,7 +64,7 @@ describe(`patch http.createServer(cb)`, function () { before(async function () { mm(http, 'createServer', mockCreateServer); - patchHttp({ addLiveRequest, addCloseRequest, addSentRequest, addHttpStatusCode }); + patchHttp({ setHttpConfig, addLiveRequest, addCloseRequest, addSentRequest, addHttpStatusCode }); await http.createServer(function (request, response) { triggerTimes++; response.statusCode = 200; @@ -90,6 +95,14 @@ describe(`patch http.createServer(cb)`, function () { expect(http.createServer).not.to.be(mockCreateServer); }); + it('http config.http_detail_profiling should be false', function () { + expect(httpConfig.http_detail_profiling).to.be(false); + }); + + it('http config.start_time should be 0', function () { + expect(httpConfig.start_time).to.be(0); + }); + it(`request handler should trigger ${requestTimes} * 2 times`, function () { expect(triggerTimes).to.be(requestTimes * 2); });