github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/util/sync_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestBlockRunner(t *testing.T) {
     9  	r := &BlockRunner{}
    10  
    11  	r.Init(2)
    12  
    13  	ch := make(chan int, 2)
    14  	for i := 0; i < 2; i++ {
    15  		go func(i int) {
    16  			// Can only initialize once
    17  			r.Init(3)
    18  			r.Run(func() {
    19  				t.Logf("block run %d", i+1)
    20  				time.Sleep(time.Second)
    21  			})
    22  			ch <- i
    23  		}(i)
    24  	}
    25  
    26  	select {
    27  	case <-ch:
    28  		t.Fatal("can't get data at this time")
    29  	case <-time.After(100 * time.Millisecond):
    30  	}
    31  
    32  	for i := 0; i < 2; i++ {
    33  		select {
    34  		case <-ch:
    35  		case <-time.After(5 * time.Second):
    36  			t.Fatal("can't wait ")
    37  		}
    38  	}
    39  }