summaryrefslogtreecommitdiff
path: root/test/test-pingpong.js
blob: 8ad2cf2500e9cf58c26d33082e98a6ee25fca544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
include("mjsunit");

var port = 12123;
var N = 1000;
var count = 0;

function Ponger (socket, server) {
  this.encoding = "UTF8";
  this.timeout = 0;

  this.onConnect = function () {
    puts("got socket.");
  };

  this.onReceive = function (data) {
    assertTrue(count <= N);
    stdout.print ("-");
    if (/PING/.exec(data)) {
      socket.send("PONG");
    }
  };

  this.onEOF = function () {
    puts("ponger: onEOF");
    socket.send("QUIT");
    socket.close();
  };

  this.onDisconnect = function () {
    puts("ponger: onDisconnect");
    server.close();
  };
}

function Pinger (socket) {
  this.encoding = "UTF8";

  this.onConnect = function () {
    socket.send("PING");
  };

  this.onReceive = function (data) {
    stdout.print(".");
    assertEquals("PONG", data);
    count += 1; 
    if (count < N) {
      socket.send("PING");
    } else {
      puts("sending FIN");
      socket.sendEOF();
    }
  };

  this.onEOF = function () {
    puts("pinger: onEOF");
    assertEquals(N, count);
  };
}

function onLoad() {
  var server = new TCPServer(Ponger);
  server.listen(port);

  var client = new TCPConnection(Pinger);
  client.connect(port);
}