Skip to content

Commit d0cf7c1

Browse files
authored
fix: there is no uv_handle_type_name in earlier versions of libuv
PR-URL: https://github.com/hyj1991/xprofiler/pull/45 Reviewed-By: hyj1991 <yeekwanvong@gmail.com>
1 parent 42344e8 commit d0cf7c1

File tree

9 files changed

+141
-29
lines changed

9 files changed

+141
-29
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"build": "npm run lint && node-gyp rebuild",
1111
"format": "clang-format -i --glob=\"src/**/!(report_win)[.h|.cc]\"",
1212
"lint": "npm run format && eslint --fix xprofiler.js \"test/**/*.js\" lib/*.js patch/*.js bin/xprofctl scripts/**/*.js",
13-
"test": "mocha -t 0 -R spec test/*.test.js",
13+
"test": "mocha -t 0 -R spec test/*.test.js test/patch/*.test.js",
1414
"test-single": "mocha -t 0 -R spec",
15-
"cov": "nyc --reporter=html --reporter=text --reporter=lcov mocha -R spec test/*.test.js --timeout 0",
15+
"cov": "nyc --reporter=html --reporter=text --reporter=lcov mocha -R spec test/*.test.js test/patch/*.test.js --timeout 0",
1616
"cov-single": "nyc --reporter=html --reporter=text --reporter=lcov mocha --timeout 0 -R spec",
1717
"upload": "node scripts/upload.js",
1818
"ci": "npm run lint && npm run cov && codecov && npm run upload",

patch/http.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ function serverWrapper(addLiveRequest, addCloseRequest, addSentRequest, original
2626

2727
if (typeof opts === 'function') {
2828
args.splice(0, 1, requestListenerWrapper(opts, addLiveRequest, addCloseRequest, addSentRequest));
29-
returned = original.apply(this, args);
30-
}
31-
if (typeof requestListener === 'function') {
29+
} else if (typeof requestListener === 'function') {
3230
args.splice(1, 1, requestListenerWrapper(requestListener, addLiveRequest, addCloseRequest, addSentRequest));
33-
returned = original.apply(this, args);
3431
}
3532

33+
returned = original.apply(this, args);
34+
3635
return returned;
3736
};
3837
}

src/commands/parser.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ using nlohmann::json;
1313
using std::exception;
1414
using std::string;
1515

16-
#define HANDLE_COMMANDS(cmd_str, handle) \
17-
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
18-
handle(parsed, FmtMessage, \
19-
[traceid](json data) { SuccessValue(traceid, data); }, \
20-
[traceid](string message) { ErrorValue(traceid, message); }); \
21-
handled = true; \
16+
#define HANDLE_COMMANDS(cmd_str, handle) \
17+
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
18+
handle( \
19+
parsed, FmtMessage, \
20+
[traceid](json data) { SuccessValue(traceid, data); }, \
21+
[traceid](string message) { ErrorValue(traceid, message); }); \
22+
handled = true; \
2223
}
2324

