nodejs/test/gc/test-http-client-timeout.js

80 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
2012-05-03 10:16:25 -07:00
// just like test/gc/http-client.js,
// but with a timeout set
require('../common');
2012-05-03 10:16:25 -07:00
function serverHandler(req, res) {
setTimeout(function() {
req.resume();
res.writeHead(200);
2012-05-03 10:16:25 -07:00
res.end('hello\n');
}, 100);
}
const http = require('http');
const weak = require('weak');
const assert = require('assert');
const todo = 550;
let done = 0;
let count = 0;
let countGC = 0;
2012-05-03 10:16:25 -07:00
console.log('We should do ' + todo + ' requests');
2012-05-03 10:16:25 -07:00
const server = http.createServer(serverHandler);
server.listen(0, getall);
2012-05-03 10:16:25 -07:00
function getall() {
if (count >= todo)
return;
2012-05-03 10:16:25 -07:00
(function() {
function cb(res) {
res.resume();
done += 1;
statusLater();
}
2012-05-03 10:16:25 -07:00
const req = http.get({
hostname: 'localhost',
pathname: '/',
port: server.address().port
}, cb);
req.on('error', cb);
req.setTimeout(10, function() {
console.log('timeout (expected)');
});
count++;
weak(req, afterGC);
})();
setImmediate(getall);
2012-05-03 10:16:25 -07:00
}
for (let i = 0; i < 10; i++)
getall();
function afterGC() {
countGC++;
2012-05-03 10:16:25 -07:00
}
let timer;
2012-05-03 10:16:25 -07:00
function statusLater() {
global.gc();
if (timer) clearTimeout(timer);
timer = setTimeout(status, 1);
2012-05-03 10:16:25 -07:00
}
function status() {
global.gc();
2012-05-03 10:16:25 -07:00
console.log('Done: %d/%d', done, todo);
console.log('Collected: %d/%d', countGC, count);
if (done === todo) {
console.log('All should be collected now.');
assert.strictEqual(count, countGC);
2012-05-03 10:16:25 -07:00
process.exit(0);
}
}