1
0
mirror of https://github.com/golang/go synced 2024-11-25 07:07:57 -07:00

test: Reduce race conditions in chan/nonblock.go.

nonblock.go wants to test nonblocking operations on
synchronous channels, so it is inherently racy.  This
introduces loops to make the race conditions much more likely
to succeed when using gccgo.

R=r
CC=golang-dev
https://golang.org/cl/2161043
This commit is contained in:
Ian Lance Taylor 2010-09-10 15:37:20 -07:00
parent 5011c27018
commit 8e985dcda6

View File

@ -69,6 +69,8 @@ func sleep() {
runtime.Gosched() runtime.Gosched()
} }
const maxTries = 10000 // Up to 100ms per test.
func main() { func main() {
var i32 int32 var i32 int32
var i64 int64 var i64 int64
@ -105,25 +107,30 @@ func main() {
} }
go i32receiver(c32, sync) go i32receiver(c32, sync)
sleep() try := 0
ok = c32 <- 123 for !(c32 <- 123) {
if !ok { try++
if try > maxTries {
println("i32receiver buffer=", buffer) println("i32receiver buffer=", buffer)
panic("fail") panic("fail")
} }
sleep()
}
<-sync <-sync
go i32sender(c32, sync) go i32sender(c32, sync)
if buffer > 0 { if buffer > 0 {
<-sync <-sync
} else {
sleep()
} }
i32, ok = <-c32 try = 0
if !ok { for i32, ok = <-c32; !ok; i32, ok = <-c32 {
try++
if try > maxTries {
println("i32sender buffer=", buffer) println("i32sender buffer=", buffer)
panic("fail") panic("fail")
} }
sleep()
}
if i32 != 234 { if i32 != 234 {
panic("i32sender value") panic("i32sender value")
} }
@ -132,23 +139,28 @@ func main() {
} }
go i64receiver(c64, sync) go i64receiver(c64, sync)
sleep() try = 0
ok = c64 <- 123456 for !(c64 <- 123456) {
if !ok { try++
if try > maxTries {
panic("i64receiver") panic("i64receiver")
} }
sleep()
}
<-sync <-sync
go i64sender(c64, sync) go i64sender(c64, sync)
if buffer > 0 { if buffer > 0 {
<-sync <-sync
} else {
sleep()
} }
i64, ok = <-c64 try = 0
if !ok { for i64, ok = <-c64; !ok; i64, ok = <-c64 {
try++
if try > maxTries {
panic("i64sender") panic("i64sender")
} }
sleep()
}
if i64 != 234567 { if i64 != 234567 {
panic("i64sender value") panic("i64sender value")
} }
@ -157,23 +169,28 @@ func main() {
} }
go breceiver(cb, sync) go breceiver(cb, sync)
sleep() try = 0
ok = cb <- true for !(cb <- true) {
if !ok { try++
if try > maxTries {
panic("breceiver") panic("breceiver")
} }
sleep()
}
<-sync <-sync
go bsender(cb, sync) go bsender(cb, sync)
if buffer > 0 { if buffer > 0 {
<-sync <-sync
} else {
sleep()
} }
b, ok = <-cb try = 0
if !ok { for b, ok = <-cb; !ok; b, ok = <-cb {
try++
if try > maxTries {
panic("bsender") panic("bsender")
} }
sleep()
}
if !b { if !b {
panic("bsender value") panic("bsender value")
} }
@ -182,23 +199,28 @@ func main() {
} }
go sreceiver(cs, sync) go sreceiver(cs, sync)
sleep() try = 0
ok = cs <- "hello" for !(cs <- "hello") {
if !ok { try++
if try > maxTries {
panic("sreceiver") panic("sreceiver")
} }
sleep()
}
<-sync <-sync
go ssender(cs, sync) go ssender(cs, sync)
if buffer > 0 { if buffer > 0 {
<-sync <-sync
} else {
sleep()
} }
s, ok = <-cs try = 0
if !ok { for s, ok = <-cs; !ok; s, ok = <-cs {
try++
if try > maxTries {
panic("ssender") panic("ssender")
} }
sleep()
}
if s != "hello again" { if s != "hello again" {
panic("ssender value") panic("ssender value")
} }