Browse Source

Modify the tests to use onExit hook.

No need to rely on stdout output now.
onExit callbacks should print stack trace from onExit failure
v0.7.4-release
Ryan 16 years ago
parent
commit
8b49cef10b
  1. 3
      src/node.cc
  2. 2
      src/node.js
  3. 1
      test/fixtures/b/c.js
  4. 1
      test/fixtures/b/d.js
  5. 8
      test/test-file-cat-noexist.js
  6. 23
      test/test-file-open.js
  7. 45
      test/test-http-server.js
  8. 2
      test/test-module-loading.js
  9. 18
      test/test-reconnecting-socket.js
  10. 31
      test/test-setTimeout.js
  11. 21
      test/test-tcp-pingpong.js
  12. 43
      test/test-timers.js

3
src/node.cc

@ -351,7 +351,10 @@ main (int argc, char *argv[])
Local<Value> exit_v = node->Get(String::New("exit"));
assert(exit_v->IsFunction());
Handle<Function> exit_f = Handle<Function>::Cast(exit_v);
TryCatch try_catch;
exit_f->Call(g, 0, NULL);
if (try_catch.HasCaught())
node::FatalException(try_catch);
context.Dispose();
// The following line when uncommented causes an error.

2
src/node.js

@ -100,8 +100,6 @@ node.Module.prototype.load = function (callback) {
+ "};\n"
;
var compiled_wrapper = node.compile(wrapper, self.filename);
// execute the script of interest
compiled_wrapper.apply(self.target, [self.filename]);
self.onLoad = self.target.__onLoad;
self.onExit = self.target.__onExit;

1
test/fixtures/b/c.js

@ -11,6 +11,5 @@ exports.D = function () {
};
function onExit () {
puts("c.js onExit called");
string = "C done";
}

1
test/fixtures/b/d.js

@ -5,7 +5,6 @@ exports.D = function () {
};
function onExit () {
node.debug("d.js onExit called");
string = "D done";
}

8
test/test-cat-noexist.js → test/test-file-cat-noexist.js

@ -1,11 +1,13 @@
include("mjsunit.js");
var status = null;
function onLoad () {
var dirname = node.path.dirname(__filename);
var fixtures = node.path.join(dirname, "fixtures");
var filename = node.path.join(fixtures, "does_not_exist.txt");
node.fs.cat(filename, "raw", function (s) { status = s });
}
node.fs.cat(filename, "raw", function (status, data) {
function onExit () {
assertTrue(status != 0);
});
};
}

23
test/test-file-open.js

@ -1,5 +1,8 @@
include("mjsunit.js");
var assert_count = 0;
var got_error = false;
var opened = false;
var closed = false;
function onLoad () {
var dirname = node.path.dirname(__filename);
@ -7,12 +10,18 @@ function onLoad () {
var x = node.path.join(fixtures, "x.txt");
file = new node.fs.File;
file.onError = function (method, errno, msg) {
assertTrue(false);
};
file.onError = function () { got_error = true };
file.open(x, "r", function () {
assert_count += 1;
file.close();
opened = true
file.close(function () {
closed = true;
});
});
};
}
function onExit () {
assertFalse(got_error);
assertTrue(opened);
assertTrue(closed);
}

45
test/test-http-server.js

