Skip to content

Commit

Permalink
test, refactor: use the test_util.cleanup method to close the server …
Browse files Browse the repository at this point in the history
…socket.
  • Loading branch information
xicilion committed Oct 10, 2019
1 parent ce3d328 commit ac9f50e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 76 deletions.
9 changes: 2 additions & 7 deletions test/gui_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ if (win) {
var base_port = coroutine.vmid * 10000;

describe("gui", () => {
var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
});
after(test_util.cleanup);

it("webview", () => {
var check = false;
Expand All @@ -51,7 +46,7 @@ if (win) {
r.response.write(html);
});
svr.start();
ss.push(svr.socket);
test_util.push(svr.socket);

var win = gui.open("http://127.0.0.1:" + (8999 + base_port) + "/");

Expand Down
18 changes: 7 additions & 11 deletions test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,14 @@ function set_header_for_head_req(r, cookie_for) {
}

describe("http", () => {
var ss = [];

after(() => {
http.timeout = 0;
http.autoRedirect = true;
http.enableCookie = true;

ss.forEach((s) => {
s.close();
});
});

after(test_util.cleanup);

describe("headers", () => {
var d = new http.Request().headers;

Expand Down Expand Up @@ -989,7 +985,7 @@ describe("http", () => {

svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

beforeEach(() => {
Expand Down Expand Up @@ -1440,7 +1436,7 @@ describe("http", () => {
});
svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

describe("request", () => {
Expand Down Expand Up @@ -1758,7 +1754,7 @@ describe("http", () => {
});
svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

after(() => {
Expand Down Expand Up @@ -1933,7 +1929,7 @@ describe("http", () => {
});
svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

describe("request & cookie", () => {
Expand Down Expand Up @@ -2123,7 +2119,7 @@ describe("http", () => {
});
svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

describe("constructor", () => {
Expand Down
12 changes: 3 additions & 9 deletions test/mq_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,7 @@ describe("mq", () => {
var m = new mq.Message();
});

var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
});
after(test_util.cleanup);

describe("function handler", () => {
var n = 0;
Expand Down Expand Up @@ -727,7 +721,7 @@ describe("mq", () => {

it("memory leak", () => {
var svr = new net.TcpServer(8888, () => {});
ss.push(svr.socket);
test_util.push(svr.socket);

test_util.gc();
var no1 = test_util.countObject('Chain');
Expand Down Expand Up @@ -1059,7 +1053,7 @@ describe("mq", () => {
test_util.gc();

var svr = new net.TcpServer(8890, () => {});
ss.push(svr.socket);
test_util.push(svr.socket);

test_util.gc();
var no1 = test_util.countObject('Routing');
Expand Down
26 changes: 10 additions & 16 deletions test/net_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ describe("net", () => {
assert.equal(net.backend(), backend);
});

var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
});
after(test_util.cleanup);

it("echo", () => {
function connect(c) {
Expand All @@ -85,7 +79,7 @@ describe("net", () => {

var s = new net.Socket(net_config.family,
net.SOCK_STREAM);
ss.push(s);
test_util.push(s);

s.bind(8080 + base_port);
s.listen();
Expand Down Expand Up @@ -144,7 +138,7 @@ describe("net", () => {
}

var s1 = new net.Socket(net_config.family, net.SOCK_STREAM);
ss.push(s1);
test_util.push(s1);

s1.bind(8081 + base_port);
s1.listen();
Expand Down Expand Up @@ -197,7 +191,7 @@ describe("net", () => {
}

var s2 = new net.Socket(net_config.family, net.SOCK_STREAM);
ss.push(s2);
test_util.push(s2);

s2.bind(8082 + base_port);
s2.listen();
Expand Down Expand Up @@ -272,12 +266,12 @@ describe("net", () => {
var t = 0;

function accept2(s, n) {
ss.push(s.accept());
test_util.push(s.accept());
t = n;
}

var s2 = new net.Socket(net_config.family, net.SOCK_STREAM);
ss.push(s2);
test_util.push(s2);

s2.bind(8083 + base_port);
s2.listen();
Expand Down Expand Up @@ -320,7 +314,7 @@ describe("net", () => {
}

var s2 = new net.Socket(net_config.family, net.SOCK_STREAM);
ss.push(s2);
test_util.push(s2);

s2.bind(8084 + base_port);
s2.listen();
Expand Down Expand Up @@ -349,13 +343,13 @@ describe("net", () => {
function accept4(s) {
try {
while (true) {
ss.push(s.accept());
test_util.push(s.accept());
}
} catch (e) {}
}

var s2 = new net.Socket(net_config.family, net.SOCK_STREAM);
ss.push(s2);
test_util.push(s2);

s2.bind(8085 + base_port);
s2.listen();
Expand Down Expand Up @@ -407,7 +401,7 @@ describe("net", () => {
assert.throws(() => {
new net.TcpServer(8811 + base_port, (c) => {});
});
ss.push(svr.socket);
test_util.push(svr.socket);
});

describe("abort Pending I/O", () => {
Expand Down
12 changes: 4 additions & 8 deletions test/process_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var test = require("test");
test.setup();

var test_util = require('./test_util');

var process = require('process');
var coroutine = require("coroutine");
var path = require('path');
Expand All @@ -17,13 +19,7 @@ describe('process', () => {
cmd = process.execPath;
});

var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
});
after(test_util.cleanup);

it("hrtime", () => {
var start = process.hrtime();
Expand Down Expand Up @@ -207,7 +203,7 @@ describe('process', () => {
};
})
});
ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();

var p = process.open(cmd, [path.join(__dirname, 'process', 'exec19.js')]);
Expand Down
19 changes: 7 additions & 12 deletions test/ssl_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var test = require("test");
test.setup();

var test_util = require('./test_util');

var ssl = require("ssl");
var crypto = require("crypto");
var net = require("net");
Expand Down Expand Up @@ -46,14 +48,7 @@ function del(f) {
describe('ssl', () => {
var sss;

var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
ssl.ca.clear();
});
after(test_util.cleanup);

it("root ca", () => {
var cert = new crypto.X509Cert();
Expand Down Expand Up @@ -104,7 +99,7 @@ describe('ssl', () => {
});
svr.start();

ss.push(svr.socket);
test_util.push(svr.socket);
});

function test_handshake() {
Expand Down Expand Up @@ -229,7 +224,7 @@ describe('ssl', () => {
ss.close();
s.close();
});
ss.push(svr.socket);
test_util.push(svr.socket);
svr.start();

function t_conn() {
Expand Down Expand Up @@ -265,7 +260,7 @@ describe('ssl', () => {
while (buf = s.read())
s.write(buf);
}));
ss.push(svr.socket);
test_util.push(svr.socket);
svr.start();

for (var i = 0; i < 10; i++) {
Expand All @@ -290,7 +285,7 @@ describe('ssl', () => {
while (buf = s.read())
s.write(buf);
});
ss.push(svr.socket);
test_util.push(svr.socket);
svr.start();

for (var i = 0; i < 10; i++) {
Expand Down
10 changes: 10 additions & 0 deletions test/test_util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var os = require('os');
var coroutine = require('coroutine');

var ss = [];

exports.countObject = nm => {
var cnt = 0;

Expand All @@ -25,4 +27,12 @@ exports.gc = () => {
coroutine.sleep(1);
GC();
}
}

exports.push = s => ss.push(s);

exports.cleanup = () => {
ss.forEach(s => {
s.close();
});
}
20 changes: 7 additions & 13 deletions test/ws_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ var test_util = require('./test_util');
var base_port = coroutine.vmid * 10000;

describe('ws', () => {
var ss = [];

after(() => {
ss.forEach((s) => {
s.close();
});
});
after(test_util.cleanup);

describe('Message', () => {
function load_msg(data) {
Expand Down Expand Up @@ -272,7 +266,7 @@ describe('ws', () => {
};
})
});
ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();
});

Expand Down Expand Up @@ -516,7 +510,7 @@ describe('ws', () => {
});
}
}]);
ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();
});

Expand Down Expand Up @@ -702,7 +696,7 @@ describe('ws', () => {
});
})
});
ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();

var t = false;
Expand Down Expand Up @@ -910,7 +904,7 @@ describe('ws', () => {
})
});

ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();

assert.equal(test_util.countObject('WebSocket'), no1);
Expand Down Expand Up @@ -944,12 +938,12 @@ describe('ws', () => {
var no1 = test_util.countObject('WebSocket');
var httpd = new http.Server(8817 + base_port, {
"/ws": ws.upgrade((s, req) => {
ss.push(s);
test_util.push(s);
s.send(new Date());
})
});

ss.push(httpd.socket);
test_util.push(httpd.socket);
httpd.start();

test_util.gc();
Expand Down

0 comments on commit ac9f50e

Please sign in to comment.