OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "runtime" | 8 "runtime" |
9 "testing" | 9 "testing" |
10 "time" | 10 "time" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 77 } |
78 if !terr.Timeout() { | 78 if !terr.Timeout() { |
79 t.Fatalf("got error %q; not a timeout",
err) | 79 t.Fatalf("got error %q; not a timeout",
err) |
80 } | 80 } |
81 // Pass. We saw a timeout error. | 81 // Pass. We saw a timeout error. |
82 return | 82 return |
83 } | 83 } |
84 } | 84 } |
85 } | 85 } |
86 } | 86 } |
| 87 |
| 88 func TestSelfConnect(t *testing.T) { |
| 89 // Test that Dial does not honor self-connects. |
| 90 // See the comment in DialTCP. |
| 91 |
| 92 // Find a port that would be used as a local address. |
| 93 l, err := Listen("tcp", "127.0.0.1:0") |
| 94 if err != nil { |
| 95 t.Fatal(err) |
| 96 } |
| 97 c, err := Dial("tcp", l.Addr().String()) |
| 98 if err != nil { |
| 99 t.Fatal(err) |
| 100 } |
| 101 addr := c.LocalAddr().String() |
| 102 c.Close() |
| 103 l.Close() |
| 104 |
| 105 // Try to connect to that address repeatedly. |
| 106 n := 100000 |
| 107 if testing.Short() { |
| 108 n = 1000 |
| 109 } |
| 110 for i := 0; i < n; i++ { |
| 111 c, err := Dial("tcp", addr) |
| 112 if err == nil { |
| 113 c.Close() |
| 114 t.Errorf("#%d: Dial %q succeeded", i, addr) |
| 115 } |
| 116 } |
| 117 } |
OLD | NEW |