github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/net_test.go (about) 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 ( 8 "io" 9 "io/ioutil" 10 "os" 11 "runtime" 12 "testing" 13 "time" 14 ) 15 16 func TestShutdown(t *testing.T) { 17 if runtime.GOOS == "plan9" { 18 t.Skipf("skipping test on %q", runtime.GOOS) 19 } 20 ln, err := Listen("tcp", "127.0.0.1:0") 21 if err != nil { 22 if ln, err = Listen("tcp6", "[::1]:0"); err != nil { 23 t.Fatalf("ListenTCP on :0: %v", err) 24 } 25 } 26 27 go func() { 28 defer ln.Close() 29 c, err := ln.Accept() 30 if err != nil { 31 t.Fatalf("Accept: %v", err) 32 } 33 var buf [10]byte 34 n, err := c.Read(buf[:]) 35 if n != 0 || err != io.EOF { 36 t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err) 37 } 38 c.Write([]byte("response")) 39 c.Close() 40 }() 41 42 c, err := Dial("tcp", ln.Addr().String()) 43 if err != nil { 44 t.Fatalf("Dial: %v", err) 45 } 46 defer c.Close() 47 48 err = c.(*TCPConn).CloseWrite() 49 if err != nil { 50 t.Fatalf("CloseWrite: %v", err) 51 } 52 var buf [10]byte 53 n, err := c.Read(buf[:]) 54 if err != nil { 55 t.Fatalf("client Read: %d, %v", n, err) 56 } 57 got := string(buf[:n]) 58 if got != "response" { 59 t.Errorf("read = %q, want \"response\"", got) 60 } 61 } 62 63 func TestShutdownUnix(t *testing.T) { 64 switch runtime.GOOS { 65 case "windows", "plan9": 66 t.Skipf("skipping test on %q", runtime.GOOS) 67 } 68 f, err := ioutil.TempFile("", "go_net_unixtest") 69 if err != nil { 70 t.Fatalf("TempFile: %s", err) 71 } 72 f.Close() 73 tmpname := f.Name() 74 os.Remove(tmpname) 75 ln, err := Listen("unix", tmpname) 76 if err != nil { 77 t.Fatalf("ListenUnix on %s: %s", tmpname, err) 78 } 79 defer func() { 80 ln.Close() 81 os.Remove(tmpname) 82 }() 83 84 go func() { 85 c, err := ln.Accept() 86 if err != nil { 87 t.Fatalf("Accept: %v", err) 88 } 89 var buf [10]byte 90 n, err := c.Read(buf[:]) 91 if n != 0 || err != io.EOF { 92 t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err) 93 } 94 c.Write([]byte("response")) 95 c.Close() 96 }() 97 98 c, err := Dial("unix", tmpname) 99 if err != nil { 100 t.Fatalf("Dial: %v", err) 101 } 102 defer c.Close() 103 104 err = c.(*UnixConn).CloseWrite() 105 if err != nil { 106 t.Fatalf("CloseWrite: %v", err) 107 } 108 var buf [10]byte 109 n, err := c.Read(buf[:]) 110 if err != nil { 111 t.Fatalf("client Read: %d, %v", n, err) 112 } 113 got := string(buf[:n]) 114 if got != "response" { 115 t.Errorf("read = %q, want \"response\"", got) 116 } 117 } 118 119 func TestTCPListenClose(t *testing.T) { 120 ln, err := Listen("tcp", "127.0.0.1:0") 121 if err != nil { 122 t.Fatalf("Listen failed: %v", err) 123 } 124 125 done := make(chan bool, 1) 126 go func() { 127 time.Sleep(100 * time.Millisecond) 128 ln.Close() 129 }() 130 go func() { 131 c, err := ln.Accept() 132 if err == nil { 133 c.Close() 134 t.Error("Accept succeeded") 135 } else { 136 t.Logf("Accept timeout error: %s (any error is fine)", err) 137 } 138 done <- true 139 }() 140 select { 141 case <-done: 142 case <-time.After(2 * time.Second): 143 t.Fatal("timeout waiting for TCP close") 144 } 145 } 146 147 func TestUDPListenClose(t *testing.T) { 148 switch runtime.GOOS { 149 case "plan9": 150 t.Skipf("skipping test on %q", runtime.GOOS) 151 } 152 ln, err := ListenPacket("udp", "127.0.0.1:0") 153 if err != nil { 154 t.Fatalf("Listen failed: %v", err) 155 } 156 157 buf := make([]byte, 1000) 158 done := make(chan bool, 1) 159 go func() { 160 time.Sleep(100 * time.Millisecond) 161 ln.Close() 162 }() 163 go func() { 164 _, _, err = ln.ReadFrom(buf) 165 if err == nil { 166 t.Error("ReadFrom succeeded") 167 } else { 168 t.Logf("ReadFrom timeout error: %s (any error is fine)", err) 169 } 170 done <- true 171 }() 172 select { 173 case <-done: 174 case <-time.After(2 * time.Second): 175 t.Fatal("timeout waiting for UDP close") 176 } 177 } 178 179 func TestTCPClose(t *testing.T) { 180 switch runtime.GOOS { 181 case "plan9": 182 t.Skipf("skipping test on %q", runtime.GOOS) 183 } 184 l, err := Listen("tcp", "127.0.0.1:0") 185 if err != nil { 186 t.Fatal(err) 187 } 188 defer l.Close() 189 190 read := func(r io.Reader) error { 191 var m [1]byte 192 _, err := r.Read(m[:]) 193 return err 194 } 195 196 go func() { 197 c, err := Dial("tcp", l.Addr().String()) 198 if err != nil { 199 t.Fatal(err) 200 } 201 202 go read(c) 203 204 time.Sleep(10 * time.Millisecond) 205 c.Close() 206 }() 207 208 c, err := l.Accept() 209 if err != nil { 210 t.Fatal(err) 211 } 212 defer c.Close() 213 214 for err == nil { 215 err = read(c) 216 } 217 if err != nil && err != io.EOF { 218 t.Fatal(err) 219 } 220 } 221 222 func TestErrorNil(t *testing.T) { 223 c, err := Dial("tcp", "127.0.0.1:65535") 224 if err == nil { 225 t.Fatal("Dial 127.0.0.1:65535 succeeded") 226 } 227 if c != nil { 228 t.Fatalf("Dial returned non-nil interface %T(%v) with err != nil", c, c) 229 } 230 231 // Make Listen fail by relistening on the same address. 232 l, err := Listen("tcp", "127.0.0.1:0") 233 if err != nil { 234 t.Fatal("Listen 127.0.0.1:0: %v", err) 235 } 236 defer l.Close() 237 l1, err := Listen("tcp", l.Addr().String()) 238 if err == nil { 239 t.Fatal("second Listen %v: %v", l.Addr(), err) 240 } 241 if l1 != nil { 242 t.Fatalf("Listen returned non-nil interface %T(%v) with err != nil", l1, l1) 243 } 244 245 // Make ListenPacket fail by relistening on the same address. 246 lp, err := ListenPacket("udp", "127.0.0.1:0") 247 if err != nil { 248 t.Fatal("Listen 127.0.0.1:0: %v", err) 249 } 250 defer lp.Close() 251 lp1, err := ListenPacket("udp", lp.LocalAddr().String()) 252 if err == nil { 253 t.Fatal("second Listen %v: %v", lp.LocalAddr(), err) 254 } 255 if lp1 != nil { 256 t.Fatalf("ListenPacket returned non-nil interface %T(%v) with err != nil", lp1, lp1) 257 } 258 }