github.com/annchain/OG@v0.0.9/tests/testChan/limit_test.go (about) 1 package testChan 2 3 import ( 4 "fmt" 5 "runtime" 6 "testing" 7 "time" 8 ) 9 10 func TestLimit(t *testing.T) { 11 a := app{ 12 ch: make(chan int, 3), 13 quit: make(chan bool), 14 } 15 //go a.loop() 16 17 for i := 0; i < 10; i++ { 18 fmt.Println("before write ", i, time.Now()) 19 a.process(i) 20 fmt.Println("after write ", i, time.Now()) 21 } 22 23 } 24 25 type app struct { 26 ch chan int 27 quit chan bool 28 } 29 30 func (a *app) loop() { 31 32 for { 33 select { 34 case i := <-a.ch: 35 fmt.Println("start ", i, time.Now()) 36 time.Sleep(time.Millisecond * 500) 37 fmt.Println("stop ", i, time.Now()) 38 case <-a.quit: 39 fmt.Println("exit") 40 return 41 42 } 43 } 44 } 45 46 func (a *app) process(i int) { 47 a.ch <- i 48 fmt.Println(runtime.NumGoroutine(), "goroutine num") 49 go func() { 50 fmt.Println("start ", i, time.Now()) 51 time.Sleep(time.Millisecond * 1000) 52 fmt.Println("stop ", i, time.Now()) 53 <-a.ch 54 }() 55 56 }