Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 committed Dec 8, 2022
1 parent e18f610 commit d6e99aa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
15 changes: 12 additions & 3 deletions test/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -73,7 +77,7 @@ describe('commands', () => {
XPROFILER_UNIT_TEST_TMP_HOMEDIR: tmphome,
XPROFILER_LOG_LEVEL: 2,
XPROFILER_LOG_TYPE: 1
})
}, env)
});
pid = p.pid;

Expand Down Expand Up @@ -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);
}

Expand Down
22 changes: 21 additions & 1 deletion test/fixtures/cases/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) +
Expand Down
7 changes: 5 additions & 2 deletions test/fixtures/scripts/process_normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -53,7 +56,7 @@ const interval = setInterval(() => {
sendRequest();
}
times++;
}, 150);
}, 10);
interval.unref();

setTimeout(() => {
Expand Down
15 changes: 14 additions & 1 deletion test/patch/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit d6e99aa

Please sign in to comment.