| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| package main |
|
|
| import "time" |
|
|
| const always = "function did not" |
| const never = "function did" |
|
|
| func unreachable() { |
| panic("control flow shouldn't reach here") |
| } |
|
|
| |
| func testPanic(signal string, f func()) { |
| defer func() { |
| s := never |
| if recover() != nil { |
| s = always |
| } |
| if s != signal { |
| panic(signal + " panic") |
| } |
| }() |
| f() |
| } |
|
|
| |
| func testBlock(signal string, f func()) { |
| c := make(chan string) |
| go func() { |
| f() |
| c <- never |
| }() |
| go func() { |
| if signal == never { |
| |
| time.Sleep(10 * time.Second) |
| } else { |
| |
| |
| time.Sleep(10 * time.Millisecond) |
| } |
| c <- always |
| }() |
| if <-c != signal { |
| panic(signal + " block") |
| } |
| } |
|
|
| func main() { |
| const async = 1 |
| var nilch chan int |
| closedch := make(chan int) |
| close(closedch) |
|
|
| |
| testBlock(always, func() { |
| nilch <- 7 |
| }) |
| testBlock(always, func() { |
| <-nilch |
| }) |
|
|
| |
| testPanic(never, func() { |
| select { |
| case nilch <- 7: |
| unreachable() |
| default: |
| } |
| }) |
| testPanic(never, func() { |
| select { |
| case <-nilch: |
| unreachable() |
| default: |
| } |
| }) |
|
|
| |
| testBlock(never, func() { |
| ch := make(chan int, async) |
| ch <- 7 |
| }) |
|
|
| |
| testBlock(never, func() { |
| for i := 0; i < 10; i++ { |
| if <-closedch != 0 { |
| panic("expected zero value when reading from closed channel") |
| } |
| if x, ok := <-closedch; x != 0 || ok { |
| println("closedch:", x, ok) |
| panic("expected 0, false from closed channel") |
| } |
| } |
| }) |
|
|
| |
| testPanic(always, func() { |
| closedch <- 7 |
| }) |
|
|
| |
| testBlock(always, func() { |
| ch := make(chan int) |
| <-ch |
| }) |
|
|
| |
| testBlock(always, func() { |
| select {} |
| }) |
|
|
| |
| testBlock(always, func() { |
| select { |
| case <-nilch: |
| unreachable() |
| } |
| }) |
| testBlock(always, func() { |
| select { |
| case nilch <- 7: |
| unreachable() |
| } |
| }) |
| testBlock(always, func() { |
| select { |
| case <-nilch: |
| unreachable() |
| case nilch <- 7: |
| unreachable() |
| } |
| }) |
|
|
| |
| testBlock(always, func() { |
| ch := make(chan int) |
| select { |
| case <-ch: |
| unreachable() |
| } |
| }) |
|
|
| |
| testBlock(never, func() { |
| select { |
| default: |
| } |
| }) |
| testBlock(never, func() { |
| select { |
| case <-nilch: |
| unreachable() |
| default: |
| } |
| }) |
| testBlock(never, func() { |
| select { |
| case nilch <- 7: |
| unreachable() |
| default: |
| } |
| }) |
|
|
| |
| testBlock(never, func() { |
| ch := make(chan int, async) |
| select { |
| case ch <- 7: |
| default: |
| unreachable() |
| } |
| }) |
| testBlock(never, func() { |
| ch := make(chan int, async) |
| ch <- 7 |
| select { |
| case <-ch: |
| default: |
| unreachable() |
| } |
| }) |
|
|
| |
| testBlock(never, func() { |
| select { |
| case <-closedch: |
| } |
| }) |
| testBlock(never, func() { |
| select { |
| case x := (<-closedch): |
| _ = x |
| } |
| }) |
| testBlock(never, func() { |
| select { |
| case x, ok := (<-closedch): |
| _, _ = x, ok |
| } |
| }) |
| testPanic(always, func() { |
| select { |
| case closedch <- 7: |
| } |
| }) |
|
|
| |
| testBlock(always, func() { |
| c := make(chan int) |
| select { |
| case c <- 1: |
| case <-c: |
| } |
| }) |
| } |
|
|