summaryrefslogtreecommitdiff
path: root/test/closedchan.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-01-31 18:36:28 -0500
committerRuss Cox <rsc@golang.org>2011-01-31 18:36:28 -0500
commitf9f5975990179a1d9b40965dcb7f5e992fe8043c (patch)
tree1c2455376a52b1520a6dd4eb45ac2e5dd34b3b6e /test/closedchan.go
parent95fa0b246b3c7b0a57ef0ba20cca22b500ed3ade (diff)
downloadgo-f9f5975990179a1d9b40965dcb7f5e992fe8043c.tar.gz
replace non-blocking send, receive syntax with select
R=golang-dev, nigeltao, niemeyer, r CC=golang-dev http://codereview.appspot.com/4079053
Diffstat (limited to 'test/closedchan.go')
-rw-r--r--test/closedchan.go92
1 files changed, 82 insertions, 10 deletions
diff --git a/test/closedchan.go b/test/closedchan.go
index 8126d5a4e..46d9d0f5d 100644
--- a/test/closedchan.go
+++ b/test/closedchan.go
@@ -21,14 +21,21 @@ type Chan interface {
Impl() string
}
-// direct channel operations
+// direct channel operations when possible
type XChan chan int
+
func (c XChan) Send(x int) {
c <- x
}
func (c XChan) Nbsend(x int) bool {
- return c <- x
+ select {
+ case c <- x:
+ return true
+ default:
+ return false
+ }
+ panic("nbsend")
}
func (c XChan) Recv() int {
@@ -36,8 +43,13 @@ func (c XChan) Recv() int {
}
func (c XChan) Nbrecv() (int, bool) {
- x, ok := <-c
- return x, ok
+ select {
+ case x := <-c:
+ return x, true
+ default:
+ return 0, false
+ }
+ panic("nbrecv")
}
func (c XChan) Close() {
@@ -54,6 +66,7 @@ func (c XChan) Impl() string {
// indirect operations via select
type SChan chan int
+
func (c SChan) Send(x int) {
select {
case c <- x:
@@ -62,10 +75,10 @@ func (c SChan) Send(x int) {
func (c SChan) Nbsend(x int) bool {
select {
- case c <- x:
- return true
default:
return false
+ case c <- x:
+ return true
}
panic("nbsend")
}
@@ -80,10 +93,10 @@ func (c SChan) Recv() int {
func (c SChan) Nbrecv() (int, bool) {
select {
- case x := <-c:
- return x, true
default:
return 0, false
+ case x := <-c:
+ return x, true
}
panic("nbrecv")
}
@@ -100,6 +113,62 @@ func (c SChan) Impl() string {
return "(select)"
}
+// indirect operations via larger selects
+var dummy = make(chan bool)
+
+type SSChan chan int
+
+func (c SSChan) Send(x int) {
+ select {
+ case c <- x:
+ case <-dummy:
+ }
+}
+
+func (c SSChan) Nbsend(x int) bool {
+ select {
+ default:
+ return false
+ case <-dummy:
+ case c <- x:
+ return true
+ }
+ panic("nbsend")
+}
+
+func (c SSChan) Recv() int {
+ select {
+ case <-dummy:
+ case x := <-c:
+ return x
+ }
+ panic("recv")
+}
+
+func (c SSChan) Nbrecv() (int, bool) {
+ select {
+ case <-dummy:
+ default:
+ return 0, false
+ case x := <-c:
+ return x, true
+ }
+ panic("nbrecv")
+}
+
+func (c SSChan) Close() {
+ close(c)
+}
+
+func (c SSChan) Closed() bool {
+ return closed(c)
+}
+
+func (c SSChan) Impl() string {
+ return "(select)"
+}
+
+
func shouldPanic(f func()) {
defer func() {
if recover() == nil {
@@ -137,7 +206,7 @@ func test1(c Chan) {
}
// send should work with ,ok too: sent a value without blocking, so ok == true.
- shouldPanic(func(){c.Nbsend(1)})
+ shouldPanic(func() { c.Nbsend(1) })
// the value should have been discarded.
if x := c.Recv(); x != 0 {
@@ -145,7 +214,7 @@ func test1(c Chan) {
}
// similarly Send.
- shouldPanic(func(){c.Send(2)})
+ shouldPanic(func() { c.Send(2) })
if x := c.Recv(); x != 0 {
println("test1: recv on closed got non-zero after send on closed:", x, c.Impl())
}
@@ -195,9 +264,12 @@ func closedasync() chan int {
func main() {
test1(XChan(closedsync()))
test1(SChan(closedsync()))
+ test1(SSChan(closedsync()))
testasync1(XChan(closedasync()))
testasync1(SChan(closedasync()))
+ testasync1(SSChan(closedasync()))
testasync2(XChan(closedasync()))
testasync2(SChan(closedasync()))
+ testasync2(SSChan(closedasync()))
}