github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/testutils/seed.go (about)

     1  package testutils
     2  
     3  import (
     4  	"math/rand"
     5  	"os"
     6  	"strconv"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  var randSeed struct {
    13  	value int64
    14  	once  sync.Once
    15  }
    16  
    17  func Rand(tb testing.TB) *rand.Rand {
    18  	randSeed.once.Do(func() {
    19  		randSeed.value = time.Now().UnixMicro()
    20  	})
    21  
    22  	seed := randSeed.value
    23  	if seedStr, ok := os.LookupEnv("TEST_SEED"); ok {
    24  		var err error
    25  		seed, err = strconv.ParseInt(seedStr, 0, 64)
    26  		if err != nil {
    27  			tb.Fatal("Parse TEST_SEED environment variable:", err)
    28  		}
    29  	}
    30  
    31  	tb.Logf("TEST_SEED=%d\n", seed)
    32  	return rand.New(rand.NewSource(seed))
    33  }