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.
16 func TestShutdown(t *testing.T) {
17 if runtime.GOOS == "plan9" {
18 t.Skipf("skipping test on %q", runtime.GOOS)
20 ln, err := Listen("tcp", "127.0.0.1:0")
22 if ln, err = Listen("tcp6", "[::1]:0"); err != nil {
23 t.Fatalf("ListenTCP on :0: %v", err)
30 t.Fatalf("Accept: %v", err)
33 n, err := c.Read(buf[:])
34 if n != 0 || err != io.EOF {
35 t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err)
37 c.Write([]byte("response"))
41 c, err := Dial("tcp", ln.Addr().String())
43 t.Fatalf("Dial: %v", err)
47 err = c.(*TCPConn).CloseWrite()
49 t.Fatalf("CloseWrite: %v", err)
52 n, err := c.Read(buf[:])
54 t.Fatalf("client Read: %d, %v", n, err)
56 got := string(buf[:n])
57 if got != "response" {
58 t.Errorf("read = %q, want \"response\"", got)
62 func TestShutdownUnix(t *testing.T) {
64 case "windows", "plan9":
65 t.Skipf("skipping test on %q", runtime.GOOS)
67 f, err := ioutil.TempFile("", "go_net_unixtest")
69 t.Fatalf("TempFile: %s", err)
74 ln, err := Listen("unix", tmpname)
76 t.Fatalf("ListenUnix on %s: %s", tmpname, err)
78 defer os.Remove(tmpname)
83 t.Fatalf("Accept: %v", err)
86 n, err := c.Read(buf[:])
87 if n != 0 || err != io.EOF {
88 t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err)
90 c.Write([]byte("response"))
94 c, err := Dial("unix", tmpname)
96 t.Fatalf("Dial: %v", err)
100 err = c.(*UnixConn).CloseWrite()
102 t.Fatalf("CloseWrite: %v", err)
105 n, err := c.Read(buf[:])
107 t.Fatalf("client Read: %d, %v", n, err)
109 got := string(buf[:n])
110 if got != "response" {
111 t.Errorf("read = %q, want \"response\"", got)
115 func TestTCPListenClose(t *testing.T) {
116 ln, err := Listen("tcp", "127.0.0.1:0")
118 t.Fatalf("Listen failed: %v", err)
121 done := make(chan bool, 1)
123 time.Sleep(100 * time.Millisecond)
127 c, err := ln.Accept()
130 t.Error("Accept succeeded")
132 t.Logf("Accept timeout error: %s (any error is fine)", err)
138 case <-time.After(2 * time.Second):
139 t.Fatal("timeout waiting for TCP close")
143 func TestUDPListenClose(t *testing.T) {
144 switch runtime.GOOS {
146 t.Skipf("skipping test on %q", runtime.GOOS)
148 ln, err := ListenPacket("udp", "127.0.0.1:0")
150 t.Fatalf("Listen failed: %v", err)
153 buf := make([]byte, 1000)
154 done := make(chan bool, 1)
156 time.Sleep(100 * time.Millisecond)
160 _, _, err = ln.ReadFrom(buf)
162 t.Error("ReadFrom succeeded")
164 t.Logf("ReadFrom timeout error: %s (any error is fine)", err)
170 case <-time.After(2 * time.Second):
171 t.Fatal("timeout waiting for UDP close")
175 func TestTCPClose(t *testing.T) {
176 switch runtime.GOOS {
178 t.Skipf("skipping test on %q", runtime.GOOS)
180 l, err := Listen("tcp", "127.0.0.1:0")
186 read := func(r io.Reader) error {
188 _, err := r.Read(m[:])
193 c, err := Dial("tcp", l.Addr().String())
200 time.Sleep(10 * time.Millisecond)
213 if err != nil && err != io.EOF {