0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/test/mjsunit/test-tcp-reconnect.js
Ryan Dahl 8185e1fd25 Remove include() add node.mixin()
include() should not be used by libraries because it will pollute the global
namespace. To discourage this behavior and bring Node more in-line with
the current CommonJS module system, include() is removed.

Small scripts like unit tests often times do want to pollute the global
namespace for ease. To avoid the boiler plate code of

  var x = require("/x.js");
  var foo = x.foo;
  var bar = x.bar;

The function node.mixin() is stolen from jQuery's jQuery.extend. So that it
can be written:

  node.mixin(require("/x.js"));

Reference:
http://docs.jquery.com/Utilities/jQuery.extend
http://groups.google.com/group/nodejs/browse_thread/thread/f9ac83e5c11e7e87
2009-10-05 15:46:31 +02:00

54 lines
1.2 KiB
JavaScript

node.mixin(require("common.js"));
tcp = require("/tcp.js");
var N = 50;
var port = 8921;
var c = 0;
var client_recv_count = 0;
var disconnect_count = 0;
var server = tcp.createServer(function (socket) {
socket.addListener("connect", function () {
socket.send("hello\r\n");
});
socket.addListener("eof", function () {
socket.close();
});
socket.addListener("close", function (had_error) {
//puts("server had_error: " + JSON.stringify(had_error));
assertFalse(had_error);
});
});
server.listen(port);
var client = tcp.createConnection(port);
client.setEncoding("UTF8");
client.addListener("connect", function () {
puts("client connected.");
});
client.addListener("receive", function (chunk) {
client_recv_count += 1;
puts("client_recv_count " + client_recv_count);
assertEquals("hello\r\n", chunk);
client.close();
});
client.addListener("close", function (had_error) {
puts("disconnect");
assertFalse(had_error);
if (disconnect_count++ < N)
client.connect(port); // reconnect
else
server.close();
});
process.addListener("exit", function () {
assertEquals(N+1, disconnect_count);
assertEquals(N+1, client_recv_count);
});