diff --git a/src/node.cc b/src/node.cc index 37e6d84da7..187509b862 100644 --- a/src/node.cc +++ b/src/node.cc @@ -351,7 +351,10 @@ main (int argc, char *argv[]) Local exit_v = node->Get(String::New("exit")); assert(exit_v->IsFunction()); Handle exit_f = Handle::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. diff --git a/src/node.js b/src/node.js index d558f977bf..bca29bdcd0 100644 --- a/src/node.js +++ b/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; diff --git a/test/fixtures/b/c.js b/test/fixtures/b/c.js index e2091fdaa8..be070e2d0c 100644 --- a/test/fixtures/b/c.js +++ b/test/fixtures/b/c.js @@ -11,6 +11,5 @@ exports.D = function () { }; function onExit () { - puts("c.js onExit called"); string = "C done"; } diff --git a/test/fixtures/b/d.js b/test/fixtures/b/d.js index 7e178e99b3..3258850dbe 100644 --- a/test/fixtures/b/d.js +++ b/test/fixtures/b/d.js @@ -5,7 +5,6 @@ exports.D = function () { }; function onExit () { - node.debug("d.js onExit called"); string = "D done"; } diff --git a/test/test-cat-noexist.js b/test/test-file-cat-noexist.js similarity index 61% rename from test/test-cat-noexist.js rename to test/test-file-cat-noexist.js index a460feff81..cf1f129e5d 100644 --- a/test/test-cat-noexist.js +++ b/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) { - assertTrue(status != 0); - }); -}; +function onExit () { + assertTrue(status != 0); +} diff --git a/test/test-file-open.js b/test/test-file-open.js index ad33a97f37..52809a3398 100644 --- a/test/test-file-open.js +++ b/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); +} diff --git a/test/test-http-server.js b/test/test-http-server.js index 9ea99228d5..0cf09c697e 100644 --- a/test/test-http-server.js +++ b/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"); - - var hello = new RegExp("/hello"); - assertTrue(hello.exec(total) != null); - - var quit = new RegExp("/quit"); - assertTrue(quit.exec(total) != null); }; c.connect(port); } + +function onExit () { + assertEquals(2, request_number); + assertEquals(2, requests_sent); + + var hello = new RegExp("/hello"); + assertTrue(hello.exec(server_response) != null); + + var quit = new RegExp("/quit"); + assertTrue(quit.exec(server_response) != null); + + assertTrue(client_got_eof); +} diff --git a/test/test-module-loading.js b/test/test-module-loading.js index 519c516510..ef3647f1c5 100644 --- a/test/test-module-loading.js +++ b/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"); } diff --git a/test/test-reconnecting-socket.js b/test/test-reconnecting-socket.js index f3464be164..bbf9dc783b 100644 --- a/test/test-reconnecting-socket.js +++ b/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); +} diff --git a/test/test-setTimeout.js b/test/test-setTimeout.js deleted file mode 100644 index 718373c607..0000000000 --- a/test/test-setTimeout.js +++ /dev/null @@ -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); -} diff --git a/test/test-pingpong.js b/test/test-tcp-pingpong.js similarity index 78% rename from test/test-pingpong.js rename to test/test-tcp-pingpong.js index b373d820a5..6e62dca4da 100644 --- a/test/test-pingpong.js +++ b/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); +} diff --git a/test/test-timers.js b/test/test-timers.js new file mode 100644 index 0000000000..7ae3e2ec21 --- /dev/null +++ b/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); +}