Skip to content

Commit

Permalink
make Spec SEED more random
Browse files Browse the repository at this point in the history
When the go-test executes multiple packages, they run concurrently with each other.
If the seed is purely time based, then they could generate the same values across different instances,
which is not good when a shared external resource is involved in testing with t.Random values.
  • Loading branch information
adamluzsi committed Feb 16, 2023
1 parent d2edad7 commit 0e8e916
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package testcase

import (
"fmt"
"github.com/adamluzsi/testcase/random"
"math/rand"
"os"
"strconv"
"testing"
Expand All @@ -13,7 +15,9 @@ import (
func makeSeed() (int64, error) {
rawSeed, injectedRandomSeedIsSet := os.LookupEnv(EnvKeySeed)
if !injectedRandomSeedIsSet {
return time.Now().UnixNano(), nil
salt := rand.New(random.CryptoSeed{}).Int63()
base := time.Now().UnixNano()
return base + salt, nil
}
seed, err := strconv.ParseInt(rawSeed, 10, 64)
if err != nil {
Expand Down

0 comments on commit 0e8e916

Please sign in to comment.