1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// $G $D/$F.go && $L $F.$A && ./$A.out
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Tests verifying the semantics of the select statement
// for basic empty/non-empty cases.
package main
import "time"
const always = "function did not"
const never = "function did"
func unreachable() {
panic("control flow shouldn't reach here")
}
// Calls f and verifies that f always/never panics depending on signal.
func testPanic(signal string, f func()) {
defer func() {
s := never
if recover() != nil {
s = always // f panicked
}
if s != signal {
panic(signal + " panic")
}
}()
f()
}
// Calls f and empirically verifies that f always/never blocks depending on signal.
func testBlock(signal string, f func()) {
c := make(chan string)
go func() {
f()
c <- never // f didn't block
}()
go func() {
time.Sleep(1e8) // 0.1s seems plenty long
c <- always // f blocked always
}()
if <-c != signal {
panic(signal + " block")
}
}
func main() {
const async = 1 // asynchronous channels
var nilch chan int
closedch := make(chan int)
close(closedch)
// sending/receiving from a nil channel outside a select panics
testPanic(always, func() {
nilch <- 7
})
testPanic(always, func() {
<-nilch
})
// sending/receiving from a nil channel inside a select never panics
testPanic(never, func() {
select {
case nilch <- 7:
unreachable()
default:
}
})
testPanic(never, func() {
select {
case <-nilch:
unreachable()
default:
}
})
// sending to an async channel with free buffer space never blocks
testBlock(never, func() {
ch := make(chan int, async)
ch <- 7
})
// receiving (a small number of times) from a closed channel never blocks
testBlock(never, func() {
for i := 0; i < 10; i++ {
if <-closedch != 0 {
panic("expected zero value when reading from closed channel")
}
}
})
// sending (a small number of times) to a closed channel is not specified
// but the current implementation doesn't block: test that different
// implementations behave the same
testBlock(never, func() {
for i := 0; i < 10; i++ {
closedch <- 7
}
})
// receiving from a non-ready channel always blocks
testBlock(always, func() {
ch := make(chan int)
<-ch
})
// empty selects always block
testBlock(always, func() {
select {
}
})
// selects with only nil channels always block
testBlock(always, func() {
select {
case <-nilch:
unreachable()
}
})
testBlock(always, func() {
select {
case nilch <- 7:
unreachable()
}
})
testBlock(always, func() {
select {
case <-nilch:
unreachable()
case nilch <- 7:
unreachable()
}
})
// selects with non-ready non-nil channels always block
testBlock(always, func() {
ch := make(chan int)
select {
case <-ch:
unreachable()
}
})
// selects with default cases don't block
testBlock(never, func() {
select {
default:
}
})
testBlock(never, func() {
select {
case <-nilch:
unreachable()
default:
}
})
testBlock(never, func() {
select {
case nilch <- 7:
unreachable()
default:
}
})
// selects with ready channels don't block
testBlock(never, func() {
ch := make(chan int, async)
select {
case ch <- 7:
default:
unreachable()
}
})
testBlock(never, func() {
ch := make(chan int, async)
ch <- 7
select {
case <-ch:
default:
unreachable()
}
})
// selects with closed channels don't block
testBlock(never, func() {
select {
case <-closedch:
}
})
testBlock(never, func() {
select {
case closedch <- 7:
}
})
}
|