Skip to content

Commit

Permalink
test: rewrite TestReconnect
Browse files Browse the repository at this point in the history
Rewrote TestReconnect.
Instead of just conn.Close() used stop the tarantool instance to
avoid auto-reconnect. Restarted it after. Added checks.

Part of #157
  • Loading branch information
better0fdead committed Mar 24, 2023
1 parent cc73cc2 commit 5b333c2
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions multi/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,51 @@ func TestConnSuccessfully(t *testing.T) {
}

func TestReconnect(t *testing.T) {
multiConn, _ := Connect([]string{server1, server2}, connOpts)
sleep := 100 * time.Millisecond
sleepCnt := 50
servers := []string{server1, server2}
multiConn, _ := Connect(servers, connOpts)
if multiConn == nil {
t.Errorf("conn is nil after Connect")
return
}
timer := time.NewTimer(300 * time.Millisecond)
<-timer.C
defer multiConn.Close()
test_helpers.StopTarantoolWithCleanup(instances[0])

conn, _ := multiConn.getConnectionFromPool(server1)
conn.Close()
for i := 0; i < sleepCnt; i++ {
_, ok := multiConn.getConnectionFromPool(servers[0])
if !ok {
break
}
multiConn.getState()
time.Sleep(sleep)
}

_, ok := multiConn.getConnectionFromPool(servers[0])
if ok {
t.Fatalf("failed to close conn")
}

if multiConn.getCurrentConnection().Addr() == server1 {
if multiConn.getCurrentConnection().Addr() == servers[0] {
t.Errorf("conn has incorrect addr: %s after disconnect server1", multiConn.getCurrentConnection().Addr())
}
if !multiConn.ConnectedNow() {
t.Errorf("incorrect multiConn status after reconnecting")

err := test_helpers.RestartTarantool(&instances[0])
if err != nil {
t.Fatalf("failed to restart Tarantool: %s", err)
}

timer = time.NewTimer(100 * time.Millisecond)
<-timer.C
conn, _ = multiConn.getConnectionFromPool(server1)
for i := 0; i < sleepCnt; i++ {
_, ok := multiConn.getConnectionFromPool(servers[0])
if ok {
break
}
time.Sleep(sleep)
}

conn, ok := multiConn.getConnectionFromPool(servers[0])
if !ok {
t.Fatalf("incorrect conn status after reconnecting")
}
if !conn.ConnectedNow() {
t.Errorf("incorrect conn status after reconnecting")
}
Expand Down

0 comments on commit 5b333c2

Please sign in to comment.