This repository was archived by the owner on Feb 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcluster_test.go
More file actions
114 lines (74 loc) · 2.23 KB
/
cluster_test.go
File metadata and controls
114 lines (74 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package clickhouse
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func active_host(conn *Conn) string {
if conn == nil {
return ""
}
return conn.Host
}
func TestPartialCluster(t *testing.T) {
goodTr := getMockTransport("1")
badTr := getMockTransport("Code: 9999, Error: ...")
conn1 := NewConn("host1", badTr)
conn2 := NewConn("host2", goodTr)
cl := NewCluster(conn1, conn2)
// assert.Equal(t, conn1, cl.conn[0])
// assert.Equal(t, conn2, cl.conn[1])
assert.True(t, cl.IsDown())
cl.OnCheckError(func(c *Conn) {
assert.Equal(t, conn1, c)
})
cl.Check()
assert.Equal(t, conn2.Host, active_host(cl.ActiveConn()))
assert.False(t, cl.IsDown())
}
func TestFailedCluster(t *testing.T) {
badTr := getMockTransport("Code: 9999, Error: ...")
conn1 := NewConn("host1", badTr)
conn2 := NewConn("host2", badTr)
cl := NewCluster(conn1, conn2)
downCall := 0
cl.OnClusterDown(func() {
downCall++
})
cl.Check()
assert.Equal(t, 1, downCall)
assert.Nil(t, cl.ActiveConn())
assert.True(t, cl.IsDown())
}
func TestBestTransport(t *testing.T) {
transport_slow := &waitTransport{response: "1", duration: time.Millisecond * 200}
transport_fast := &waitTransport{response: "1", duration: time.Millisecond * 10}
transport_medium := &waitTransport{response: "1", duration: time.Millisecond * 50}
conn1 := NewConn("host1", transport_slow)
conn2 := NewConn("host2", transport_fast)
conn3 := NewConn("host3", transport_medium)
cl := NewCluster(conn1, conn2, conn3)
cl.Check()
assert.NotNil(t, cl.ActiveConn())
assert.False(t, cl.IsDown())
cl.Check()
assert.Equal(t, conn2.Host, active_host(cl.BestConn()))
transport_fast.response = "Code: 9999, Error: ..."
cl.Check()
assert.Equal(t, conn3.Host, active_host(cl.BestConn()))
mp := cl.RankConn()
t.Logf("ranks: %v", mp)
if mp[conn2] >= mp[conn1] || mp[conn2] >= mp[conn3] {
t.Error("Rank is corrupted")
}
}
func TestBestTransportOthers(t *testing.T) {
transport_fast := &waitTransport{response: "1", duration: time.Millisecond * 10}
conn1 := NewConn("host1", transport_fast)
cl := NewCluster(conn1)
cl.Check()
assert.NotNil(t, cl.ActiveConn())
assert.False(t, cl.IsDown())
cl.Check()
assert.Equal(t, conn1.Host, active_host(cl.BestConn()))
}