1
// Copyright Joyent, Inc. and other Node contributors.
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:
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
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.
22
// This example attempts to time out before the connection is established
23
// https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8
24
// https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw
26
// TODO: how to do this without relying on the responses of specific sites?
28
var common = require('../common');
29
var net = require('net');
30
var assert = require('assert');
32
var start = new Date();
34
var gotTimeout0 = false;
35
var gotTimeout1 = false;
37
var gotConnect0 = false;
38
var gotConnect1 = false;
45
var socket0 = net.createConnection(9999, 'google.com');
47
socket0.setTimeout(T);
49
socket0.on('timeout', function() {
50
console.error("timeout");
53
assert.ok(now - start < T + 500);
57
socket0.on('connect', function() {
58
console.error("connect");
66
var socket1 = net.createConnection(9999, '24.24.24.24');
68
socket1.setTimeout(T);
70
socket1.on('timeout', function() {
71
console.error("timeout");
74
assert.ok(now - start < T + 500);
78
socket1.on('connect', function() {
79
console.error("connect");
88
process.on('exit', function() {
89
assert.ok(gotTimeout0);
90
assert.ok(!gotConnect0);
92
assert.ok(gotTimeout1);
93
assert.ok(!gotConnect1);