Skip to content

Commit 8c35168

Browse files
better0fdeadoleg-jukovec
authored andcommitted
test: rewrite TestReconnect
Rewrote TestReconnect. Instead of just conn.Close() used stop the tarantool instance to avoid auto-reconnect. Restarted it after. Added checks. Part of #157
1 parent 4c6be30 commit 8c35168

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

multi/multi_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,49 @@ func TestConnSuccessfully(t *testing.T) {
9292
}
9393

9494
func TestReconnect(t *testing.T) {
95-
multiConn, _ := Connect([]string{server1, server2}, connOpts)
95+
sleep := 100 * time.Millisecond
96+
sleepCnt := 50
97+
servers := []string{server1, server2}
98+
multiConn, _ := Connect(servers, connOpts)
9699
if multiConn == nil {
97100
t.Errorf("conn is nil after Connect")
98101
return
99102
}
100-
timer := time.NewTimer(300 * time.Millisecond)
101-
<-timer.C
102-
defer multiConn.Close()
103+
test_helpers.StopTarantoolWithCleanup(instances[0])
103104

104-
conn, _ := multiConn.getConnectionFromPool(server1)
105-
conn.Close()
105+
for i := 0; i < sleepCnt; i++ {
106+
_, ok := multiConn.getConnectionFromPool(servers[0])
107+
if !ok {
108+
break
109+
}
110+
time.Sleep(sleep)
111+
}
112+
113+
_, ok := multiConn.getConnectionFromPool(servers[0])
114+
if ok {
115+
t.Fatalf("failed to close conn")
116+
}
106117

107-
if multiConn.getCurrentConnection().Addr() == server1 {
118+
if multiConn.getCurrentConnection().Addr() == servers[0] {
108119
t.Errorf("conn has incorrect addr: %s after disconnect server1", multiConn.getCurrentConnection().Addr())
109120
}
110-
if !multiConn.ConnectedNow() {
111-
t.Errorf("incorrect multiConn status after reconnecting")
121+
122+
err := test_helpers.RestartTarantool(&instances[0])
123+
if err != nil {
124+
t.Fatalf("failed to restart Tarantool: %s", err)
112125
}
113126

114-
timer = time.NewTimer(100 * time.Millisecond)
115-
<-timer.C
116-
conn, _ = multiConn.getConnectionFromPool(server1)
117-
if !conn.ConnectedNow() {
118-
t.Errorf("incorrect conn status after reconnecting")
127+
for i := 0; i < sleepCnt; i++ {
128+
_, ok := multiConn.getConnectionFromPool(servers[0])
129+
if ok {
130+
break
131+
}
132+
time.Sleep(sleep)
133+
}
134+
135+
_, ok = multiConn.getConnectionFromPool(servers[0])
136+
if !ok {
137+
t.Fatalf("incorrect conn status after reconnecting")
119138
}
120139
}
121140

0 commit comments

Comments
 (0)