Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 7 | // Test close(c), receive of closed channel. |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 8 | // |
| 9 | // TODO(rsc): Doesn't check behavior of close(c) when there |
| 10 | // are blocked senders/receivers. |
| 11 | |
| 12 | package main |
| 13 | |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 14 | import "os" |
| 15 | |
| 16 | var failed bool |
| 17 | |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 18 | type Chan interface { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 19 | Send(int) |
| 20 | Nbsend(int) bool |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 21 | Recv() (int) |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 22 | Nbrecv() (int, bool) |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 23 | Recv2() (int, bool) |
| 24 | Nbrecv2() (int, bool, bool) |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 25 | Close() |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 26 | Impl() string |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 29 | // direct channel operations when possible |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 30 | type XChan chan int |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 31 | |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 32 | func (c XChan) Send(x int) { |
| 33 | c <- x |
| 34 | } |
| 35 | |
| 36 | func (c XChan) Nbsend(x int) bool { |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 37 | select { |
| 38 | case c <- x: |
| 39 | return true |
| 40 | default: |
| 41 | return false |
| 42 | } |
| 43 | panic("nbsend") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (c XChan) Recv() int { |
| 47 | return <-c |
| 48 | } |
| 49 | |
| 50 | func (c XChan) Nbrecv() (int, bool) { |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 51 | select { |
| 52 | case x := <-c: |
| 53 | return x, true |
| 54 | default: |
| 55 | return 0, false |
| 56 | } |
| 57 | panic("nbrecv") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 60 | func (c XChan) Recv2() (int, bool) { |
| 61 | x, ok := <-c |
| 62 | return x, ok |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 65 | func (c XChan) Nbrecv2() (int, bool, bool) { |
| 66 | select { |
| 67 | case x, ok := <-c: |
| 68 | return x, ok, true |
| 69 | default: |
| 70 | return 0, false, false |
| 71 | } |
| 72 | panic("nbrecv2") |
| 73 | } |
| 74 | |
| 75 | func (c XChan) Close() { |
| 76 | close(c) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func (c XChan) Impl() string { |
| 80 | return "(<- operator)" |
| 81 | } |
| 82 | |
| 83 | // indirect operations via select |
| 84 | type SChan chan int |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 85 | |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 86 | func (c SChan) Send(x int) { |
| 87 | select { |
| 88 | case c <- x: |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func (c SChan) Nbsend(x int) bool { |
| 93 | select { |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 94 | default: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 95 | return false |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 96 | case c <- x: |
| 97 | return true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 98 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 99 | panic("nbsend") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (c SChan) Recv() int { |
| 103 | select { |
| 104 | case x := <-c: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 105 | return x |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 106 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 107 | panic("recv") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func (c SChan) Nbrecv() (int, bool) { |
| 111 | select { |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 112 | default: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 113 | return 0, false |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 114 | case x := <-c: |
| 115 | return x, true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 116 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 117 | panic("nbrecv") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 120 | func (c SChan) Recv2() (int, bool) { |
| 121 | select { |
| 122 | case x, ok := <-c: |
| 123 | return x, ok |
| 124 | } |
| 125 | panic("recv") |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 128 | func (c SChan) Nbrecv2() (int, bool, bool) { |
| 129 | select { |
| 130 | default: |
| 131 | return 0, false, false |
| 132 | case x, ok := <-c: |
| 133 | return x, ok, true |
| 134 | } |
| 135 | panic("nbrecv") |
| 136 | } |
| 137 | |
| 138 | func (c SChan) Close() { |
| 139 | close(c) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | func (c SChan) Impl() string { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 143 | return "(select)" |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 146 | // indirect operations via larger selects |
| 147 | var dummy = make(chan bool) |
| 148 | |
| 149 | type SSChan chan int |
| 150 | |
| 151 | func (c SSChan) Send(x int) { |
| 152 | select { |
| 153 | case c <- x: |
| 154 | case <-dummy: |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func (c SSChan) Nbsend(x int) bool { |
| 159 | select { |
| 160 | default: |
| 161 | return false |
| 162 | case <-dummy: |
| 163 | case c <- x: |
| 164 | return true |
| 165 | } |
| 166 | panic("nbsend") |
| 167 | } |
| 168 | |
| 169 | func (c SSChan) Recv() int { |
| 170 | select { |
| 171 | case <-dummy: |
| 172 | case x := <-c: |
| 173 | return x |
| 174 | } |
| 175 | panic("recv") |
| 176 | } |
| 177 | |
| 178 | func (c SSChan) Nbrecv() (int, bool) { |
| 179 | select { |
| 180 | case <-dummy: |
| 181 | default: |
| 182 | return 0, false |
| 183 | case x := <-c: |
| 184 | return x, true |
| 185 | } |
| 186 | panic("nbrecv") |
| 187 | } |
| 188 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 189 | func (c SSChan) Recv2() (int, bool) { |
| 190 | select { |
| 191 | case <-dummy: |
| 192 | case x, ok := <-c: |
| 193 | return x, ok |
| 194 | } |
| 195 | panic("recv") |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 198 | func (c SSChan) Nbrecv2() (int, bool, bool) { |
| 199 | select { |
| 200 | case <-dummy: |
| 201 | default: |
| 202 | return 0, false, false |
| 203 | case x, ok := <-c: |
| 204 | return x, ok, true |
| 205 | } |
| 206 | panic("nbrecv") |
| 207 | } |
| 208 | |
| 209 | func (c SSChan) Close() { |
| 210 | close(c) |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | func (c SSChan) Impl() string { |
| 214 | return "(select)" |
| 215 | } |
| 216 | |
| 217 | |
Russ Cox | 27c74d3 | 2011-01-21 15:07:13 -0500 | [diff] [blame] | 218 | func shouldPanic(f func()) { |
| 219 | defer func() { |
| 220 | if recover() == nil { |
| 221 | panic("did not panic") |
| 222 | } |
| 223 | }() |
| 224 | f() |
| 225 | } |
| 226 | |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 227 | func test1(c Chan) { |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 228 | for i := 0; i < 3; i++ { |
| 229 | // recv a close signal (a zero value) |
| 230 | if x := c.Recv(); x != 0 { |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 231 | println("test1: recv on closed:", x, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 232 | failed = true |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 233 | } |
| 234 | if x, ok := c.Recv2(); x != 0 || ok { |
| 235 | println("test1: recv2 on closed:", x, ok, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 236 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 239 | // should work with select: received a value without blocking, so selected == true. |
| 240 | x, selected := c.Nbrecv() |
| 241 | if x != 0 || !selected { |
| 242 | println("test1: recv on closed nb:", x, selected, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 243 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 244 | } |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 245 | x, ok, selected := c.Nbrecv2() |
| 246 | if x != 0 || ok || !selected { |
| 247 | println("test1: recv2 on closed nb:", x, ok, selected, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 248 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // send should work with ,ok too: sent a value without blocking, so ok == true. |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 253 | shouldPanic(func() { c.Nbsend(1) }) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 254 | |
Russ Cox | 27c74d3 | 2011-01-21 15:07:13 -0500 | [diff] [blame] | 255 | // the value should have been discarded. |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 256 | if x := c.Recv(); x != 0 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 257 | println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 258 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // similarly Send. |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 262 | shouldPanic(func() { c.Send(2) }) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 263 | if x := c.Recv(); x != 0 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 264 | println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 265 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | func testasync1(c Chan) { |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 270 | // should be able to get the last value via Recv |
| 271 | if x := c.Recv(); x != 1 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 272 | println("testasync1: Recv did not get 1:", x, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 273 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 276 | test1(c) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | func testasync2(c Chan) { |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 280 | // should be able to get the last value via Recv2 |
| 281 | if x, ok := c.Recv2(); x != 1 || !ok { |
| 282 | println("testasync1: Recv did not get 1, true:", x, ok, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 283 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 286 | test1(c) |
| 287 | } |
| 288 | |
| 289 | func testasync3(c Chan) { |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 290 | // should be able to get the last value via Nbrecv |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 291 | if x, selected := c.Nbrecv(); x != 1 || !selected { |
| 292 | println("testasync2: Nbrecv did not get 1, true:", x, selected, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 293 | failed = true |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 296 | test1(c) |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 299 | func testasync4(c Chan) { |
| 300 | // should be able to get the last value via Nbrecv2 |
| 301 | if x, ok, selected := c.Nbrecv2(); x != 1 || !ok || !selected { |
| 302 | println("testasync2: Nbrecv did not get 1, true, true:", x, ok, selected, c.Impl()) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 303 | failed = true |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 304 | } |
| 305 | test1(c) |
| 306 | } |
| 307 | |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 308 | func closedsync() chan int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 309 | c := make(chan int) |
| 310 | close(c) |
| 311 | return c |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | func closedasync() chan int { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 315 | c := make(chan int, 2) |
| 316 | c <- 1 |
| 317 | close(c) |
| 318 | return c |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 321 | var mks = []func(chan int) Chan { |
| 322 | func(c chan int) Chan { return XChan(c) }, |
| 323 | func(c chan int) Chan { return SChan(c) }, |
| 324 | func(c chan int) Chan { return SSChan(c) }, |
| 325 | } |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 326 | |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 327 | var testcloseds = []func(Chan) { |
| 328 | testasync1, |
| 329 | testasync2, |
| 330 | testasync3, |
| 331 | testasync4, |
| 332 | } |
| 333 | |
| 334 | func main() { |
| 335 | for _, mk := range mks { |
| 336 | test1(mk(closedsync())) |
| 337 | } |
| 338 | |
| 339 | for _, testclosed := range testcloseds { |
| 340 | for _, mk := range mks { |
| 341 | testclosed(mk(closedasync())) |
| 342 | } |
| 343 | } |
Russ Cox | f58ed4e | 2011-10-13 16:58:04 -0400 | [diff] [blame] | 344 | |
| 345 | var ch chan int |
| 346 | shouldPanic(func() { |
| 347 | close(ch) |
| 348 | }) |
| 349 | |
| 350 | ch = make(chan int) |
| 351 | close(ch) |
| 352 | shouldPanic(func() { |
| 353 | close(ch) |
| 354 | }) |
Ian Lance Taylor | 7f0622e | 2011-11-04 14:12:35 -0700 | [diff] [blame] | 355 | |
| 356 | if failed { |
| 357 | os.Exit(1) |
| 358 | } |
Russ Cox | 13584f4 | 2009-03-23 18:50:35 -0700 | [diff] [blame] | 359 | } |