2012-02-17 04:48:57 +00:00
|
|
|
// run
|
2008-07-16 21:41:27 +00:00
|
|
|
|
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2012-02-19 06:44:02 +00:00
|
|
|
// Test channel operations that test for blocking.
|
|
|
|
// Use several sizes and types of operands.
|
2008-07-16 21:41:27 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-05-08 22:21:41 +00:00
|
|
|
import "runtime"
|
2009-01-24 01:04:56 +00:00
|
|
|
import "time"
|
2008-07-16 21:41:27 +00:00
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func i32receiver(c chan int32, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
if <-c != 123 {
|
|
|
|
panic("i32 value")
|
|
|
|
}
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func i32sender(c chan int32, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
c <- 234
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func i64receiver(c chan int64, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
if <-c != 123456 {
|
|
|
|
panic("i64 value")
|
|
|
|
}
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func i64sender(c chan int64, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
c <- 234567
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func breceiver(c chan bool, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
if !<-c {
|
|
|
|
panic("b value")
|
|
|
|
}
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func bsender(c chan bool, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
c <- true
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-24 01:04:56 +00:00
|
|
|
func sreceiver(c chan string, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
if <-c != "hello" {
|
|
|
|
panic("s value")
|
|
|
|
}
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ssender(c chan string, strobe chan bool) {
|
2010-03-30 17:34:57 +00:00
|
|
|
c <- "hello again"
|
2009-01-24 01:04:56 +00:00
|
|
|
strobe <- true
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 17:34:57 +00:00
|
|
|
var ticker = time.Tick(10 * 1000) // 10 us
|
2009-01-24 01:04:56 +00:00
|
|
|
func sleep() {
|
2010-03-30 17:34:57 +00:00
|
|
|
<-ticker
|
|
|
|
<-ticker
|
|
|
|
runtime.Gosched()
|
|
|
|
runtime.Gosched()
|
|
|
|
runtime.Gosched()
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2010-09-10 22:37:20 +00:00
|
|
|
const maxTries = 10000 // Up to 100ms per test.
|
|
|
|
|
2008-07-16 21:41:27 +00:00
|
|
|
func main() {
|
2010-03-30 17:34:57 +00:00
|
|
|
var i32 int32
|
|
|
|
var i64 int64
|
|
|
|
var b bool
|
|
|
|
var s string
|
2008-07-16 21:41:27 +00:00
|
|
|
|
2010-03-30 17:34:57 +00:00
|
|
|
var sync = make(chan bool)
|
2009-01-24 01:04:56 +00:00
|
|
|
|
2008-09-26 18:47:04 +00:00
|
|
|
for buffer := 0; buffer < 2; buffer++ {
|
2010-03-30 17:34:57 +00:00
|
|
|
c32 := make(chan int32, buffer)
|
|
|
|
c64 := make(chan int64, buffer)
|
|
|
|
cb := make(chan bool, buffer)
|
|
|
|
cs := make(chan string, buffer)
|
|
|
|
|
2011-01-31 23:36:28 +00:00
|
|
|
select {
|
|
|
|
case i32 = <-c32:
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("blocked i32sender")
|
2011-01-31 23:36:28 +00:00
|
|
|
default:
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 23:36:28 +00:00
|
|
|
select {
|
|
|
|
case i64 = <-c64:
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("blocked i64sender")
|
2011-01-31 23:36:28 +00:00
|
|
|
default:
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 23:36:28 +00:00
|
|
|
select {
|
|
|
|
case b = <-cb:
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("blocked bsender")
|
2011-01-31 23:36:28 +00:00
|
|
|
default:
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 23:36:28 +00:00
|
|
|
select {
|
|
|
|
case s = <-cs:
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("blocked ssender")
|
2011-01-31 23:36:28 +00:00
|
|
|
default:
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go i32receiver(c32, sync)
|
2010-09-10 22:37:20 +00:00
|
|
|
try := 0
|
2011-01-31 23:36:28 +00:00
|
|
|
Send32:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c32 <- 123:
|
|
|
|
break Send32
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
println("i32receiver buffer=", buffer)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go i32sender(c32, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
Recv32:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case i32 = <-c32:
|
|
|
|
break Recv32
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
println("i32sender buffer=", buffer)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
if i32 != 234 {
|
|
|
|
panic("i32sender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go i64receiver(c64, sync)
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
Send64:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c64 <- 123456:
|
|
|
|
break Send64
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("i64receiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go i64sender(c64, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
Recv64:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case i64 = <-c64:
|
|
|
|
break Recv64
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("i64sender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
if i64 != 234567 {
|
|
|
|
panic("i64sender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go breceiver(cb, sync)
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
SendBool:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case cb <- true:
|
|
|
|
break SendBool
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("breceiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go bsender(cb, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
RecvBool:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b = <-cb:
|
|
|
|
break RecvBool
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("bsender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
if !b {
|
|
|
|
panic("bsender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go sreceiver(cs, sync)
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
SendString:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case cs <- "hello":
|
|
|
|
break SendString
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("sreceiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go ssender(cs, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 22:37:20 +00:00
|
|
|
try = 0
|
2011-01-31 23:36:28 +00:00
|
|
|
RecvString:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case s = <-cs:
|
|
|
|
break RecvString
|
|
|
|
default:
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("ssender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-09-10 22:37:20 +00:00
|
|
|
}
|
2010-03-30 17:34:57 +00:00
|
|
|
}
|
|
|
|
if s != "hello again" {
|
|
|
|
panic("ssender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2008-09-26 18:47:04 +00:00
|
|
|
}
|
2008-07-16 21:41:27 +00:00
|
|
|
}
|