github.com/pubgo/xprocess@v0.1.11/xprocess_waitgroup/waitgroup_test.go (about)

     1  package xprocess_waitgroup
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func handleTime(wg *WaitGroup, i int) {
    10  	defer wg.Done()
    11  	time.Sleep(time.Millisecond * time.Duration(i) * 5)
    12  	fmt.Println("ok")
    13  }
    14  
    15  func TestWaitGroup(t *testing.T) {
    16  	var wg WaitGroup
    17  
    18  	for i := 100; i > 0; i-- {
    19  		wg.Inc()
    20  		i := i
    21  		go handleTime(&wg, i)
    22  	}
    23  
    24  	wg.Wait()
    25  }
    26  
    27  func TestWaitGroupWithCheck(t *testing.T) {
    28  	// enable checked
    29  	var wg = WaitGroup{Check: true}
    30  
    31  	for i := 100; i > 0; i-- {
    32  		wg.Inc()
    33  		i := i
    34  		go handleTime(&wg, i)
    35  	}
    36  
    37  	wg.Wait()
    38  }