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

70 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
// Flags: --expose-gc
// just like test-gc-http-client.js,
2012-05-03 10:16:25 -07:00
// but with a timeout set
require('../common');
const onGC = require('../common/ongc');
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 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;
}
2012-05-03 10:16:25 -07:00
const req = http.get({
hostname: 'localhost',
pathname: '/',
port: server.address().port
}, cb);
req.setTimeout(10, function() {
console.log('timeout (expected)');
});
count++;
onGC(req, { ongc });
})();
setImmediate(getall);
2012-05-03 10:16:25 -07:00
}
for (let i = 0; i < 10; i++)
getall();
function ongc() {
countGC++;
2012-05-03 10:16:25 -07:00
}
setInterval(status, 100).unref();
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 (countGC === todo) server.close();
2012-05-03 10:16:25 -07:00
}