github.com/mailru/activerecord@v1.12.2/pkg/iproto/syncutil/throttle_test.go (about)

     1  package syncutil
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"sync/atomic"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestThrottleDo(t *testing.T) {
    12  	t.Skip("Fails on centos")
    13  	th := NewThrottle(time.Millisecond * 50)
    14  
    15  	var (
    16  		wg sync.WaitGroup
    17  		n  int32
    18  	)
    19  	for i := 0; i < runtime.NumCPU(); i++ {
    20  		wg.Add(1)
    21  		go func() {
    22  			defer wg.Done()
    23  			tick := time.Tick(time.Millisecond * 5)
    24  			for j := 0; j < 100; j++ {
    25  				<-tick
    26  				if th.Next() {
    27  					atomic.AddInt32(&n, 1)
    28  				}
    29  			}
    30  		}()
    31  	}
    32  	wg.Wait()
    33  
    34  	if act, exp := int(atomic.LoadInt32(&n)), 10; act != exp {
    35  		t.Errorf("got %d truly Next(); want %d", act, exp)
    36  	}
    37  }
    38  
    39  func BenchmarkThrottleDo(b *testing.B) {
    40  	th := NewThrottle(time.Millisecond * 10)
    41  	b.RunParallel(func(pb *testing.PB) {
    42  		for pb.Next() {
    43  			th.Next()
    44  		}
    45  	})
    46  }