aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-07-14 13:18:57 -0700
committerRobert Griesemer <gri@golang.org>2010-07-14 13:18:57 -0700
commit47b835e4a8e1b4792eb3698d78751fe213336690 (patch)
tree98a9fbaa1d3d8817e6c09a0438cb72ec91dad921
parenta3855235cd493af6d67e232136b8f5e3f86de183 (diff)
downloadgo-47b835e4a8e1b4792eb3698d78751fe213336690.tar.gz
go-47b835e4a8e1b4792eb3698d78751fe213336690.zip
channel tests: added a couple of tests with closed channels
R=rsc CC=golang-dev https://golang.org/cl/1774047
-rw-r--r--test/chan/select3.go34
1 files changed, 33 insertions, 1 deletions
diff --git a/test/chan/select3.go b/test/chan/select3.go
index f429f9e8f9..d4f7ebcec0 100644
--- a/test/chan/select3.go
+++ b/test/chan/select3.go
@@ -25,7 +25,7 @@ func testPanic(signal string, f func()) {
defer func() {
s := never
if recover() != nil {
- s = always // f panicked
+ s = always // f panicked
}
if s != signal {
panic(signal + " panic")
@@ -55,6 +55,8 @@ func testBlock(signal string, f func()) {
func main() {
const async = 1 // asynchronous channels
var nilch chan int
+ closedch := make(chan int)
+ close(closedch)
// sending/receiving from a nil channel outside a select panics
testPanic(always, func() {
@@ -86,6 +88,24 @@ func main() {
ch <- 7
})
+ // receiving (a small number of times) from a closed channel never blocks
+ testBlock(never, func() {
+ for i := 0; i < 10; i++ {
+ if <-closedch != 0 {
+ panic("expected zero value when reading from closed channel")
+ }
+ }
+ })
+
+ // sending (a small number of times) to a closed channel is not specified
+ // but the current implementation doesn't block: test that different
+ // implementations behave the same
+ testBlock(never, func() {
+ for i := 0; i < 10; i++ {
+ closedch <- 7
+ }
+ })
+
// receiving from a non-ready channel always blocks
testBlock(always, func() {
ch := make(chan int)
@@ -173,4 +193,16 @@ func main() {
unreachable()
}
})
+
+ // selects with closed channels don't block
+ testBlock(never, func() {
+ select {
+ case <-closedch:
+ }
+ })
+ testBlock(never, func() {
+ select {
+ case closedch <- 7:
+ }
+ })
}