Skip to content

Commit

Permalink
test: make http(s)-set-timeout-server more similar
Browse files Browse the repository at this point in the history
Make test-http(s)-set-timeout-server tests more similar and resolve the
following issues:

* `test-https-set-timeout-server.js` was missing some `assert`
   statements, including with `http` module

* Both files were missing some calls to `common.mustCall()`

* Both files were calling `createServer()` in different ways

PR-URL: #13822
Refs: #13588
Refs: #13625
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
jklepatch authored and addaleax committed Jul 18, 2017
1 parent 587c905 commit 2659573
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 85 deletions.
74 changes: 43 additions & 31 deletions test/parallel/test-http-set-timeout-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,70 +42,76 @@ function run() {
}

test(function serverTimeout(cb) {
const server = http.createServer(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
});
server.listen(common.mustCall(function() {
http.get({ port: server.address().port }).on('error', common.mustCall());
}));
const s = server.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
server.listen(common.mustCall(function() {
const s = server.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.Server);
http.get({
port: server.address().port
}).on('error', common.mustCall());
}));
assert.ok(s instanceof http.Server);
});

test(function serverRequestTimeout(cb) {
const server = http.createServer(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
const s = req.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.IncomingMessage);
});
}));
server.listen(common.mustCall(function() {
const port = server.address().port;
const req = http.request({ port: port, method: 'POST' });
const req = http.request({
port: server.address().port,
method: 'POST'
});
req.on('error', common.mustCall());
req.write('Hello');
// req is in progress
}));
});

test(function serverResponseTimeout(cb) {
const server = http.createServer(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
const s = res.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.OutgoingMessage);
});
}));
server.listen(common.mustCall(function() {
const port = server.address().port;
http.get({ port: port }).on('error', common.mustCall());
http.get({
port: server.address().port
}).on('error', common.mustCall());
}));
});

test(function serverRequestNotTimeoutAfterEnd(cb) {
const server = http.createServer(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
const s = req.setTimeout(50, common.mustNotCall());
assert.ok(s instanceof http.IncomingMessage);
res.on('timeout', common.mustCall());
});
server.on('timeout', function(socket) {
}));
server.on('timeout', common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
});
}));
server.listen(common.mustCall(function() {
const port = server.address().port;
http.get({ port: port }).on('error', common.mustCall());
http.get({
port: server.address().port
}).on('error', common.mustCall());
}));
});

