github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/test/chan/select.go (about) 1 // run 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test simple select. 8 9 package main 10 11 var counter uint 12 var shift uint 13 14 func GetValue() uint { 15 counter++ 16 return 1 << shift 17 } 18 19 func Send(a, b chan uint) int { 20 var i int 21 22 LOOP: 23 for { 24 select { 25 case a <- GetValue(): 26 i++ 27 a = nil 28 case b <- GetValue(): 29 i++ 30 b = nil 31 default: 32 break LOOP 33 } 34 shift++ 35 } 36 return i 37 } 38 39 func main() { 40 a := make(chan uint, 1) 41 b := make(chan uint, 1) 42 if v := Send(a, b); v != 2 { 43 println("Send returned", v, "!= 2") 44 panic("fail") 45 } 46 if av, bv := <-a, <-b; av|bv != 3 { 47 println("bad values", av, bv) 48 panic("fail") 49 } 50 if v := Send(a, nil); v != 1 { 51 println("Send returned", v, "!= 1") 52 panic("fail") 53 } 54 if counter != 10 { 55 println("counter is", counter, "!= 10") 56 panic("fail") 57 } 58 }