github.com/netdata/go.d.plugin@v0.58.1/modules/vsphere/scrape/throttled_caller_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package scrape 4 5 import ( 6 "sync" 7 "sync/atomic" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Test_throttledCaller(t *testing.T) { 15 var current int64 16 var max int64 17 var total int64 18 var mux sync.Mutex 19 limit := 5 20 n := 10000 21 tc := newThrottledCaller(limit) 22 23 for i := 0; i < n; i++ { 24 job := func() { 25 atomic.AddInt64(&total, 1) 26 atomic.AddInt64(¤t, 1) 27 time.Sleep(100 * time.Microsecond) 28 29 mux.Lock() 30 defer mux.Unlock() 31 if atomic.LoadInt64(¤t) > max { 32 max = atomic.LoadInt64(¤t) 33 } 34 atomic.AddInt64(¤t, -1) 35 } 36 tc.call(job) 37 } 38 tc.wait() 39 40 assert.Equal(t, int64(n), total) 41 assert.Equal(t, max, int64(limit)) 42 }