• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const net = require('net');
26
27let exchanges = 0;
28let starttime = null;
29let timeouttime = null;
30const timeout = 1000;
31
32const echo_server = net.createServer((socket) => {
33  socket.setTimeout(timeout);
34
35  socket.on('timeout', () => {
36    console.log('server timeout');
37    timeouttime = new Date();
38    console.dir(timeouttime);
39    socket.destroy();
40  });
41
42  socket.on('error', (e) => {
43    throw new Error(
44      'Server side socket should not get error. We disconnect willingly.');
45  });
46
47  socket.on('data', (d) => {
48    console.log(d);
49    socket.write(d);
50  });
51
52  socket.on('end', () => {
53    socket.end();
54  });
55});
56
57echo_server.listen(0, () => {
58  const port = echo_server.address().port;
59  console.log(`server listening at ${port}`);
60
61  const client = net.createConnection(port);
62  client.setEncoding('UTF8');
63  client.setTimeout(0); // Disable the timeout for client
64  client.on('connect', () => {
65    console.log('client connected.');
66    client.write('hello\r\n');
67  });
68
69  client.on('data', (chunk) => {
70    assert.strictEqual(chunk, 'hello\r\n');
71    if (exchanges++ < 5) {
72      setTimeout(() => {
73        console.log('client write "hello"');
74        client.write('hello\r\n');
75      }, 500);
76
77      if (exchanges === 5) {
78        console.log(`wait for timeout - should come in ${timeout} ms`);
79        starttime = new Date();
80        console.dir(starttime);
81      }
82    }
83  });
84
85  client.on('timeout', () => {
86    throw new Error("client timeout - this shouldn't happen");
87  });
88
89  client.on('end', () => {
90    console.log('client end');
91    client.end();
92  });
93
94  client.on('close', () => {
95    console.log('client disconnect');
96    echo_server.close();
97  });
98});
99
100process.on('exit', () => {
101  assert.ok(starttime != null);
102  assert.ok(timeouttime != null);
103
104  const diff = timeouttime - starttime;
105  console.log(`diff = ${diff}`);
106
107  assert.ok(timeout < diff);
108
109  // Allow for 800 milliseconds more
110  assert.ok(diff < timeout + 800);
111});
112