Skip to content

test: import test-http-server-keep-alive-timeout #13448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 114 additions & 69 deletions test/parallel/test-http-server-keep-alive-timeout.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,122 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
module.exports.testServerKeepAliveTimeout = function(common, isHttps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md
put const common = require('../common'); as L2 then a blank line and then a comment

// Testing http[s] server keepalive semantics.
// Reusing tests with test-https-server-keep-alive-timeout.js
// Ref: https://github.com/nodejs/node/pull/13448

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test file, not a library file. It shouldn't be exporting. -100 to exporting something to another test file from it.

let http;
let net;
let createOptions;
let serverOptions;
let createServer;

const tests = [];
if (isHttps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the only if id do this separately for each file, and pass all the objects as args.

if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}

function test(fn) {
if (!tests.length) {
process.nextTick(run);
}
tests.push(fn);
}

function run() {
const fn = tests.shift();
if (fn) fn(run);
}

test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', () => {
assert.strictEqual(requestCount, 3);
});
const server = http.createServer((req, res) => {
requestCount++;
res.end();
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true
http = require('https');
net = require('tls');

createOptions = function(server) {
return {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
};
const c = net.connect(options, () => {
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');
});
}));
});

test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', () => {
assert.strictEqual(requestCount, 3);
});
const server = http.createServer((req, res) => {
requestCount++;
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true
const fs = require('fs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then move all the require to the top

serverOptions = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};
const c = net.connect(options, () => {
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');

createServer = function(fn) {
return http.createServer(serverOptions, fn);
};


} else {
http = require('http');
net = require('net');

createOptions = function(server) {
return {
port: server.address().port,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the indentation is off here.

allowHalfOpen: true,
};
};

createServer = function(fn) {
return http.createServer(fn);
};

}

const assert = require('assert');
const tests = [];

function test(fn) {
if (!tests.length) {
process.nextTick(run);
}
tests.push(fn);
}

function run() {
const fn = tests.shift();
if (fn) fn(run);
}

test(function serverKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', function() {
assert.strictEqual(requestCount, 3);
});
const server = createServer((req, res) => {
requestCount++;
res.end();
});
}));
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const c = net.connect(createOptions(server), () => {
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');
});
}));
});

test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', () => {
assert.strictEqual(requestCount, 3);
});
const server = createServer((req, res) => {
requestCount++;
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const c = net.connect(createOptions(server), () => {
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');
});
}));
});
};


const common = require('../common');
module.exports.testServerKeepAliveTimeout(common, false);
88 changes: 2 additions & 86 deletions test/parallel/test-https-server-keep-alive-timeout.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,5 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const assert = require('assert');
const https = require('https');
const tls = require('tls');
const fs = require('fs');

const tests = [];

const serverOptions = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

function test(fn) {
if (!tests.length) {
process.nextTick(run);
}
tests.push(fn);
}

function run() {
const fn = tests.shift();
if (fn) fn(run);
}

test(function serverKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', function() {
assert.strictEqual(requestCount, 3);
});
const server = https.createServer(serverOptions, (req, res) => {
requestCount++;
res.end();
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
const c = tls.connect(options, () => {
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');
});
}));
});

test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', () => {
assert.strictEqual(requestCount, 3);
});
const server = https.createServer(serverOptions, (req, res) => {
requestCount++;
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
const c = tls.connect(options, () => {
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');
});
}));
});
const timeoutTest = require('./test-http-server-keep-alive-timeout');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this could be moved to other file as well, and delete this one.
So other file will be:

const setup http, net, createOptions, serverOptions, createServer;
testServerKeepAliveTimeout(http, net, createOptions, serverOptions, createServer)
const https, tls, createOptions, serverOptions, createServer;
testServerKeepAliveTimeout(https, tls, createOptions, serverOptions, createServer)

This has the benefit of eliminating node startup time (that could be ~2s)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use require() to load an adjacent test file as a module

timeoutTest.testServerKeepAliveTimeout(common, true);