Skip to content

Commit 2d7bb13

Browse files
author
Kryvchun
committed
Fix reconnect on ping
1 parent 13bf620 commit 2d7bb13

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

sonic/pool_test.go

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net"
7-
"sync"
87
"testing"
98
"time"
109

@@ -14,15 +13,19 @@ import (
1413
func TestPool_Reconnect(t *testing.T) {
1514
host, port, pass := getSonicConfig(t)
1615

17-
closeAllCond := sync.NewCond(&sync.Mutex{})
1816
proxyLn, proxyDoneCh := runTCPProxy(t,
1917
fmt.Sprintf("%s:%d", host, port), // Target addr.
2018
"127.0.0.1:0", // Proxy addr.
2119
)
2220

2321
proxyHost, proxyPort := mustSplitHostPort(t, proxyLn.Addr().String())
2422

25-
ing, err := sonic.NewIngester(proxyHost, proxyPort, pass)
23+
ing, err := sonic.NewIngester(
24+
proxyHost,
25+
proxyPort,
26+
pass,
27+
sonic.OptionPoolPingThreshold(time.Nanosecond),
28+
)
2629
if err != nil {
2730
t.Fatal("NewIngester", err)
2831
}
@@ -34,10 +37,6 @@ func TestPool_Reconnect(t *testing.T) {
3437
t.Fatal("Ping", err)
3538
}
3639

37-
closeAllCond.L.Lock()
38-
closeAllCond.Broadcast()
39-
closeAllCond.L.Unlock()
40-
4140
err = ing.Ping()
4241
if err != nil {
4342
t.Fatal("Ping", err)
@@ -85,6 +84,80 @@ func TestPool_Reconnect(t *testing.T) {
8584
}
8685
}
8786

87+
func TestPool_Reconnect_Threshold(t *testing.T) {
88+
host, port, pass := getSonicConfig(t)
89+
90+
proxyLn, proxyDoneCh := runTCPProxy(t,
91+
fmt.Sprintf("%s:%d", host, port), // Target addr.
92+
"127.0.0.1:0", // Proxy addr.
93+
)
94+
95+
proxyHost, proxyPort := mustSplitHostPort(t, proxyLn.Addr().String())
96+
97+
ing, err := sonic.NewIngester(
98+
proxyHost,
99+
proxyPort,
100+
pass,
101+
sonic.OptionPoolPingThreshold(time.Minute),
102+
)
103+
if err != nil {
104+
t.Fatal("NewIngester", err)
105+
}
106+
107+
// Connection healthy, ping should work.
108+
109+
err = ing.Ping()
110+
if err != nil {
111+
t.Fatal("Ping", err)
112+
}
113+
114+
err = ing.Ping()
115+
if err != nil {
116+
t.Fatal("Ping", err)
117+
}
118+
119+
// Close connection, ping should not work.
120+
121+
err = proxyLn.Close()
122+
if err != nil {
123+
t.Fatal("Close", err)
124+
}
125+
126+
select {
127+
case <-proxyDoneCh:
128+
case <-time.After(2 * time.Second):
129+
t.Fatal("Timeout")
130+
}
131+
132+
err = ing.Ping()
133+
if err == nil {
134+
t.Fatal("Ping", err)
135+
}
136+
137+
// Reconnect in threshold, ping still should not work.
138+
139+
proxyLn, proxyDoneCh = runTCPProxy(t,
140+
fmt.Sprintf("%s:%d", host, port), // Target addr.
141+
fmt.Sprintf("%s:%d", proxyHost, proxyPort), // Proxy addr.
142+
)
143+
144+
err = ing.Ping()
145+
if err == nil {
146+
t.Fatal("Ping", err)
147+
}
148+
149+
err = proxyLn.Close()
150+
if err != nil {
151+
t.Fatal("Close", err)
152+
}
153+
154+
select {
155+
case <-proxyDoneCh:
156+
case <-time.After(2 * time.Second):
157+
t.Fatal("Timeout")
158+
}
159+
}
160+
88161
func runTCPProxy(
89162
tb testing.TB,
90163
targetAddr string,

0 commit comments

Comments
 (0)