1 // $G $D/$F.go && $L $F.$A && ./$A.out
3 // Copyright 2010 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.
7 // Tests verifying the semantics of the select statement
8 // for basic empty/non-empty cases.
14 const always = "function did not"
15 const never = "function did"
19 panic("control flow shouldn't reach here")
23 // Calls f and verifies that f always/never panics depending on signal.
24 func testPanic(signal string, f func()) {
28 s = always // f panicked
31 panic(signal + " panic")
38 // Calls f and empirically verifies that f always/never blocks depending on signal.
39 func testBlock(signal string, f func()) {
40 c := make(chan string)
43 c <- never // f didn't block
46 time.Sleep(1e8) // 0.1s seems plenty long
47 c <- always // f blocked always
50 panic(signal + " block")
56 const async = 1 // asynchronous channels
58 closedch := make(chan int)
61 // sending/receiving from a nil channel outside a select panics
62 testPanic(always, func() {
65 testPanic(always, func() {
69 // sending/receiving from a nil channel inside a select never panics
70 testPanic(never, func() {
77 testPanic(never, func() {
85 // sending to an async channel with free buffer space never blocks
86 testBlock(never, func() {
87 ch := make(chan int, async)
91 // receiving (a small number of times) from a closed channel never blocks
92 testBlock(never, func() {
93 for i := 0; i < 10; i++ {
95 panic("expected zero value when reading from closed channel")
100 // sending (a small number of times) to a closed channel is not specified
101 // but the current implementation doesn't block: test that different
102 // implementations behave the same
103 testBlock(never, func() {
104 for i := 0; i < 10; i++ {
109 // receiving from a non-ready channel always blocks
110 testBlock(always, func() {
115 // empty selects always block
116 testBlock(always, func() {
121 // selects with only nil channels always block
122 testBlock(always, func() {
128 testBlock(always, func() {
134 testBlock(always, func() {
143 // selects with non-ready non-nil channels always block
144 testBlock(always, func() {
152 // selects with default cases don't block
153 testBlock(never, func() {
158 testBlock(never, func() {
165 testBlock(never, func() {
173 // selects with ready channels don't block
174 testBlock(never, func() {
175 ch := make(chan int, async)
182 testBlock(never, func() {
183 ch := make(chan int, async)
192 // selects with closed channels don't block
193 testBlock(never, func() {
198 testBlock(never, func() {