|
2 | 2 | // This example attempts to time out before the connection is established |
3 | 3 | // https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8 |
4 | 4 | // https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw |
5 | | -// |
6 | | -// TODO: how to do this without relying on the responses of specific sites? |
7 | 5 |
|
8 | 6 | var common = require('../common'); |
9 | 7 | var net = require('net'); |
10 | 8 | var assert = require('assert'); |
11 | 9 |
|
12 | 10 | var start = new Date(); |
13 | 11 |
|
14 | | -var gotTimeout0 = false; |
15 | | -var gotTimeout1 = false; |
| 12 | +var gotTimeout = false; |
16 | 13 |
|
17 | | -var gotConnect0 = false; |
18 | | -var gotConnect1 = false; |
| 14 | +var gotConnect = false; |
19 | 15 |
|
20 | 16 | var T = 100; |
21 | 17 |
|
22 | 18 |
|
23 | | -// With DNS |
| 19 | +// 240.*.*.*.* is "reserved for future use" |
| 20 | +var socket = net.createConnection(9999, '240.0.0.0'); |
24 | 21 |
|
25 | | -var socket0 = net.createConnection(9999, 'google.com'); |
| 22 | +socket.setTimeout(T); |
26 | 23 |
|
27 | | -socket0.setTimeout(T); |
28 | | - |
29 | | -socket0.on('timeout', function() { |
30 | | - console.error('timeout'); |
31 | | - gotTimeout0 = true; |
32 | | - var now = new Date(); |
33 | | - assert.ok(now - start < T + 500); |
34 | | - socket0.destroy(); |
35 | | -}); |
36 | | - |
37 | | -socket0.on('connect', function() { |
38 | | - console.error('connect'); |
39 | | - gotConnect0 = true; |
40 | | - socket0.destroy(); |
41 | | -}); |
42 | | - |
43 | | - |
44 | | -// Without DNS |
45 | | - |
46 | | -var socket1 = net.createConnection(9999, '24.24.24.24'); |
47 | | - |
48 | | -socket1.setTimeout(T); |
49 | | - |
50 | | -socket1.on('timeout', function() { |
| 24 | +socket.on('timeout', function() { |
51 | 25 | console.error('timeout'); |
52 | | - gotTimeout1 = true; |
| 26 | + gotTimeout = true; |
53 | 27 | var now = new Date(); |
54 | 28 | assert.ok(now - start < T + 500); |
55 | | - socket1.destroy(); |
| 29 | + socket.destroy(); |
56 | 30 | }); |
57 | 31 |
|
58 | | -socket1.on('connect', function() { |
| 32 | +socket.on('connect', function() { |
59 | 33 | console.error('connect'); |
60 | | - gotConnect1 = true; |
61 | | - socket1.destroy(); |
| 34 | + gotConnect = true; |
| 35 | + socket.destroy(); |
62 | 36 | }); |
63 | 37 |
|
64 | 38 |
|
65 | 39 | process.on('exit', function() { |
66 | | - assert.ok(gotTimeout0); |
67 | | - assert.ok(!gotConnect0); |
68 | | - |
69 | | - assert.ok(gotTimeout1); |
70 | | - assert.ok(!gotConnect1); |
| 40 | + assert.ok(gotTimeout); |
| 41 | + assert.ok(!gotConnect); |
71 | 42 | }); |
0 commit comments