github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/functicker/ticker_test.go (about)

     1  package functicker
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestTicker(t *testing.T) {
    10  	var count atomic.Int32
    11  	ticker := New(100*time.Millisecond, func() {
    12  		count.Add(1)
    13  		t.Log("functicker.Ticker tick")
    14  	})
    15  	time.Sleep(time.Second)
    16  
    17  	ticker.Reset(200 * time.Millisecond)
    18  	time.Sleep(time.Second)
    19  
    20  	ticker.Stop()
    21  	time.Sleep(time.Second)
    22  
    23  	if n := count.Load(); !(n > 10 && n <= 15) {
    24  		t.Errorf("got unexpected count %d", n)
    25  	}
    26  }