blob: 7e3c0c74daec7568d6d1b6ee4f3ca2d785e5fcb7 [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Rob Pikef6c13bb2008-07-16 14:41:27 -07002
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
Rob Pike3fb5f322012-02-19 17:44:02 +11007// Test channel operations that test for blocking.
8// Use several sizes and types of operands.
Rob Pikef6c13bb2008-07-16 14:41:27 -07009
10package main
11
Russ Cox918afd942009-05-08 15:21:41 -070012import "runtime"
Russ Coxd2117ad2009-01-23 17:04:56 -080013import "time"
Rob Pikef6c13bb2008-07-16 14:41:27 -070014
Russ Coxd2117ad2009-01-23 17:04:56 -080015func i32receiver(c chan int32, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070016 if <-c != 123 {
17 panic("i32 value")
18 }
Russ Coxd2117ad2009-01-23 17:04:56 -080019 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070020}
21
Russ Coxd2117ad2009-01-23 17:04:56 -080022func i32sender(c chan int32, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070023 c <- 234
Russ Coxd2117ad2009-01-23 17:04:56 -080024 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070025}
26
Russ Coxd2117ad2009-01-23 17:04:56 -080027func i64receiver(c chan int64, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070028 if <-c != 123456 {
29 panic("i64 value")
30 }
Russ Coxd2117ad2009-01-23 17:04:56 -080031 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070032}
33
Russ Coxd2117ad2009-01-23 17:04:56 -080034func i64sender(c chan int64, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070035 c <- 234567
Russ Coxd2117ad2009-01-23 17:04:56 -080036 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070037}
38
Russ Coxd2117ad2009-01-23 17:04:56 -080039func breceiver(c chan bool, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070040 if !<-c {
41 panic("b value")
42 }
Russ Coxd2117ad2009-01-23 17:04:56 -080043 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070044}
45
Russ Coxd2117ad2009-01-23 17:04:56 -080046func bsender(c chan bool, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070047 c <- true
Russ Coxd2117ad2009-01-23 17:04:56 -080048 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070049}
50
Russ Coxd2117ad2009-01-23 17:04:56 -080051func sreceiver(c chan string, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070052 if <-c != "hello" {
53 panic("s value")
54 }
Russ Coxd2117ad2009-01-23 17:04:56 -080055 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070056}
57
Russ Coxd2117ad2009-01-23 17:04:56 -080058func ssender(c chan string, strobe chan bool) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070059 c <- "hello again"
Russ Coxd2117ad2009-01-23 17:04:56 -080060 strobe <- true
61}
62
Russ Cox00f9f0c2010-03-30 10:34:57 -070063var ticker = time.Tick(10 * 1000) // 10 us
Russ Coxd2117ad2009-01-23 17:04:56 -080064func sleep() {
Russ Cox00f9f0c2010-03-30 10:34:57 -070065 <-ticker
66 <-ticker
67 runtime.Gosched()
68 runtime.Gosched()
69 runtime.Gosched()
Rob Pikef6c13bb2008-07-16 14:41:27 -070070}
71
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -070072const maxTries = 10000 // Up to 100ms per test.
73
Rob Pikef6c13bb2008-07-16 14:41:27 -070074func main() {
Russ Cox00f9f0c2010-03-30 10:34:57 -070075 var i32 int32
76 var i64 int64
77 var b bool
78 var s string
Rob Pikef6c13bb2008-07-16 14:41:27 -070079
Russ Cox00f9f0c2010-03-30 10:34:57 -070080 var sync = make(chan bool)
Russ Coxd2117ad2009-01-23 17:04:56 -080081
Russ Coxd448d182008-09-26 11:47:04 -070082 for buffer := 0; buffer < 2; buffer++ {
Russ Cox00f9f0c2010-03-30 10:34:57 -070083 c32 := make(chan int32, buffer)
84 c64 := make(chan int64, buffer)
85 cb := make(chan bool, buffer)
86 cs := make(chan string, buffer)
Russ Cox08ca30b2008-12-19 03:05:37 -080087
Russ Coxf4e76d82011-01-31 18:36:28 -050088 select {
89 case i32 = <-c32:
Russ Cox00f9f0c2010-03-30 10:34:57 -070090 panic("blocked i32sender")
Russ Coxf4e76d82011-01-31 18:36:28 -050091 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -070092 }
Russ Cox08ca30b2008-12-19 03:05:37 -080093
Russ Coxf4e76d82011-01-31 18:36:28 -050094 select {
95 case i64 = <-c64:
Russ Cox00f9f0c2010-03-30 10:34:57 -070096 panic("blocked i64sender")
Russ Coxf4e76d82011-01-31 18:36:28 -050097 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -070098 }
Russ Cox08ca30b2008-12-19 03:05:37 -080099
Russ Coxf4e76d82011-01-31 18:36:28 -0500100 select {
101 case b = <-cb:
Russ Cox00f9f0c2010-03-30 10:34:57 -0700102 panic("blocked bsender")
Russ Coxf4e76d82011-01-31 18:36:28 -0500103 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -0700104 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800105
Russ Coxf4e76d82011-01-31 18:36:28 -0500106 select {
107 case s = <-cs:
Russ Cox00f9f0c2010-03-30 10:34:57 -0700108 panic("blocked ssender")
Russ Coxf4e76d82011-01-31 18:36:28 -0500109 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -0700110 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800111
Russ Cox00f9f0c2010-03-30 10:34:57 -0700112 go i32receiver(c32, sync)
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700113 try := 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500114 Send32:
115 for {
116 select {
117 case c32 <- 123:
118 break Send32
119 default:
120 try++
121 if try > maxTries {
122 println("i32receiver buffer=", buffer)
123 panic("fail")
124 }
125 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700126 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700127 }
128 <-sync
Russ Cox08ca30b2008-12-19 03:05:37 -0800129
Russ Cox00f9f0c2010-03-30 10:34:57 -0700130 go i32sender(c32, sync)
131 if buffer > 0 {
132 <-sync
Russ Cox00f9f0c2010-03-30 10:34:57 -0700133 }
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700134 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500135 Recv32:
136 for {
137 select {
138 case i32 = <-c32:
139 break Recv32
140 default:
141 try++
142 if try > maxTries {
143 println("i32sender buffer=", buffer)
144 panic("fail")
145 }
146 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700147 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700148 }
149 if i32 != 234 {
150 panic("i32sender value")
151 }
152 if buffer == 0 {
153 <-sync
154 }
Russ Coxd2117ad2009-01-23 17:04:56 -0800155
Russ Cox00f9f0c2010-03-30 10:34:57 -0700156 go i64receiver(c64, sync)
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700157 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500158 Send64:
159 for {
160 select {
161 case c64 <- 123456:
162 break Send64
163 default:
164 try++
165 if try > maxTries {
166 panic("i64receiver")
167 }
168 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700169 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700170 }
171 <-sync
Russ Coxd2117ad2009-01-23 17:04:56 -0800172
Russ Cox00f9f0c2010-03-30 10:34:57 -0700173 go i64sender(c64, sync)
174 if buffer > 0 {
175 <-sync
Russ Cox00f9f0c2010-03-30 10:34:57 -0700176 }
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700177 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500178 Recv64:
179 for {
180 select {
181 case i64 = <-c64:
182 break Recv64
183 default:
184 try++
185 if try > maxTries {
186 panic("i64sender")
187 }
188 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700189 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700190 }
191 if i64 != 234567 {
192 panic("i64sender value")
193 }
194 if buffer == 0 {
195 <-sync
196 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800197
Russ Cox00f9f0c2010-03-30 10:34:57 -0700198 go breceiver(cb, sync)
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700199 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500200 SendBool:
201 for {
202 select {
203 case cb <- true:
204 break SendBool
205 default:
206 try++
207 if try > maxTries {
208 panic("breceiver")
209 }
210 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700211 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700212 }
213 <-sync
Russ Coxd2117ad2009-01-23 17:04:56 -0800214
Russ Cox00f9f0c2010-03-30 10:34:57 -0700215 go bsender(cb, sync)
216 if buffer > 0 {
217 <-sync
Russ Cox00f9f0c2010-03-30 10:34:57 -0700218 }
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700219 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500220 RecvBool:
221 for {
222 select {
223 case b = <-cb:
224 break RecvBool
225 default:
226 try++
227 if try > maxTries {
228 panic("bsender")
229 }
230 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700231 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700232 }
233 if !b {
234 panic("bsender value")
235 }
236 if buffer == 0 {
237 <-sync
238 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800239
Russ Cox00f9f0c2010-03-30 10:34:57 -0700240 go sreceiver(cs, sync)
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700241 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500242 SendString:
243 for {
244 select {
245 case cs <- "hello":
246 break SendString
247 default:
248 try++
249 if try > maxTries {
250 panic("sreceiver")
251 }
252 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700253 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700254 }
255 <-sync
Russ Coxd2117ad2009-01-23 17:04:56 -0800256
Russ Cox00f9f0c2010-03-30 10:34:57 -0700257 go ssender(cs, sync)
258 if buffer > 0 {
259 <-sync
Russ Cox00f9f0c2010-03-30 10:34:57 -0700260 }
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700261 try = 0
Russ Coxf4e76d82011-01-31 18:36:28 -0500262 RecvString:
263 for {
264 select {
265 case s = <-cs:
266 break RecvString
267 default:
268 try++
269 if try > maxTries {
270 panic("ssender")
271 }
272 sleep()
Ian Lance Taylor8e985dc2010-09-10 15:37:20 -0700273 }
Russ Cox00f9f0c2010-03-30 10:34:57 -0700274 }
275 if s != "hello again" {
276 panic("ssender value")
277 }
278 if buffer == 0 {
279 <-sync
280 }
Russ Coxd448d182008-09-26 11:47:04 -0700281 }
Rob Pikef6c13bb2008-07-16 14:41:27 -0700282}