github.com/mailru/activerecord@v1.12.2/pkg/iproto/util/time/timer_test.go (about)

     1  package time
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestTimerPool(t *testing.T) {
    10  	for i := 0; i < 1000000; i++ {
    11  		if i%2 == 0 {
    12  			tm := AcquireTimer(0)
    13  			ReleaseTimer(tm)
    14  			continue
    15  		}
    16  
    17  		tm := AcquireTimer(time.Second)
    18  		select {
    19  		case <-tm.C:
    20  			t.Fatalf("unexpected timer event after %d iterations!", i)
    21  		default:
    22  			ReleaseTimer(tm)
    23  		}
    24  	}
    25  }
    26  
    27  func BenchmarkTimerPool(b *testing.B) {
    28  	b.SetParallelism(1024)
    29  	b.RunParallel(func(pb *testing.PB) {
    30  		for pb.Next() {
    31  			tm := AcquireTimer(0)
    32  			runtime.Gosched()
    33  			ReleaseTimer(tm)
    34  		}
    35  	})
    36  }