Expand All @@ -124,16 +130,19 @@ test(function serverResponseTimeoutWithPipeline(cb) {
assert.ok(s instanceof http.OutgoingMessage);
if (req.url === '/1') res.end();
});
server.on('timeout', function(socket) {
server.on('timeout', common.mustCall(function(socket) {
if (secReceived) {
socket.destroy();
server.close();
cb();
}
});
}));
server.listen(common.mustCall(function() {
const port = server.address().port;
const c = net.connect({ port: port, allowHalfOpen: true }, function() {
const options = {
port: server.address().port,
allowHalfOpen: true,
};
const c = net.connect(options, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
Expand All @@ -142,20 +151,23 @@ test(function serverResponseTimeoutWithPipeline(cb) {
});

test(function idleTimeout(cb) {
const server = http.createServer(function(req, res) {
const server = http.createServer(common.mustCall(function(req, res) {
req.on('timeout', common.mustNotCall());
res.on('timeout', common.mustNotCall());
res.end();
});
}));
const s = server.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.Server);
server.listen(common.mustCall(function() {
const port = server.address().port;
const c = net.connect({ port: port, allowHalfOpen: true }, function() {
const options = {
port: server.address().port,
allowHalfOpen: true,
};
const c = net.connect(options, function() {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
Expand Down
122 changes: 68 additions & 54 deletions test/sequential/test-https-set-timeout-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (!common.hasCrypto) {
const assert = require('assert');
const fs = require('fs');
const https = require('https');
const http = require('http');
const tls = require('tls');

const tests = [];
Expand All @@ -56,83 +57,93 @@ function run() {
}

test(function serverTimeout(cb) {
const server = https.createServer(serverOptions, function(req, res) {
// just do nothing, we should get a timeout event.
});
server.listen(0, common.mustCall(function() {
const server = https.createServer(
serverOptions,
common.mustCall(function(req, res) {
// just do nothing, we should get a
// timeout event.
}));
server.listen(common.mustCall(function() {
const s = server.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof https.Server);
https.get({
port: this.address().port,
port: server.address().port,
rejectUnauthorized: false
}).on('error', common.mustCall());
}));
});

test(function serverRequestTimeout(cb) {
function handler(req, res) {
// just do nothing, we should get a timeout event.
req.setTimeout(50, common.mustCall(function() {
req.socket.destroy();
server.close();
cb();
const server = https.createServer(
serverOptions,
common.mustCall(function(req, res) {
// just do nothing, we should get a
// timeout event.
const s = req.setTimeout(
50,
common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.IncomingMessage);
}));
}

const server = https.createServer(serverOptions, common.mustCall(handler));
server.listen(0, function() {
server.listen(common.mustCall(function() {
const req = https.request({
port: this.address().port,
port: server.address().port,
method: 'POST',
rejectUnauthorized: false
});
req.on('error', common.mustCall());
req.write('Hello');
// req is in progress
});
}));
});

test(function serverResponseTimeout(cb) {
function handler(req, res) {
// just do nothing, we should get a timeout event.
res.setTimeout(50, common.mustCall(function() {
res.socket.destroy();
server.close();
cb();
const server = https.createServer(
serverOptions,
common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
const s = res.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.OutgoingMessage);
}));
}

const server = https.createServer(serverOptions, common.mustCall(handler));
server.listen(0, function() {
server.listen(common.mustCall(function() {
https.get({
port: this.address().port,
port: server.address().port,
rejectUnauthorized: false
}).on('error', common.mustCall());
});
}));
});

test(function serverRequestNotTimeoutAfterEnd(cb) {
function handler(req, res) {
// just do nothing, we should get a timeout event.
req.setTimeout(50, common.mustNotCall());
res.on('timeout', common.mustCall());
}
const server = https.createServer(serverOptions, common.mustCall(handler));
server.on('timeout', function(socket) {
const server = https.createServer(
serverOptions,
common.mustCall(function(req, res) {
// just do nothing, we should get a timeout event.
const s = req.setTimeout(50, common.mustNotCall());
assert.ok(s instanceof http.IncomingMessage);
res.on('timeout', common.mustCall());
}));
server.on('timeout', common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
});
server.listen(0, function() {
}));
server.listen(common.mustCall(function() {
https.get({
port: this.address().port,
port: server.address().port,
rejectUnauthorized: false
}).on('error', common.mustCall());
});
}));
});

test(function serverResponseTimeoutWithPipeline(cb) {
Expand All @@ -144,9 +155,10 @@ test(function serverResponseTimeoutWithPipeline(cb) {
const server = https.createServer(serverOptions, function(req, res) {
if (req.url === '/2')
secReceived = true;
res.setTimeout(50, function() {
const s = res.setTimeout(50, function() {
caughtTimeout += req.url;
});
assert.ok(s instanceof http.OutgoingMessage);
if (req.url === '/1') res.end();
});
server.on('timeout', function(socket) {
Expand All @@ -156,9 +168,9 @@ test(function serverResponseTimeoutWithPipeline(cb) {
cb();
}
});
server.listen(0, function() {
server.listen(common.mustCall(function() {
const options = {
port: this.address().port,
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
Expand All @@ -167,30 +179,32 @@ test(function serverResponseTimeoutWithPipeline(cb) {
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
});
});
}));
});

test(function idleTimeout(cb) {
const server = https.createServer(serverOptions,
common.mustCall(function(req, res) {
req.on('timeout', common.mustNotCall());
res.on('timeout', common.mustNotCall());
res.end();
}));
server.setTimeout(50, common.mustCall(function(socket) {
const server = https.createServer(
serverOptions,
common.mustCall(function(req, res) {
req.on('timeout', common.mustNotCall());
res.on('timeout', common.mustNotCall());
res.end();
}));
const s = server.setTimeout(50, common.mustCall(function(socket) {
socket.destroy();
server.close();
cb();
}));
server.listen(0, function() {
assert.ok(s instanceof https.Server);
server.listen(common.mustCall(function() {
const options = {
port: this.address().port,
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
tls.connect(options, function() {
this.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
});
}));
});

0 comments on commit 2659573

Please sign in to comment.