25 lines
613 B
Go
25 lines
613 B
Go
|
package usync
|
||
|
|
||
|
import "time"
|
||
|
import "testing"
|
||
|
import "context"
|
||
|
|
||
|
func TestSelect (test *testing.T) {
|
||
|
// https://stackoverflow.com/questions/19992334
|
||
|
c1 := make(chan int)
|
||
|
c2 := make(chan int)
|
||
|
c3 := make(chan int)
|
||
|
chs := []chan int { c1, c2, c3 }
|
||
|
go func () {
|
||
|
time.Sleep(time.Second)
|
||
|
c2 <- 42
|
||
|
} ()
|
||
|
ctx, done := context.WithTimeout(context.Background(), 5 * time.Second)
|
||
|
defer done()
|
||
|
|
||
|
chosen, val, ok := Select(ctx, chs...)
|
||
|
if !ok { test.Fatal("not ok") }
|
||
|
if 1 != chosen { test.Fatal("expected 1, got", chosen) }
|
||
|
if 42 != val { test.Fatal("expected 42, got", val) }
|
||
|
}
|