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.
14 func testTimeout(t *testing.T, network, addr string, readFrom bool) {
15 // Timeouts are not implemented on windows.
16 if runtime.GOOS == "windows" {
19 fd, err := Dial(network, "", addr)
21 t.Errorf("dial %s %s failed: %v", network, addr, err)
25 t0 := time.Nanoseconds()
26 fd.SetReadTimeout(1e8) // 100ms
31 n, _, err1 = fd.(PacketConn).ReadFrom(b[0:])
33 n, err1 = fd.Read(b[0:])
35 t1 := time.Nanoseconds()
40 if n != 0 || err1 == nil || !err1.(Error).Timeout() {
41 t.Errorf("fd.%s on %s %s did not return 0, timeout: %v, %v", what, network, addr, n, err1)
43 if t1-t0 < 0.5e8 || t1-t0 > 1.5e8 {
44 t.Errorf("fd.%s on %s %s took %f seconds, expected 0.1", what, network, addr, float64(t1-t0)/1e9)
48 func TestTimeoutUDP(t *testing.T) {
49 testTimeout(t, "udp", "127.0.0.1:53", false)
50 testTimeout(t, "udp", "127.0.0.1:53", true)
53 func TestTimeoutTCP(t *testing.T) {
54 // 74.125.19.99 is www.google.com.
55 // could use dns, but dns depends on
56 // timeouts and this is the timeout test.
57 testTimeout(t, "tcp", "74.125.19.99:80", false)