@ -2,23 +2,23 @@ include("mjsunit.js");
var port = 8222;
function onLoad() {
var request_number = 0;
var requests_sent = 0;
var server_response = "";
var client_got_eof = false;
var request_number = 0;
function onLoad() {
new node.http.Server(function (req, res) {
res.id = request_number;
req.id = request_number++;
if (req.id == 0) {
//puts("get req");
assertEquals("GET", req.method);
assertEquals("/hello", req.uri.path);
}
if (req.id == 1) {
//puts("post req");
assertEquals("POST", req.method);
assertEquals("/quit", req.uri.path);
this.close();
@ -26,7 +26,6 @@ function onLoad() {
}
setTimeout(function () {
//puts("send response");
res.sendHeader(200, [["Content-Type", "text/plain"]]);
res.sendBody(req.uri.path);
res.finish();
@ -36,44 +35,42 @@ function onLoad() {
var c = new node.tcp.Connection();
c.setEncoding("utf8");
var req_sent = 0;
c.onConnect = function () {
//puts("send get");
c.send( "GET /hello HTTP/1.1\r\n\r\n" );
req_sent += 1;
requests_sent += 1;
};
var total = "";
c.onReceive = function (chunk) {
//puts("client recv");
total += chunk;
puts("total: " + JSON.stringify(total));
server_response += chunk;
if ( req_sent == 1) {
puts("send post");
if ( requests_sent == 1) {
c.send("POST /quit HTTP/1.1\r\n\r\n");
c.close();
puts("client half close");
assertEquals(c.readyState, "readOnly");
req_sent += 1;
requests_sent += 1;
}
};
c.onEOF = function () {
puts("client got eof");
client_got_eof = true;
};
c.onDisconnect = function () {
puts("client disconnected");
assertEquals(c.readyState, "closed");
};
c.connect(port);
}
function onExit () {
assertEquals(2, request_number);
assertEquals(2, requests_sent);
var hello = new RegExp("/hello");
assertTrue(hello.exec(total) != null);
assertTrue(hello.exec(server_response) != null);
var quit = new RegExp("/quit");
assertTrue(quit.exec(total) != null);
};
assertTrue(quit.exec(server_response) != null);
c.connect(port);
assertTrue(client_got_eof);
}

2
test/test-module-loading.js

@ -37,6 +37,4 @@ function onExit () {
assertInstanceof(d2.D, Function);
assertEquals("D done", d2.D());
node.debug("test-module-loading.js onExit() called");
}

18
test/test-reconnecting-socket.js

@ -1,10 +1,13 @@
include("mjsunit.js");
var N = 50;
var port = 8921;
var disconnect_count = 0;
var client_recv_count = 0;
function onLoad () {
var server = new node.tcp.Server(function (socket) {
puts("new connection");
socket.onConnect = function () {
socket.send("hello\r\n");
};
@ -19,25 +22,21 @@ function onLoad () {
};
});
server.listen(port);
var count = 0;
var client = new node.tcp.Connection();
client.setEncoding("UTF8");
client.onConnect = function () {
puts("client connected");
};
client.onReceive = function (chunk) {
puts("got msg");
client_recv_count += 1;
assertEquals("hello\r\n", chunk);
client.fullClose();
};
client.onDisconnect = function (had_error) {
assertFalse(had_error);
puts("client disconnected");
if (count++ < 5)
if (disconnect_count++ < N)
client.connect(port); // reconnect
else
server.close();
@ -45,3 +44,8 @@ function onLoad () {
client.connect(port);
}
function onExit () {
assertEquals(N+1, disconnect_count);
assertEquals(N+1, client_recv_count);
}

31
test/test-setTimeout.js

@ -1,31 +0,0 @@
include("mjsunit.js");
function onLoad () {
assertInstanceof(setTimeout, Function);
var starttime = new Date;
setTimeout(function () {
var endtime = new Date;
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
assertTrue(900 < diff || diff < 1100);
}, 1000);
// this timer shouldn't execute
var id = setTimeout(function () { assertTrue(false); }, 500);
clearTimeout(id);
var count = 0;
setInterval(function () {
count += 1;
var endtime = new Date;
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
puts(diff);
var t = count * 1000;
assertTrue(t - 100 < diff || diff < t + 100);
assertTrue(count <= 3);
if (count == 3)
clearInterval(this);
}, 1000);
}

21
test/test-pingpong.js → test/test-tcp-pingpong.js

@ -4,17 +4,15 @@ var port = 12123;
var N = 1000;
var count = 0;
var sent_final_ping = false;
function Ponger (socket) {
socket.setEncoding("utf8");
socket.timeout = 0;
puts("got socket.");
socket.onReceive = function (data) {
assertEquals("open", socket.readyState);
//puts("server recved data: " + JSON.stringify(data));
assertTrue(count <= N);
stdout.print("-");
if (/PING/.exec(data)) {
socket.send("PONG");
}
@ -22,7 +20,6 @@ function Ponger (socket) {
socket.onEOF = function () {
assertEquals("writeOnly", socket.readyState);
puts("ponger: onEOF");
socket.close();
};
@ -30,7 +27,6 @@ function Ponger (socket) {
assertEquals("127.0.0.1", socket.remoteAddress);
assertFalse(had_error);
assertEquals("closed", socket.readyState);
puts("ponger: onDisconnect");
socket.server.close();
};
}
@ -46,15 +42,10 @@ function onLoad() {
client.onConnect = function () {
assertEquals("open", client.readyState);
puts("client is connected.");
client.send("PING");
};
var sent_final_ping = false;
client.onReceive = function (data) {
//puts("client recved data: " + JSON.stringify(data));
stdout.print(".");
assertEquals("PONG", data);
count += 1;
@ -68,7 +59,6 @@ function onLoad() {
if (count < N) {
client.send("PING");
} else {
puts("sending final ping");
sent_final_ping = true;
client.send("PING");
client.close();
@ -76,10 +66,13 @@ function onLoad() {
};
client.onEOF = function () {
puts("pinger: onEOF");
assertEquals(N+1, count);
};
client.connect(port);
assertEquals("closed", client.readyState);
}
function onExit () {
assertEquals(N+1, count);
assertTrue(sent_final_ping);
}

43
test/test-timers.js

@ -0,0 +1,43 @@
include("mjsunit.js");
var WINDOW = 800; // why is does this need to be so big?
var interval_count = 0;
var setTimeout_called = false;
function onLoad () {
assertInstanceof(setTimeout, Function);
var starttime = new Date;
setTimeout(function () {
var endtime = new Date;
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
assertTrue(1000 - WINDOW < diff && diff < 1000 + WINDOW);
setTimeout_called = true;
}, 1000);
// this timer shouldn't execute
var id = setTimeout(function () { assertTrue(false); }, 500);
clearTimeout(id);
setInterval(function () {
interval_count += 1;
var endtime = new Date;
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
var t = interval_count * 1000;
assertTrue(t - WINDOW < diff && diff < t + WINDOW);
assertTrue(interval_count <= 3);
if (interval_count == 3)
clearInterval(this);
}, 1000);
}
function onExit () {
assertTrue(setTimeout_called);
assertEquals(3, interval_count);
}
Loading…
Cancel
Save