1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:54:40 -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,24 +107,29 @@ func main() {
} }
go i32receiver(c32, sync) go i32receiver(c32, sync)
sleep() try := 0
ok = c32 <- 123 for !(c32 <- 123) {
if !ok { try++
println("i32receiver buffer=", buffer) if try > maxTries {
panic("fail") println("i32receiver buffer=", buffer)
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 {
println("i32sender buffer=", buffer) try++
panic("fail") if try > maxTries {
println("i32sender buffer=", buffer)
panic("fail")
}
sleep()
} }
if i32 != 234 { if i32 != 234 {
panic("i32sender value") panic("i32sender value")
@ -132,22 +139,27 @@ func main() {
} }
go i64receiver(c64, sync) go i64receiver(c64, sync)
sleep() try = 0
ok = c64 <- 123456 for !(c64 <- 123456) {
if !ok { try++
panic("i64receiver") if try > maxTries {
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 {
panic("i64sender") try++
if try > maxTries {
panic("i64sender")
}
sleep()
} }
if i64 != 234567 { if i64 != 234567 {
panic("i64sender value") panic("i64sender value")
@ -157,22 +169,27 @@ func main() {
} }
go breceiver(cb, sync) go breceiver(cb, sync)
sleep() try = 0
ok = cb <- true for !(cb <- true) {
if !ok { try++
panic("breceiver") if try > maxTries {
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 {
panic("bsender") try++
if try > maxTries {
panic("bsender")
}
sleep()
} }
if !b { if !b {
panic("bsender value") panic("bsender value")
@ -182,22 +199,27 @@ func main() {
} }
go sreceiver(cs, sync) go sreceiver(cs, sync)
sleep() try = 0
ok = cs <- "hello" for !(cs <- "hello") {
if !ok { try++
panic("sreceiver") if try > maxTries {
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 {
panic("ssender") try++
if try > maxTries {
panic("ssender")
}
sleep()
} }
if s != "hello again" { if s != "hello again" {
panic("ssender value") panic("ssender value")