Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package net |
| 6 | |
| 7 | import ( |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 8 | "os"; |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 9 | "testing"; |
| 10 | "time"; |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 11 | ) |
| 12 | |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 13 | func testTimeout(t *testing.T, network, addr string, readFrom bool) { |
Russ Cox | b04ac10 | 2009-08-12 13:19:17 -0700 | [diff] [blame] | 14 | fd, err := Dial(network, "", addr); |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 15 | defer fd.Close(); |
| 16 | if err != nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 17 | t.Errorf("dial %s %s failed: %v", network, addr, err) |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 18 | } |
| 19 | t0 := time.Nanoseconds(); |
| 20 | fd.SetReadTimeout(1e8); // 100ms |
| 21 | var b [100]byte; |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 22 | var n int; |
| 23 | var err1 os.Error; |
| 24 | if readFrom { |
| 25 | n, _, err1 = fd.(PacketConn).ReadFrom(&b) |
| 26 | } else { |
| 27 | n, err1 = fd.Read(&b) |
| 28 | } |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 29 | t1 := time.Nanoseconds(); |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 30 | what := "Read"; |
| 31 | if readFrom { |
| 32 | what = "ReadFrom" |
| 33 | } |
Russ Cox | a0bcaf4 | 2009-06-25 20:24:55 -0700 | [diff] [blame] | 34 | if n != 0 || !isEAGAIN(err1) { |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 35 | t.Errorf("fd.%s on %s %s did not return 0, EAGAIN: %v, %v", what, network, addr, n, err1) |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 36 | } |
Robert Griesemer | 5d37705 | 2009-11-04 23:16:46 -0800 | [diff] [blame] | 37 | if t1-t0 < 0.5e8 || t1-t0 > 1.5e8 { |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 38 | t.Errorf("fd.%s on %s %s took %f seconds, expected 0.1", what, network, addr, float64(t1-t0)/1e9) |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 42 | func TestTimeoutUDP(t *testing.T) { |
| 43 | testTimeout(t, "udp", "127.0.0.1:53", false); |
| 44 | testTimeout(t, "udp", "127.0.0.1:53", true); |
| 45 | } |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 46 | |
| 47 | func TestTimeoutTCP(t *testing.T) { |
| 48 | // 74.125.19.99 is www.google.com. |
| 49 | // could use dns, but dns depends on |
| 50 | // timeouts and this is the timeout test. |
Russ Cox | 6e788e0 | 2009-11-17 08:39:17 -0800 | [diff] [blame] | 51 | testTimeout(t, "tcp", "74.125.19.99:80", false) |
Russ Cox | 1e37e8a | 2009-03-06 17:51:31 -0800 | [diff] [blame] | 52 | } |