Skip to content

Commit

Permalink
fix: connection test with "random" strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Feb 26, 2025
1 parent c7e323f commit 5374fff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func DefaultDialStrategy(ctx context.Context, connID int, opt *Options, dial Dia
case ConnOpenInOrder:
num = i
case ConnOpenRoundRobin:
num = (int(connID) + i) % len(opt.Addr)
num = (connID + i) % len(opt.Addr)
case ConnOpenRandom:
random := rand.Int()
num = (random + i) % len(opt.Addr)
Expand Down
4 changes: 2 additions & 2 deletions clickhouse_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func (o *stdConnOpener) Connect(ctx context.Context) (_ driver.Conn, err error)
return nil, ErrAcquireConnNoAddress
}

random := rand.Int()
for i := range o.opt.Addr {
var num int
switch o.opt.ConnOpenStrategy {
case ConnOpenInOrder:
num = i
case ConnOpenRoundRobin:
num = (int(connID) + i) % len(o.opt.Addr)
num = (connID + i) % len(o.opt.Addr)
case ConnOpenRandom:
random := rand.Int()
num = (random + i) % len(o.opt.Addr)
}
if conn, err = dialFunc(ctx, o.opt.Addr[num], connID, o.opt); err == nil {
Expand Down
18 changes: 9 additions & 9 deletions tests/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"crypto/tls"
"fmt"
"math/rand"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -67,20 +68,20 @@ func TestBadConn(t *testing.T) {
}

func TestConnFailover(t *testing.T) {
testConnFailover(t, nil)
testConnFailover(t, clickhouse.ConnOpenInOrder)
}

func TestConnFailoverRoundRobin(t *testing.T) {
strategy := clickhouse.ConnOpenRoundRobin
testConnFailover(t, &strategy)
testConnFailover(t, clickhouse.ConnOpenRoundRobin)
}

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

func testConnFailover(t *testing.T, connOpenStrategy *clickhouse.ConnOpenStrategy) {
func testConnFailover(t *testing.T, connOpenStrategy clickhouse.ConnOpenStrategy) {
env, err := GetNativeTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(GetEnv("CLICKHOUSE_USE_SSL", "false"))
Expand All @@ -92,6 +93,7 @@ func testConnFailover(t *testing.T, connOpenStrategy *clickhouse.ConnOpenStrateg
tlsConfig = &tls.Config{}
}
options := clickhouse.Options{
ConnOpenStrategy: connOpenStrategy,
Addr: []string{
"127.0.0.1:9001",
"127.0.0.1:9002",
Expand All @@ -107,9 +109,7 @@ func testConnFailover(t *testing.T, connOpenStrategy *clickhouse.ConnOpenStrateg
},
TLS: tlsConfig,
}
if connOpenStrategy != nil {
options.ConnOpenStrategy = *connOpenStrategy
}

conn, err := GetConnectionWithOptions(&options)
require.NoError(t, err)
require.NoError(t, conn.Ping(context.Background()))
Expand Down

0 comments on commit 5374fff

Please sign in to comment.