github.com/lingyao2333/mo-zero@v1.4.1/core/syncx/barrier_test.go (about)

     1  package syncx
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestBarrier_Guard(t *testing.T) {
    11  	const total = 10000
    12  	var barrier Barrier
    13  	var count int
    14  	var wg sync.WaitGroup
    15  	wg.Add(total)
    16  	for i := 0; i < total; i++ {
    17  		go barrier.Guard(func() {
    18  			count++
    19  			wg.Done()
    20  		})
    21  	}
    22  	wg.Wait()
    23  	assert.Equal(t, total, count)
    24  }
    25  
    26  func TestBarrierPtr_Guard(t *testing.T) {
    27  	const total = 10000
    28  	barrier := new(Barrier)
    29  	var count int
    30  	wg := new(sync.WaitGroup)
    31  	wg.Add(total)
    32  	for i := 0; i < total; i++ {
    33  		go barrier.Guard(func() {
    34  			count++
    35  			wg.Done()
    36  		})
    37  	}
    38  	wg.Wait()
    39  	assert.Equal(t, total, count)
    40  }
    41  
    42  func TestGuard(t *testing.T) {
    43  	const total = 10000
    44  	var count int
    45  	var lock sync.Mutex
    46  	wg := new(sync.WaitGroup)
    47  	wg.Add(total)
    48  	for i := 0; i < total; i++ {
    49  		go Guard(&lock, func() {
    50  			count++
    51  			wg.Done()
    52  		})
    53  	}
    54  	wg.Wait()
    55  	assert.Equal(t, total, count)
    56  }