github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/bgticker_test.go (about)

     1  package libkb
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  const chWait = 5 * time.Second
    11  
    12  func TestBgTicker(t *testing.T) {
    13  	duration := 2 * time.Millisecond
    14  	wait := time.Millisecond
    15  	start := time.Now()
    16  	ticker := NewBgTickerWithWait(duration, wait)
    17  
    18  	// Test tick
    19  	for i := 0; i < 5; i++ {
    20  		select {
    21  		case <-ticker.C:
    22  			if i == 0 {
    23  				require.True(t, time.Since(start) >= wait, "time.Since(start) %v", time.Since(start))
    24  			}
    25  		case <-time.After(chWait):
    26  			require.Fail(t, "ticker did not fire")
    27  		}
    28  	}
    29  }