2008-07-16 15:41:27 -06:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Verify channel operations that test for blocking
|
|
|
|
// Use several sizes and types of operands
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
import "runtime"
|
2009-01-23 18:04:56 -07:00
|
|
|
import "time"
|
2008-07-16 15:41:27 -06:00
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func i32receiver(c chan int32, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
if <-c != 123 {
|
|
|
|
panic("i32 value")
|
|
|
|
}
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func i32sender(c chan int32, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
c <- 234
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func i64receiver(c chan int64, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
if <-c != 123456 {
|
|
|
|
panic("i64 value")
|
|
|
|
}
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func i64sender(c chan int64, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
c <- 234567
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func breceiver(c chan bool, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
if !<-c {
|
|
|
|
panic("b value")
|
|
|
|
}
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func bsender(c chan bool, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
c <- true
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-23 18:04:56 -07:00
|
|
|
func sreceiver(c chan string, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
if <-c != "hello" {
|
|
|
|
panic("s value")
|
|
|
|
}
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ssender(c chan string, strobe chan bool) {
|
2010-03-30 11:34:57 -06:00
|
|
|
c <- "hello again"
|
2009-01-23 18:04:56 -07:00
|
|
|
strobe <- true
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
var ticker = time.Tick(10 * 1000) // 10 us
|
2009-01-23 18:04:56 -07:00
|
|
|
func sleep() {
|
2010-03-30 11:34:57 -06:00
|
|
|
<-ticker
|
|
|
|
<-ticker
|
|
|
|
runtime.Gosched()
|
|
|
|
runtime.Gosched()
|
|
|
|
runtime.Gosched()
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|
|
|
|
|
2010-09-10 16:37:20 -06:00
|
|
|
const maxTries = 10000 // Up to 100ms per test.
|
|
|
|
|
2008-07-16 15:41:27 -06:00
|
|
|
func main() {
|
2010-03-30 11:34:57 -06:00
|
|
|
var i32 int32
|
|
|
|
var i64 int64
|
|
|
|
var b bool
|
|
|
|
var s string
|
|
|
|
var ok bool
|
2008-07-16 15:41:27 -06:00
|
|
|
|
2010-03-30 11:34:57 -06:00
|
|
|
var sync = make(chan bool)
|
2009-01-23 18:04:56 -07:00
|
|
|
|
2008-09-26 12:47:04 -06:00
|
|
|
for buffer := 0; buffer < 2; buffer++ {
|
2010-03-30 11:34:57 -06:00
|
|
|
c32 := make(chan int32, buffer)
|
|
|
|
c64 := make(chan int64, buffer)
|
|
|
|
cb := make(chan bool, buffer)
|
|
|
|
cs := make(chan string, buffer)
|
|
|
|
|
|
|
|
i32, ok = <-c32
|
|
|
|
if ok {
|
|
|
|
panic("blocked i32sender")
|
|
|
|
}
|
|
|
|
|
|
|
|
i64, ok = <-c64
|
|
|
|
if ok {
|
|
|
|
panic("blocked i64sender")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok = <-cb
|
|
|
|
if ok {
|
|
|
|
panic("blocked bsender")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok = <-cs
|
|
|
|
if ok {
|
|
|
|
panic("blocked ssender")
|
|
|
|
}
|
|
|
|
|
|
|
|
go i32receiver(c32, sync)
|
2010-09-10 16:37:20 -06:00
|
|
|
try := 0
|
|
|
|
for !(c32 <- 123) {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
println("i32receiver buffer=", buffer)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go i32sender(c32, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for i32, ok = <-c32; !ok; i32, ok = <-c32 {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
println("i32sender buffer=", buffer)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
if i32 != 234 {
|
|
|
|
panic("i32sender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go i64receiver(c64, sync)
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for !(c64 <- 123456) {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("i64receiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go i64sender(c64, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for i64, ok = <-c64; !ok; i64, ok = <-c64 {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("i64sender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
if i64 != 234567 {
|
|
|
|
panic("i64sender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go breceiver(cb, sync)
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for !(cb <- true) {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("breceiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go bsender(cb, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for b, ok = <-cb; !ok; b, ok = <-cb {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("bsender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
if !b {
|
|
|
|
panic("bsender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
|
|
|
|
|
|
|
go sreceiver(cs, sync)
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for !(cs <- "hello") {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("sreceiver")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
<-sync
|
|
|
|
|
|
|
|
go ssender(cs, sync)
|
|
|
|
if buffer > 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2010-09-10 16:37:20 -06:00
|
|
|
try = 0
|
|
|
|
for s, ok = <-cs; !ok; s, ok = <-cs {
|
|
|
|
try++
|
|
|
|
if try > maxTries {
|
|
|
|
panic("ssender")
|
|
|
|
}
|
|
|
|
sleep()
|
2010-03-30 11:34:57 -06:00
|
|
|
}
|
|
|
|
if s != "hello again" {
|
|
|
|
panic("ssender value")
|
|
|
|
}
|
|
|
|
if buffer == 0 {
|
|
|
|
<-sync
|
|
|
|
}
|
2008-09-26 12:47:04 -06:00
|
|
|
}
|
2008-08-11 23:07:49 -06:00
|
|
|
print("PASS\n")
|
2008-07-16 15:41:27 -06:00
|
|
|
}
|