github.com/aergoio/aergo@v1.3.1/p2p/p2putil/chan_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2putil
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestClosedChannel(t *testing.T) {
    14  	ca := make(chan int, 10)
    15  	cb := make(chan int)
    16  
    17  	go func(size int) {
    18  		for i:=0; i<size; i++ {
    19  			ca <- i
    20  		}
    21  	}(100)
    22  	close(cb)
    23  	LOOP:
    24  	for {
    25  		select {
    26  		case v := <-ca:
    27  			t.Log("got val ", v)
    28  			time.Sleep(time.Millisecond<<2)
    29  		case <-cb:
    30  			t.Log("closed")
    31  			break LOOP
    32  		}
    33  	}
    34  }