Skip to content

Commit 85ca9fc

Browse files
committed
fix(test): revert to previous rand.Seed
1 parent d6ad120 commit 85ca9fc

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

examples/clickhouse_api/main_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ package clickhouse_api
2020
import (
2121
"context"
2222
"fmt"
23-
"math/rand"
23+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
24+
"github.com/stretchr/testify/require"
2425
"os"
2526
"strconv"
2627
"testing"
27-
"time"
28-
29-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
30-
"github.com/stretchr/testify/require"
3128
)
3229

3330
func TestMain(m *testing.M) {
34-
seed := time.Now().UnixNano()
35-
fmt.Printf("using random seed %d for %s tests\n", seed, TestSet)
36-
rand.Seed(seed)
31+
ResetRandSeed()
32+
fmt.Printf("using random seed %d for %s tests\n", randSeed, TestSet)
33+
3734
useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
3835
if err != nil {
3936
panic(err)

examples/clickhouse_api/multi_host.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ package clickhouse_api
1919

2020
import (
2121
"fmt"
22-
"github.com/ClickHouse/clickhouse-go/v2"
2322
"math/rand"
23+
24+
"github.com/ClickHouse/clickhouse-go/v2"
2425
)
2526

2627
func MultiHostVersion() error {
@@ -34,6 +35,7 @@ func MultiHostRoundRobinVersion() error {
3435

3536
func MultiHostRandomVersion() error {
3637
rand.Seed(85206178671753423)
38+
defer ResetRandSeed()
3739
connOpenStrategy := clickhouse.ConnOpenRandom
3840
return multiHostVersion(&connOpenStrategy)
3941
}

examples/clickhouse_api/utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/ClickHouse/clickhouse-go/v2"
2323
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
2424
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25+
"math/rand"
26+
"time"
2527
)
2628

2729
const TestSet string = "examples_clickhouse_api"
@@ -41,3 +43,9 @@ func GetNativeConnectionWithOptions(settings clickhouse.Settings, tlsConfig *tls
4143
func CheckMinServerVersion(conn driver.Conn, major, minor, patch uint64) bool {
4244
return clickhouse_tests.CheckMinServerServerVersion(conn, major, minor, patch)
4345
}
46+
47+
var randSeed = time.Now().UnixNano()
48+
49+
func ResetRandSeed() {
50+
rand.Seed(randSeed)
51+
}

tests/conn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func TestConnFailoverRoundRobin(t *testing.T) {
7676
}
7777

7878
func TestConnFailoverRandom(t *testing.T) {
79-
// TODO: find better way to make test deterministic
8079
rand.Seed(85206178671753423)
80+
defer ResetRandSeed()
8181
testConnFailover(t, clickhouse.ConnOpenRandom)
8282
}
8383

tests/std/conn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func TestStdConnFailoverRoundRobin(t *testing.T) {
6060
}
6161

6262
func TestStdConnFailoverRandom(t *testing.T) {
63-
// TODO: find better way to make test deterministic
6463
rand.Seed(85206178671753423)
64+
defer clickhouse_tests.ResetRandSeed()
6565
testStdConnFailover(t, "random")
6666
}
6767

tests/utils.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353

5454
var testUUID = uuid.NewString()[0:12]
5555
var testTimestamp = time.Now().UnixMilli()
56+
var randSeed = time.Now().UnixNano()
5657

5758
const defaultClickHouseVersion = "latest"
5859

@@ -787,10 +788,13 @@ func OptionsToDSN(o *clickhouse.Options) string {
787788
return u.String()
788789
}
789790

791+
func ResetRandSeed() {
792+
rand.Seed(randSeed)
793+
}
794+
790795
func Runtime(m *testing.M, ts string) (exitCode int) {
791-
seed := time.Now().UnixNano()
792-
rand.Seed(seed)
793-
fmt.Printf("using random seed %d for %s tests\n", seed, ts)
796+
ResetRandSeed()
797+
fmt.Printf("using random seed %d for %s tests\n", randSeed, ts)
794798

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

0 commit comments

Comments
 (0)