0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/gc/test-http-client-timeout.js

81 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
2012-05-03 19:16:25 +02:00
// just like test/gc/http-client.js,
// but with a timeout set
function serverHandler(req, res) {
setTimeout(function() {
req.resume();
res.writeHead(200);
2012-05-03 19:16:25 +02:00
res.end('hello\n');
}, 100);
}
var http = require('http'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 550,
common = require('../common'),
2012-05-03 19:16:25 +02:00
assert = require('assert'),
PORT = common.PORT;
console.log('We should do ' + todo + ' requests');
2012-05-03 19:16:25 +02:00
var server = http.createServer(serverHandler);
server.listen(PORT, getall);
function getall() {
if (count >= todo)
return;
2012-05-03 19:16:25 +02:00
(function() {
function cb(res) {
res.resume();
done += 1;
statusLater();
}
2012-05-03 19:16:25 +02:00
var req = http.get({
hostname: 'localhost',
pathname: '/',
port: PORT
}, cb);
req.on('error', cb);
req.setTimeout(10, function() {
console.log('timeout (expected)');
});
count++;
weak(req, afterGC);
})();
setImmediate(getall);
2012-05-03 19:16:25 +02:00
}
for (var i = 0; i < 10; i++)
getall();
function afterGC() {
2012-05-03 19:16:25 +02:00
countGC ++;
}
var timer;
2012-05-03 19:16:25 +02:00
function statusLater() {
gc();
if (timer) clearTimeout(timer);
timer = setTimeout(status, 1);
2012-05-03 19:16:25 +02:00
}
function status() {
gc();
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(count === countGC);
process.exit(0);
}
}