gitee.com/gricks/utils@v1.0.8/parallel_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  )
     9  
    10  func Test_Parallel(t *testing.T) {
    11  	Convey("Parallel", t, func() {
    12  		n := int32(1000)
    13  		c := 5
    14  		p := NewParallel(c)
    15  
    16  		vc := int32(0)
    17  		vn := int32(0)
    18  		for i := int32(1); i <= n; i++ {
    19  			p.Add(1)
    20  			go func(idx int32) {
    21  				defer p.Done()
    22  				atomic.AddInt32(&vn, 1)
    23  				atomic.AddInt32(&vc, idx)
    24  			}(i)
    25  		}
    26  		p.Wait()
    27  
    28  		So(vc, ShouldEqual, ((1+n)*n)/2)
    29  		So(vn, ShouldEqual, n)
    30  	})
    31  }