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, net, addr string, readFrom bool) {
15 c, err := Dial(net, addr)
17 t.Errorf("Dial(%q, %q) failed: %v", net, addr, err)
26 errc := make(chan error, 1)
29 c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
34 n, _, err = c.(PacketConn).ReadFrom(b[0:])
36 n, err = c.Read(b[0:])
39 if n != 0 || err == nil || !err.(Error).Timeout() {
40 errc <- fmt.Errorf("%s(%q, %q) did not return 0, timeout: %v, %v", what, net, addr, n, err)
43 if dt := t1.Sub(t0); dt < 50*time.Millisecond || !testing.Short() && dt > 250*time.Millisecond {
44 errc <- fmt.Errorf("%s(%q, %q) took %s, expected 0.1s", what, net, addr, dt)
54 case <-time.After(1 * time.Second):
55 t.Errorf("%s(%q, %q) took over 1 second, expected 0.1s", what, net, addr)
59 func TestTimeoutUDP(t *testing.T) {
62 t.Logf("skipping test on %q", runtime.GOOS)
66 // set up a listener that won't talk back
67 listening := make(chan string)
68 done := make(chan int)
69 go runDatagramPacketConnServer(t, "udp", "127.0.0.1:0", listening, done)
72 testTimeout(t, "udp", addr, false)
73 testTimeout(t, "udp", addr, true)
77 func TestTimeoutTCP(t *testing.T) {
80 t.Logf("skipping test on %q", runtime.GOOS)
84 // set up a listener that won't talk back
85 listening := make(chan string)
86 done := make(chan int)
87 go runStreamConnServer(t, "tcp", "127.0.0.1:0", listening, done)
90 testTimeout(t, "tcp", addr, false)
94 func TestDeadlineReset(t *testing.T) {
97 t.Logf("skipping test on %q", runtime.GOOS)
100 ln, err := Listen("tcp", "127.0.0.1:0")
105 tl := ln.(*TCPListener)
106 tl.SetDeadline(time.Now().Add(1 * time.Minute))
107 tl.SetDeadline(time.Time{}) // reset it
108 errc := make(chan error, 1)
110 _, err := ln.Accept()
114 case <-time.After(50 * time.Millisecond):
117 // Accept should never return; we never
119 t.Errorf("unexpected return from Accept; err=%v", err)