Skip to content

Commit

Permalink
fix(test): revert to previous rand.Seed
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Feb 27, 2025
1 parent d6ad120 commit 85ca9fc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
13 changes: 5 additions & 8 deletions examples/clickhouse_api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ package clickhouse_api
import (
"context"
"fmt"
"math/rand"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
"os"
"strconv"
"testing"
"time"

clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
fmt.Printf("using random seed %d for %s tests\n", seed, TestSet)
rand.Seed(seed)
ResetRandSeed()
fmt.Printf("using random seed %d for %s tests\n", randSeed, TestSet)

useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
Expand Down
4 changes: 3 additions & 1 deletion examples/clickhouse_api/multi_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package clickhouse_api

import (
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
"math/rand"

"github.com/ClickHouse/clickhouse-go/v2"
)

func MultiHostVersion() error {
Expand All @@ -34,6 +35,7 @@ func MultiHostRoundRobinVersion() error {

func MultiHostRandomVersion() error {
rand.Seed(85206178671753423)
defer ResetRandSeed()
connOpenStrategy := clickhouse.ConnOpenRandom
return multiHostVersion(&connOpenStrategy)
}
Expand Down
8 changes: 8 additions & 0 deletions examples/clickhouse_api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"math/rand"
"time"
)

const TestSet string = "examples_clickhouse_api"
Expand All @@ -41,3 +43,9 @@ func GetNativeConnectionWithOptions(settings clickhouse.Settings, tlsConfig *tls
func CheckMinServerVersion(conn driver.Conn, major, minor, patch uint64) bool {
return clickhouse_tests.CheckMinServerServerVersion(conn, major, minor, patch)
}

var randSeed = time.Now().UnixNano()

func ResetRandSeed() {
rand.Seed(randSeed)
}
2 changes: 1 addition & 1 deletion tests/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func TestConnFailoverRoundRobin(t *testing.T) {
}

func TestConnFailoverRandom(t *testing.T) {
// TODO: find better way to make test deterministic
rand.Seed(85206178671753423)
defer ResetRandSeed()
testConnFailover(t, clickhouse.ConnOpenRandom)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/std/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestStdConnFailoverRoundRobin(t *testing.T) {
}

func TestStdConnFailoverRandom(t *testing.T) {
// TODO: find better way to make test deterministic
rand.Seed(85206178671753423)
defer clickhouse_tests.ResetRandSeed()
testStdConnFailover(t, "random")
}

Expand Down
10 changes: 7 additions & 3 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (

var testUUID = uuid.NewString()[0:12]
var testTimestamp = time.Now().UnixMilli()
var randSeed = time.Now().UnixNano()

const defaultClickHouseVersion = "latest"

Expand Down Expand Up @@ -787,10 +788,13 @@ func OptionsToDSN(o *clickhouse.Options) string {
return u.String()
}

func ResetRandSeed() {
rand.Seed(randSeed)
}

func Runtime(m *testing.M, ts string) (exitCode int) {
seed := time.Now().UnixNano()
rand.Seed(seed)
fmt.Printf("using random seed %d for %s tests\n", seed, ts)
ResetRandSeed()
fmt.Printf("using random seed %d for %s tests\n", randSeed, ts)

useDocker, err := strconv.ParseBool(GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
Expand Down

0 comments on commit 85ca9fc

Please sign in to comment.