Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Revert "test: refactor test-http-server-keep-alive-timeout"
This reverts commit f0ef3e6.
  • Loading branch information
realwakka committed Jun 9, 2017
commit c2f3f7eebfa54b19d5ce86d8d87a5dfb1cfceaa9
183 changes: 69 additions & 114 deletions test/parallel/test-http-server-keep-alive-timeout.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,77 @@
'use strict';

module.exports.testServerKeepAliveTimeout = function(common, isHttps) {
let http;
let net;
let createOptions;
let serverOptions;
let createServer;

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

http = require('https');
net = require('tls');

createOptions = function(server) {
return {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
};

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

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


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

createOptions = function(server) {
return {
port: server.address().port,
allowHalfOpen: true,
};
};

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

}

const assert = require('assert');
const tests = [];
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

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

function run() {
const fn = tests.shift();
if (fn) fn(run);
function test(fn) {
if (!tests.length) {
process.nextTick(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');
});
}));
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);
});

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 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
};
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');
});
}));
});

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

const common = require('../common');
const timeoutTest = require('./test-http-server-keep-alive-timeout');
timeoutTest.testServerKeepAliveTimeout(common, true);
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');
});
}));
});