mirror of https://github.com/lukechilds/node.git
Browse Source
* Add common/countdown utility * Numerous improvements to http tests PR-URL: https://github.com/nodejs/node/pull/14315 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>v6.x
committed by
Myles Borins
24 changed files with 370 additions and 439 deletions
@ -0,0 +1,27 @@ |
|||
/* eslint-disable required-modules */ |
|||
'use strict'; |
|||
|
|||
const assert = require('assert'); |
|||
const kLimit = Symbol('limit'); |
|||
const kCallback = Symbol('callback'); |
|||
|
|||
class Countdown { |
|||
constructor(limit, cb) { |
|||
assert.strictEqual(typeof limit, 'number'); |
|||
assert.strictEqual(typeof cb, 'function'); |
|||
this[kLimit] = limit; |
|||
this[kCallback] = cb; |
|||
} |
|||
|
|||
dec() { |
|||
assert(this[kLimit] > 0, 'Countdown expired'); |
|||
if (--this[kLimit] === 0) |
|||
this[kCallback](); |
|||
} |
|||
|
|||
get remaining() { |
|||
return this[kLimit]; |
|||
} |
|||
} |
|||
|
|||
module.exports = Countdown; |
@ -0,0 +1,15 @@ |
|||
'use strict'; |
|||
|
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const Countdown = require('../common/countdown'); |
|||
|
|||
let done = ''; |
|||
|
|||
const countdown = new Countdown(2, common.mustCall(() => done = true)); |
|||
assert.strictEqual(countdown.remaining, 2); |
|||
countdown.dec(); |
|||
assert.strictEqual(countdown.remaining, 1); |
|||
countdown.dec(); |
|||
assert.strictEqual(countdown.remaining, 0); |
|||
assert.strictEqual(done, true); |
@ -1,55 +1,33 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const assert = require('assert'); |
|||
const common = require('../common'); |
|||
const http = require('http'); |
|||
const Countdown = require('../common/countdown'); |
|||
|
|||
let clientAborts = 0; |
|||
const N = 8; |
|||
|
|||
const countdown = new Countdown(N, common.mustCall(() => server.close())); |
|||
|
|||
const server = http.Server(function(req, res) { |
|||
console.log('Got connection'); |
|||
const server = http.Server(common.mustCall((req, res) => { |
|||
res.writeHead(200); |
|||
res.write('Working on it...'); |
|||
req.on('aborted', common.mustCall(() => countdown.dec())); |
|||
}, N)); |
|||
|
|||
// I would expect an error event from req or res that the client aborted
|
|||
// before completing the HTTP request / response cycle, or maybe a new
|
|||
// event like "aborted" or something.
|
|||
req.on('aborted', function() { |
|||
clientAborts++; |
|||
console.log(`Got abort ${clientAborts}`); |
|||
if (clientAborts === N) { |
|||
console.log('All aborts detected, you win.'); |
|||
server.close(); |
|||
} |
|||
}); |
|||
}); |
|||
server.listen(0, common.mustCall(() => { |
|||
|
|||
let responses = 0; |
|||
const N = 8; |
|||
const requests = []; |
|||
const requests = []; |
|||
const reqCountdown = new Countdown(N, common.mustCall(() => { |
|||
requests.forEach((req) => req.abort()); |
|||
})); |
|||
|
|||
server.listen(0, function() { |
|||
console.log('Server listening.'); |
|||
const options = { port: server.address().port }; |
|||
|
|||
for (let i = 0; i < N; i++) { |
|||
console.log(`Making client ${i}`); |
|||
const options = { port: this.address().port, path: `/?id=${i}` }; |
|||
const req = http.get(options, function(res) { |
|||
console.log(`Client response code ${res.statusCode}`); |
|||
|
|||
res.resume(); |
|||
if (++responses === N) { |
|||
console.log('All clients connected, destroying.'); |
|||
requests.forEach(function(outReq) { |
|||
console.log('abort'); |
|||
outReq.abort(); |
|||
}); |
|||
} |
|||
}); |
|||
|
|||
requests.push(req); |
|||
options.path = `/?id=${i}`; |
|||
requests.push( |
|||
http.get(options, common.mustCall((res) => { |
|||
res.resume(); |
|||
reqCountdown.dec(); |
|||
}))); |
|||
} |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.strictEqual(N, clientAborts); |
|||
}); |
|||
})); |
|||
|
@ -1,16 +1,17 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const http = require('http'); |
|||
|
|||
const server = http.createServer(function(req, res) { |
|||
const server = http.createServer(common.mustCall((req, res) => { |
|||
res.end('Hello'); |
|||
}); |
|||
})); |
|||
|
|||
server.listen(0, function() { |
|||
const req = http.get({port: this.address().port}, function(res) { |
|||
res.on('data', function(data) { |
|||
server.listen(0, common.mustCall(() => { |
|||
const options = { port: server.address().port }; |
|||
const req = http.get(options, common.mustCall((res) => { |
|||
res.on('data', (data) => { |
|||
req.abort(); |
|||
server.close(); |
|||
}); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
|
@ -1,18 +1,18 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
|
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const http = require('http'); |
|||
|
|||
http.createServer(function(req, res) { |
|||
res.end('ok\n'); |
|||
this.close(); |
|||
}).listen(0, test); |
|||
|
|||
function test() { |
|||
const server = http.createServer((req, res) => { |
|||
res.end('ok'); |
|||
server.close(); |
|||
}).listen(0, common.mustCall(() => { |
|||
http.request({ |
|||
port: this.address().port, |
|||
port: server.address().port, |
|||
encoding: 'utf8' |
|||
}, function(res) { |
|||
res.pipe(process.stdout); |
|||
}).end(); |
|||
} |
|||
}, common.mustCall((res) => { |
|||
let data = ''; |
|||
res.on('data', (chunk) => data += chunk); |
|||
res.on('end', common.mustCall(() => assert.strictEqual(data, 'ok'))); |
|||
})).end(); |
|||
})); |
|||
|
@ -1,39 +1,30 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
|
|||
const http = require('http'); |
|||
const net = require('net'); |
|||
const Countdown = require('../common/countdown'); |
|||
|
|||
const countdown = new Countdown(2, common.mustCall(() => server.close())); |
|||
|
|||
let connects = 0; |
|||
let parseErrors = 0; |
|||
const payloads = [ |
|||
'HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world', |
|||
'bad http = should trigger parse error' |
|||
]; |
|||
|
|||
// Create a TCP server
|
|||
net.createServer(function(c) { |
|||
console.log('connection'); |
|||
if (++connects === 1) { |
|||
c.end('HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world'); |
|||
} else { |
|||
c.end('bad http - should trigger parse error\r\n'); |
|||
this.close(); |
|||
} |
|||
}).listen(0, '127.0.0.1', function() { |
|||
const server = |
|||
net.createServer(common.mustCall((c) => c.end(payloads.shift()), 2)); |
|||
|
|||
server.listen(0, common.mustCall(() => { |
|||
for (let i = 0; i < 2; i++) { |
|||
http.request({ |
|||
host: '127.0.0.1', |
|||
port: this.address().port, |
|||
method: 'GET', |
|||
http.get({ |
|||
port: server.address().port, |
|||
path: '/' |
|||
}).on('error', function(e) { |
|||
console.log('got error from client'); |
|||
}).on('error', common.mustCall((e) => { |
|||
assert.ok(e.message.includes('Parse Error')); |
|||
assert.strictEqual(e.code, 'HPE_INVALID_CONSTANT'); |
|||
parseErrors++; |
|||
}).end(); |
|||
countdown.dec(); |
|||
})); |
|||
} |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.strictEqual(connects, 2); |
|||
assert.strictEqual(parseErrors, 2); |
|||
}); |
|||
})); |
|||
|
Loading…
Reference in new issue