Skip to content

Commit

Permalink
Tests for memory leaks
Browse files Browse the repository at this point in the history
Conflicts:

	Makefile
  • Loading branch information
isaacs committed May 4, 2012
1 parent e4dd8dc commit e63c782
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ ipch/
email.md
blog.html
deps/v8-*
node_modules
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ uninstall:
clean:
-rm -rf out/Makefile node node_g out/$(BUILDTYPE)/node blog.html email.md
-find out/ -name '*.o' -o -name '*.a' | xargs rm -rf
-rm -rf node_modules

distclean:
-rm -rf out
-rm -f config.gypi
-rm -f config.mk
-rm -rf node node_g blog.html email.md
-rm -rf node_modules

test: all
test: all node_modules/weak
$(PYTHON) tools/test.py --mode=release simple message
PYTHONPATH=tools/closure_linter/ $(PYTHON) tools/closure_linter/closure_linter/gjslint.py --unix_mode --strict --nojsdoc -r lib/ -r src/ --exclude_files lib/punycode.js

Expand All @@ -59,9 +61,17 @@ test-http1: all
test-valgrind: all
$(PYTHON) tools/test.py --mode=release --valgrind simple message

test-all: all
python tools/test.py --mode=debug,release
$(MAKE) test-npm
node_modules/weak:
@if [ ! -f node ]; then make all; fi
@if [ ! -d node_modules ]; then mkdir -p node_modules; fi
./node deps/npm/bin/npm-cli.js install weak --prefix="$(shell pwd)"

test-gc: all node_modules/weak
$(PYTHON) tools/test.py --mode=release gc

test-all: all node_modules/weak
$(PYTHON) tools/test.py --mode=debug,release
make test-npm

test-all-http1: all
$(PYTHON) tools/test.py --mode=debug,release --use-http1
Expand Down
61 changes: 61 additions & 0 deletions test/gc/test-http-client-connaborted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// just like test/gc/http-client.js,
// but aborting every connection that comes in.

function serverHandler(req, res) {
res.connection.destroy();
}

var http = require('http'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 18,
common = require('../common.js'),
assert = require('assert'),
PORT = common.PORT;

console.log('We should do '+ todo +' requests');

var http = require('http');
var server = http.createServer(serverHandler);
server.listen(PORT, getall);

function getall() {
for (var i = 0; i < todo; i++) {
(function(){
function cb(res) {
done+=1;
statusLater();
}

var req = http.get({
hostname: 'localhost',
pathname: '/',
port: PORT
}, cb).on('error', cb);

count++;
weak(req, afterGC);
})()
}
}

function afterGC(){
countGC ++;
}

function statusLater() {
setTimeout(status, 1);
}

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);
}
}
66 changes: 66 additions & 0 deletions test/gc/test-http-client-onerror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// just like test/gc/http-client.js,
// but with an on('error') handler that does nothing.

function serverHandler(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}

var http = require('http'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 18,
common = require('../common.js'),
assert = require('assert'),
PORT = common.PORT;

console.log('We should do '+ todo +' requests');

var http = require('http');
var server = http.createServer(serverHandler);
server.listen(PORT, getall);

function getall() {
for (var i = 0; i < todo; i++) {
(function(){
function cb(res) {
done+=1;
statusLater();
}
function onerror(er) {
throw er;
}

var req = http.get({
hostname: 'localhost',
pathname: '/',
port: PORT
}, cb).on('error', onerror);

count++;
weak(req, afterGC);
})()
}
}

function afterGC(){
countGC ++;
}

function statusLater() {
setTimeout(status, 1);
}

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);
}
}

69 changes: 69 additions & 0 deletions test/gc/test-http-client-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// just like test/gc/http-client.js,
// but with a timeout set

function serverHandler(req, res) {
setTimeout(function () {
res.writeHead(200)
res.end('hello\n');
}, 100);
}

var http = require('http'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 18,
common = require('../common.js'),
assert = require('assert'),
PORT = common.PORT;

console.log('We should do '+ todo +' requests');

var http = require('http');
var server = http.createServer(serverHandler);
server.listen(PORT, getall);

function getall() {
for (var i = 0; i < todo; i++) {
(function(){
function cb() {
done+=1;
statusLater();
}

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);
})()
}
}

function afterGC(){
countGC ++;
}

function statusLater() {
setTimeout(status, 1);
}

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);
}
}

63 changes: 63 additions & 0 deletions test/gc/test-http-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// just a simple http server and client.

function serverHandler(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}

var http = require('http'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 5,
common = require('../common.js'),
assert = require('assert'),
PORT = common.PORT;

console.log('We should do '+ todo +' requests');

var http = require('http');
var server = http.createServer(serverHandler);
server.listen(PORT, getall);


function getall() {
for (var i = 0; i < todo; i++) {
(function(){
function cb(res) {
console.error('in cb')
done+=1;
res.on('end', statusLater);
}

var req = http.get({
hostname: 'localhost',
pathname: '/',
port: PORT
}, cb)

count++;
weak(req, afterGC);
})()
}
}

function afterGC(){
countGC ++;
}

function statusLater() {
setTimeout(status, 1);
}

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);
}
}
61 changes: 61 additions & 0 deletions test/gc/test-net-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// just like test/gc/http-client-timeout.js,
// but using a net server/client instead

function serverHandler(sock) {
sock.setTimeout(120000);
setTimeout(function () {
sock.end('hello\n');
}, 100);
}

var net = require('net'),
weak = require('weak'),
done = 0,
count = 0,
countGC = 0,
todo = 18,
common = require('../common.js'),
assert = require('assert'),
PORT = common.PORT;

console.log('We should do '+ todo +' requests');

var server = net.createServer(serverHandler);
server.listen(PORT, getall);

function getall() {
for (var i = 0; i < todo; i++) {
(function(){
var req = net.connect(PORT, '127.0.0.1');
req.setTimeout(10, function() {
console.log('timeout (expected)')
req.destroy();
done++;
statusLater();
});

count++;
weak(req, afterGC);
})()
}
}

function afterGC(){
countGC ++;
}

function statusLater() {
setTimeout(status, 1);
}

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);
}
}

Loading

0 comments on commit e63c782

Please sign in to comment.