github.com/viant/toolbox@v0.34.5/batch_limiter_test.go (about)

     1  package toolbox_test
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"github.com/viant/toolbox"
     6  	"testing"
     7  )
     8  
     9  func TestBatchLimiter(t *testing.T) {
    10  	var numbers = []int{1, 4, 6, 2, 5, 7, 5, 5, 7, 2, 3, 5, 7, 6}
    11  	limiter := toolbox.NewBatchLimiter(4, len(numbers))
    12  	var sum int32 = 0
    13  	for _, n := range numbers {
    14  		go func(n int32) {
    15  			limiter.Acquire()
    16  			defer limiter.Done()
    17  			limiter.Mutex.Lock()
    18  			defer limiter.Mutex.Unlock()
    19  			//atomic.AddInt32(&sum, int32(n))
    20  			sum = sum + int32(n)
    21  		}(int32(n))
    22  
    23  	}
    24  	limiter.Wait()
    25  	var expected int32 = 0
    26  	for _, n := range numbers {
    27  		expected += int32(n)
    28  	}
    29  	assert.Equal(t, expected, sum)
    30  }