File: batch_limiter_test.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (30 lines) | stat: -rw-r--r-- 643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package toolbox_test

import (
	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
	"testing"
)

func TestBatchLimiter(t *testing.T) {
	var numbers = []int{1, 4, 6, 2, 5, 7, 5, 5, 7, 2, 3, 5, 7, 6}
	limiter := toolbox.NewBatchLimiter(4, len(numbers))
	var sum int32 = 0
	for _, n := range numbers {
		go func(n int32) {
			limiter.Acquire()
			defer limiter.Done()
			limiter.Mutex.Lock()
			defer limiter.Mutex.Unlock()
			//atomic.AddInt32(&sum, int32(n))
			sum = sum + int32(n)
		}(int32(n))

	}
	limiter.Wait()
	var expected int32 = 0
	for _, n := range numbers {
		expected += int32(n)
	}
	assert.Equal(t, expected, sum)
}