This repository has been archived on 2024-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
goutil/sync/select_test.go

25 lines
613 B
Go
Raw Normal View History

2024-12-02 12:34:39 -07:00
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) }
}