github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/grail/biofs/biofseventlog/biofseventlog_test.go (about)

     1  package biofseventlog
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestCoarseTime(t *testing.T) {
    10  	const (
    11  		weeks = 10
    12  		N     = 10000
    13  	)
    14  	minMillis := coarseMillis(1600000000000) // Arbitrary time in 2020.
    15  	maxMillis := minMillis + 7*24*weeks*time.Hour.Milliseconds()
    16  
    17  	gotCoarseMillis := map[int64]struct{}{}
    18  	rnd := rand.New(rand.NewSource(1))
    19  	for i := 0; i < N; i++ {
    20  		fineMillis := minMillis + rnd.Int63n(maxMillis-minMillis)
    21  		gotCoarseMillis[coarseMillis(fineMillis)] = struct{}{}
    22  	}
    23  
    24  	if got := len(gotCoarseMillis); got != weeks {
    25  		t.Errorf("got %d, want %d", got, weeks)
    26  	}
    27  }
    28  
    29  func coarseMillis(millis int64) int64 {
    30  	return CoarseTime(time.UnixMilli(millis)).UnixMilli()
    31  }