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'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
const assert = require('assert'); |
|
||||
const http = require('http'); |
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) { |
const server = http.Server(common.mustCall((req, res) => { |
||||
console.log('Got connection'); |
|
||||
res.writeHead(200); |
res.writeHead(200); |
||||
res.write('Working on it...'); |
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
|
server.listen(0, common.mustCall(() => { |
||||
// 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(); |
|
||||
} |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
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() { |
const options = { port: server.address().port }; |
||||
console.log('Server listening.'); |
|
||||
|
|
||||
for (let i = 0; i < N; i++) { |
for (let i = 0; i < N; i++) { |
||||
console.log(`Making client ${i}`); |
options.path = `/?id=${i}`; |
||||
const options = { port: this.address().port, path: `/?id=${i}` }; |
requests.push( |
||||
const req = http.get(options, function(res) { |
http.get(options, common.mustCall((res) => { |
||||
console.log(`Client response code ${res.statusCode}`); |
|
||||
|
|
||||
res.resume(); |
res.resume(); |
||||
if (++responses === N) { |
reqCountdown.dec(); |
||||
console.log('All clients connected, destroying.'); |
}))); |
||||
requests.forEach(function(outReq) { |
|
||||
console.log('abort'); |
|
||||
outReq.abort(); |
|
||||
}); |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
requests.push(req); |
|
||||
} |
} |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.strictEqual(N, clientAborts); |
|
||||
}); |
|
||||
|
@ -1,16 +1,17 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
const http = require('http'); |
const http = require('http'); |
||||
|
|
||||
const server = http.createServer(function(req, res) { |
const server = http.createServer(common.mustCall((req, res) => { |
||||
res.end('Hello'); |
res.end('Hello'); |
||||
}); |
})); |
||||
|
|
||||
server.listen(0, function() { |
server.listen(0, common.mustCall(() => { |
||||
const req = http.get({port: this.address().port}, function(res) { |
const options = { port: server.address().port }; |
||||
res.on('data', function(data) { |
const req = http.get(options, common.mustCall((res) => { |
||||
|
res.on('data', (data) => { |
||||
req.abort(); |
req.abort(); |
||||
server.close(); |
server.close(); |
||||
}); |
}); |
||||
}); |
})); |
||||
}); |
})); |
||||
|
@ -1,18 +1,18 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
|
const assert = require('assert'); |
||||
const http = require('http'); |
const http = require('http'); |
||||
|
|
||||
http.createServer(function(req, res) { |
const server = http.createServer((req, res) => { |
||||
res.end('ok\n'); |
res.end('ok'); |
||||
this.close(); |
server.close(); |
||||
}).listen(0, test); |
}).listen(0, common.mustCall(() => { |
||||
|
|
||||
function test() { |
|
||||
http.request({ |
http.request({ |
||||
port: this.address().port, |
port: server.address().port, |
||||
encoding: 'utf8' |
encoding: 'utf8' |
||||
}, function(res) { |
}, common.mustCall((res) => { |
||||
res.pipe(process.stdout); |
let data = ''; |
||||
}).end(); |
res.on('data', (chunk) => data += chunk); |
||||
} |
res.on('end', common.mustCall(() => assert.strictEqual(data, 'ok'))); |
||||
|
})).end(); |
||||
|
})); |
||||
|
@ -1,39 +1,30 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
const assert = require('assert'); |
const assert = require('assert'); |
||||
|
|
||||
const http = require('http'); |
const http = require('http'); |
||||
const net = require('net'); |
const net = require('net'); |
||||
|
const Countdown = require('../common/countdown'); |
||||
|
|
||||
|
const countdown = new Countdown(2, common.mustCall(() => server.close())); |
||||
|
|
||||
let connects = 0; |
const payloads = [ |
||||
let parseErrors = 0; |
'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
|
// Create a TCP server
|
||||
net.createServer(function(c) { |
const server = |
||||
console.log('connection'); |
net.createServer(common.mustCall((c) => c.end(payloads.shift()), 2)); |
||||
if (++connects === 1) { |
|
||||
c.end('HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world'); |
server.listen(0, common.mustCall(() => { |
||||
} else { |
|
||||
c.end('bad http - should trigger parse error\r\n'); |
|
||||
this.close(); |
|
||||
} |
|
||||
}).listen(0, '127.0.0.1', function() { |
|
||||
for (let i = 0; i < 2; i++) { |
for (let i = 0; i < 2; i++) { |
||||
http.request({ |
http.get({ |
||||
host: '127.0.0.1', |
port: server.address().port, |
||||
port: this.address().port, |
|
||||
method: 'GET', |
|
||||
path: '/' |
path: '/' |
||||
}).on('error', function(e) { |
}).on('error', common.mustCall((e) => { |
||||
console.log('got error from client'); |
|
||||
assert.ok(e.message.includes('Parse Error')); |
assert.ok(e.message.includes('Parse Error')); |
||||
assert.strictEqual(e.code, 'HPE_INVALID_CONSTANT'); |
assert.strictEqual(e.code, 'HPE_INVALID_CONSTANT'); |
||||
parseErrors++; |
countdown.dec(); |
||||
}).end(); |
})); |
||||
} |
} |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.strictEqual(connects, 2); |
|
||||
assert.strictEqual(parseErrors, 2); |
|
||||
}); |
|
||||
|
Loading…
Reference in new issue