2425
void ParseCmd(char *command) {

src/commands/report/uv_statistics.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,43 +227,60 @@ static void reportEndpoints(uv_handle_t *h, ostringstream &out) {
227227

228228
static void walkHandle(uv_handle_t *h, void *arg) {
229229
uv_any_handle *handle = reinterpret_cast<uv_any_handle *>(h);
230+
230231
ostringstream data;
232+
// const char *type = uv_handle_type_name(h->type);
233+
string type;
231234

232235
switch (h->type) {
233236
case UV_UNKNOWN_HANDLE:
237+
type = "unknown";
234238
break;
235239
case UV_ASYNC:
240+
type = "async";
236241
break;
237242
case UV_CHECK:
243+
type = "check";
238244
break;
239245
case UV_FS_EVENT: {
246+
type = "fs_event";
240247
reportPath(h, data);
241248
break;
242249
}
243250
case UV_FS_POLL: {
251+
type = "fs_poll";
244252
reportPath(h, data);
245253
break;
246254
}
247255
case UV_HANDLE:
256+
type = "handle";
248257
break;
249258
case UV_IDLE:
259+
type = "idle";
250260
break;
251261
case UV_NAMED_PIPE:
262+
type = "pipe";
252263
break;
253264
case UV_POLL:
265+
type = "poll";
254266
break;
255267
case UV_PREPARE:
268+
type = "prepare";
256269
break;
257270
case UV_PROCESS:
271+
type = "process";
258272
data << "pid: " << handle->process.pid;
259273
break;
260274
case UV_STREAM:
275+
type = "stream";
261276
break;
262277
case UV_TCP: {
278+
type = "tcp";
263279
reportEndpoints(h, data);
264280
break;
265281
}
266282
case UV_TIMER: {
283+
type = "timer";
267284
#if defined(_WIN32) && (UV_VERSION_HEX < ((1 << 16) | (22 << 8)))
268285
uint64_t due = handle->timer.due;
269286
#else
@@ -279,6 +296,7 @@ static void walkHandle(uv_handle_t *h, void *arg) {
279296
break;
280297
}
281298
case UV_TTY: {
299+
type = "tty";
282300
int height, width, rc;
283301
rc = uv_tty_get_winsize(&(handle->tty), &width, &height);
284302
if (rc == 0) {
@@ -287,17 +305,21 @@ static void walkHandle(uv_handle_t *h, void *arg) {
287305
break;
288306
}
289307
case UV_UDP: {
308+
type = "udp";
290309
reportEndpoints(h, data);
291310
break;
292311
}
293312
case UV_SIGNAL: {
313+
type = "signal";
294314
data << "signum: " << handle->signal.signum << " ("
295315
<< SignoString(handle->signal.signum) << ")";
296316
break;
297317
}
298318
case UV_FILE:
319+
type = "file";
299320
break;
300321
case UV_HANDLE_TYPE_MAX:
322+
type = "max";
301323
break;
302324
}
303325

@@ -349,7 +371,6 @@ static void walkHandle(uv_handle_t *h, void *arg) {
349371
}
350372

351373
JSONWriter *writer = static_cast<JSONWriter *>(arg);
352-
const char *type = uv_handle_type_name(h->type);
353374
string detail = data.str();
354375

355376
writer->json_start();

src/logbypass/http.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include "uv.h"
1+
#include "http.h"
22

33
#include "../logger.h"
4-
#include "http.h"
4+
#include "uv.h"
55

66
namespace xprofiler {
77
using Nan::To;

test/fixtures/non-blocking.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ xprofiler.setHooks();
2424
xprofiler.setHooks();
2525

2626
// http server
27-
const server1 = http.createServer(function (req, res) {
27+
const server = http.createServer(function (req, res) {
2828
setTimeout(() => res.end('hello world.'), 100);
2929
});
30-
server1.listen(8443, () => console.log('http server listen at 8443...'));
31-
server1.unref();
32-
33-
const server2 = http.createServer({}, function (req, res) { res.end('hello world.'); });
34-
server2.listen(9443, () => console.log('http server listen at 9443...'));
35-
server2.unref();
30+
server.listen(8443, () => console.log('http server listen at 8443...'));
31+
server.unref();
3632

3733
function sendRequest(abort) {
3834
const req = http.request('http://localhost:8443');
@@ -62,8 +58,7 @@ setTimeout(() => {
6258
clearInterval(interval);
6359
console.log('will close...');
6460
setTimeout(() => {
65-
server1.close();
66-
server2.close();
61+
server.close();
6762
console.log('closed');
6863
}, 200);
6964
}, 8000);

test/patch/http.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
3+
const http = require('http');
4+
const EventEmitter = require('events').EventEmitter;
5+
const mm = require('mm');
6+
const expect = require('expect.js');
7+
const { patchHttp } = require('../../patch/http');
8+
9+
10+
describe(`patch http.createServer(cb)`, function () {
11+
const requestTimes = 5;
12+
let triggerTimes = 0;
13+
14+
let liveRequest = 0;
15+
let closeRequest = 0;
16+
let sentRequest = 0;
17+
18+
function addLiveRequest() {
19+
liveRequest++;
20+
}
21+
22+
function addCloseRequest() {
23+
closeRequest++;
24+
}
25+
26+
function addSentRequest() {
27+
sentRequest++;
28+
}
29+
30+
function mockCreateServer(opts, requestHandle) {
31+
return new Promise(resolve => {
32+
let times = 0;
33+
const interval = setInterval(() => {
34+
if (times < requestTimes) {
35+
const request = new EventEmitter();
36+
const response = new EventEmitter();
37+
if (typeof opts === 'function') {
38+
opts(request, response);
39+
} else if (typeof requestHandle === 'function') {
40+
requestHandle(request, response);
41+
}
42+
times++;
43+
} else {
44+
clearInterval(interval);
45+
resolve();
46+
}
47+
}, 100);
48+
});
49+
}
50+
51+
before(async function () {
52+
mm(http, 'createServer', mockCreateServer);
53+
patchHttp(addLiveRequest, addCloseRequest, addSentRequest);
54+
await http.createServer(function (request, response) {
55+
triggerTimes++;
56+
response.emit('finish');
57+
response.emit('close');
58+
});
59+
60+
await http.createServer({}, function (request, response) {
61+
triggerTimes++;
62+
response.emit('finish');
63+
response.emit('close');
64+
});
65+
66+
await http.createServer({}, {}, function (request, response) {
67+
triggerTimes++;
68+
response.emit('finish');
69+
response.emit('close');
70+
});
71+
});
72+
73+
after(function () {
74+
mm.restore();
75+
});
76+
77+
it('patch should be ok', function () {
78+
expect(http.createServer).not.to.be(mockCreateServer);
79+
});
80+
81+
it(`request handler should trigger ${requestTimes} * 2 times`, function () {
82+
expect(triggerTimes).to.be(requestTimes * 2);
83+
});
84+
85+
it(`live request shoule be ${requestTimes} * 2`, function () {
86+
expect(liveRequest).to.be(requestTimes * 2);
87+
});
88+
89+
it(`close request shoule be ${requestTimes} * 2`, function () {
90+
expect(closeRequest).to.be(requestTimes * 2);
91+
});
92+
93+
it(`sent request shoule be ${requestTimes} * 2`, function () {
94+
expect(sentRequest).to.be(requestTimes * 2);
95+
});
96+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const expect = require('expect.js');
4-
const { wrap, unwrap } = require('../patch/shimmer');
4+
const { wrap, unwrap } = require('../../patch/shimmer');
55

66
describe('wrap / unwrap module', function () {
77
it('wrap module failed with wrong params', function () {

test/start.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe(`xprofiler starting`, function () {
5151
expect(aliveProcess[1]).to.be(logdir);
5252
});
5353

54-
it(`.xprofiler cwd: ${aliveProcess[2]} should be ${/^([.\w()/\\:]+|)$/}`, function () {
55-
expect(/^([.\w()/\\:]+|)$/.test(aliveProcess[2])).to.be.ok();
54+
it(`.xprofiler cwd: ${aliveProcess[2]} should be ${/^([.\w()/\\:-]+|)$/}`, function () {
55+
expect(/^([.\w()/\\:-]+|)$/.test(aliveProcess[2])).to.be.ok();
5656
});
5757

5858
it(`.xprofiler executable: ${aliveProcess[3]} should be node-${process.version}`, function () {
@@ -64,8 +64,8 @@ describe(`xprofiler starting`, function () {
6464
expect(version).to.be(process.version);
6565
});
6666

67-
it(`.xprofiler file: ${aliveProcess[4]} should be ${/^([.\w()/\\:]+|)$/}`, function () {
68-
expect(/^([.\w()/\\:]+|)$/.test(aliveProcess[4])).to.be.ok();
67+
it(`.xprofiler file: ${aliveProcess[4]} should be ${/^([.\w()/\\:-]+|)$/}`, function () {
68+
expect(/^([.\w()/\\:-]+|)$/.test(aliveProcess[4])).to.be.ok();
6969
});
7070

7171
it(`.xprofiler module path: ${aliveProcess[5]} should be ${path.join(__dirname, '..')}`, function () {

0 commit comments

Comments
 (0)