2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-05-03 10:16:25 -07:00
|
|
|
// just a simple http server and client.
|
|
|
|
|
2016-07-19 02:03:42 +05:30
|
|
|
require('../common');
|
|
|
|
|
2012-05-03 10:16:25 -07:00
|
|
|
function serverHandler(req, res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('Hello World\n');
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const http = require('http');
|
|
|
|
const weak = require('weak');
|
|
|
|
const assert = require('assert');
|
|
|
|
const todo = 500;
|
|
|
|
let done = 0;
|
|
|
|
let count = 0;
|
|
|
|
let countGC = 0;
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
console.log('We should do ' + todo + ' requests');
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = http.createServer(serverHandler);
|
2016-05-26 20:49:51 -07:00
|
|
|
server.listen(0, getall);
|
2012-05-03 10:16:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
function getall() {
|
2014-02-24 10:20:30 -08:00
|
|
|
if (count >= todo)
|
|
|
|
return;
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
(function() {
|
2014-02-24 10:20:30 -08:00
|
|
|
function cb(res) {
|
|
|
|
res.resume();
|
2015-05-19 13:00:06 +02:00
|
|
|
console.error('in cb');
|
|
|
|
done += 1;
|
2016-04-20 23:05:44 -07:00
|
|
|
res.on('end', global.gc);
|
2014-02-24 10:20:30 -08:00
|
|
|
}
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const req = http.get({
|
2014-02-24 10:20:30 -08:00
|
|
|
hostname: 'localhost',
|
|
|
|
pathname: '/',
|
2016-05-26 20:49:51 -07:00
|
|
|
port: server.address().port
|
2015-05-19 13:00:06 +02:00
|
|
|
}, cb);
|
2014-02-24 10:20:30 -08:00
|
|
|
|
|
|
|
count++;
|
|
|
|
weak(req, afterGC);
|
2015-05-19 13:00:06 +02:00
|
|
|
})();
|
2014-02-24 10:20:30 -08:00
|
|
|
|
|
|
|
setImmediate(getall);
|
2012-05-03 10:16:25 -07:00
|
|
|
}
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
for (let i = 0; i < 10; i++)
|
2014-02-24 10:20:30 -08:00
|
|
|
getall();
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
function afterGC() {
|
2016-02-03 12:27:40 -08:00
|
|
|
countGC++;
|
2012-05-03 10:16:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-24 10:20:30 -08:00
|
|
|
setInterval(status, 1000).unref();
|
2012-05-03 10:16:25 -07:00
|
|
|
|
|
|
|
function status() {
|
2016-04-20 23:05:44 -07:00
|
|
|
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.');
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.strictEqual(count, countGC);
|
2012-05-03 10:16:25 -07:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
}
|