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 socket.write(d); 49 }); 50 51 socket.on('end', () => { 52 socket.end(); 53 }); 54}); 55 56echo_server.listen(0, () => { 57 const port = echo_server.address().port; 58 console.log(`server listening at ${port}`); 59 60 const client = net.createConnection(port); 61 client.setEncoding('UTF8'); 62 client.setTimeout(0); // Disable the timeout for client 63 client.on('connect', () => { 64 console.log('client connected.'); 65 client.write('hello\r\n'); 66 }); 67 68 client.on('data', (chunk) => { 69 assert.strictEqual(chunk, 'hello\r\n'); 70 if (exchanges++ < 5) { 71 setTimeout(() => { 72 console.log('client write "hello"'); 73 client.write('hello\r\n'); 74 }, 500); 75 76 if (exchanges === 5) { 77 console.log(`wait for timeout - should come in ${timeout} ms`); 78 starttime = new Date(); 79 console.dir(starttime); 80 } 81 } 82 }); 83 84 client.on('timeout', () => { 85 throw new Error("client timeout - this shouldn't happen"); 86 }); 87 88 client.on('end', () => { 89 console.log('client end'); 90 client.end(); 91 }); 92 93 client.on('close', () => { 94 console.log('client disconnect'); 95 echo_server.close(); 96 }); 97}); 98 99process.on('exit', () => { 100 assert.ok(starttime != null); 101 assert.ok(timeouttime != null); 102 103 const diff = timeouttime - starttime; 104 console.log(`diff = ${diff}`); 105 106 assert.ok(timeout < diff); 107}); 108