Skip to content

Commit 703470a

Browse files
mmarchiniPeter Marton
authored andcommitted
feat(chain): use nextTick instead of setImmediate (#1808)
1 parent 9ea8227 commit 703470a

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

lib/chain.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ Chain.prototype.run = function run(req, res, done) {
112112

113113
// all done or request closed
114114
if (!handler || req.closed()) {
115-
setImmediate(done, err, req, res);
115+
process.nextTick(function nextTick() {
116+
return done(err, req, res);
117+
});
116118
return;
117119
}
118120

test/request.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,14 @@ test('should provide date when request started', function(t) {
227227
// restifyDone is emitted at the same time when server's after event is emitted,
228228
// you can find more comprehensive testing for `after` lives in server tests.
229229
test('should emit restifyDone event when request is fully served', function(t) {
230-
var clientDone = false;
230+
var restifyDoneCalled = false;
231231

232232
SERVER.get('/', function(req, res, next) {
233233
req.on('restifyDone', function(route, err) {
234234
t.ifError(err);
235235
t.ok(route);
236236
setImmediate(function() {
237-
t.ok(clientDone);
238-
t.end();
237+
restifyDoneCalled = true;
239238
});
240239
});
241240

@@ -246,7 +245,8 @@ test('should emit restifyDone event when request is fully served', function(t) {
246245
CLIENT.get('/', function(err, _, res) {
247246
t.ifError(err);
248247
t.equal(res.statusCode, 200);
249-
clientDone = true;
248+
t.ok(restifyDoneCalled);
249+
t.end();
250250
});
251251
});
252252

test/server.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ test('calling next(false) should early exit from use handlers', function(t) {
18201820

18211821
SERVER.on('after', function() {
18221822
steps++;
1823-
t.equal(steps, 2);
1823+
t.equal(steps, 1);
18241824
t.end();
18251825
});
18261826

@@ -2111,7 +2111,7 @@ test('should increment/decrement inflight request count', function(t) {
21112111
CLIENT.get('/foo', function(err, _, res) {
21122112
t.ifError(err);
21132113
t.equal(res.statusCode, 200);
2114-
t.equal(SERVER.inflightRequests(), 1);
2114+
t.equal(SERVER.inflightRequests(), 0);
21152115
});
21162116
});
21172117

@@ -2135,14 +2135,14 @@ test('should increment/decrement inflight request count for concurrent reqs', fu
21352135
CLIENT.get('/foo1', function(err, _, res) {
21362136
t.ifError(err);
21372137
t.equal(res.statusCode, 200);
2138-
t.equal(SERVER.inflightRequests(), 1);
2138+
t.equal(SERVER.inflightRequests(), 0);
21392139
t.end();
21402140
});
21412141

21422142
CLIENT.get('/foo2', function(err, _, res) {
21432143
t.ifError(err);
21442144
t.equal(res.statusCode, 200);
2145-
t.equal(SERVER.inflightRequests(), 2);
2145+
t.equal(SERVER.inflightRequests(), 1);
21462146
});
21472147
});
21482148

@@ -2174,7 +2174,7 @@ test('should cleanup inflight requests count for 404s', function(t) {
21742174
CLIENT.get('/foo1', function(err, _, res) {
21752175
t.ifError(err);
21762176
t.equal(res.statusCode, 200);
2177-
t.equal(SERVER.inflightRequests(), 1);
2177+
t.equal(SERVER.inflightRequests(), 0);
21782178

21792179
CLIENT.get('/doesnotexist', function(err2, _2, res2) {
21802180
t.ok(err2);
@@ -2219,7 +2219,7 @@ test('should cleanup inflight requests count for timeouts', function(t) {
22192219
CLIENT.get('/foo2', function(err, _, res) {
22202220
t.ifError(err);
22212221
t.equal(res.statusCode, 200);
2222-
t.equal(SERVER.inflightRequests(), 2);
2222+
t.equal(SERVER.inflightRequests(), 1);
22232223
});
22242224
});
22252225

@@ -2862,7 +2862,7 @@ test('inflightRequest accounting stable with firstChain', function(t) {
28622862
for (var i = 0; i < results.length; i++) {
28632863
// The shed request should always be returned first, since it isn't
28642864
// handled by SERVER.get
2865-
if (i === 0) {
2865+
if (i === 1) {
28662866
t.equal(
28672867
results[i].statusCode,
28682868
413,

0 commit comments

Comments
 (0)