AlexStocks commented on code in PR #140:
URL: https://github.com/apache/dubbo-getty/pull/140#discussion_r3740276211


##########
client_test.go:
##########
@@ -201,6 +201,71 @@ func TestTCPClient(t *testing.T) {
        assert.True(t, clt.IsClosed())
 }
 
+func TestTCPClientReconnectStopsAfterMaxAttempts(t *testing.T) {
+       listener, err := net.Listen("tcp", "127.0.0.1:0")
+       assert.Nil(t, err)
+       assert.NotNil(t, listener)
+       addr := listener.Addr().String()
+       assert.Nil(t, listener.Close())
+
+       clt := NewTCPClient(
+               WithServerAddress(addr),
+               WithReconnectInterval(int(time.Millisecond)),
+               WithConnectionNumber(1),
+               WithReconnectAttempts(2),
+       )
+       assert.NotNil(t, clt)
+
+       done := make(chan struct{})
+       go func() {
+               var msgHandler MessageHandler
+               cb := func(session Session) error {
+                       return newSessionCallback(session, &msgHandler)
+               }
+               clt.RunEventLoop(cb)
+               close(done)
+       }()
+
+       select {
+       case <-done:
+       case <-time.After(2 * time.Second):
+               t.Fatal("client reconnect loop did not stop after max reconnect 
attempts")
+       }
+
+       clt.Close()
+       assert.True(t, clt.IsClosed())
+}
+
+func TestTCPClientReconnectAttemptsIgnoreSuccessfulPoolFills(t *testing.T) {
+       listener, err := net.Listen("tcp", "127.0.0.1:0")
+       assert.Nil(t, err)
+       assert.NotNil(t, listener)
+       defer func() {
+               _ = listener.Close()
+       }()
+       go func() {
+               _ = http.Serve(listener, nil)
+       }()
+
+       const connPoolSize = 3
+       clt := NewTCPClient(
+               WithServerAddress(listener.Addr().String()),
+               WithReconnectInterval(int(time.Millisecond)),
+               WithConnectionNumber(connPoolSize),
+               WithReconnectAttempts(1),
+       )
+       assert.NotNil(t, clt)
+       defer clt.Close()
+
+       var msgHandler MessageHandler
+       cb := func(session Session) error {
+               return newSessionCallback(session, &msgHandler)
+       }
+       clt.RunEventLoop(cb)
+
+       assert.Equal(t, connPoolSize, msgHandler.SessionNumber())

Review Comment:
   [P1] 让测试真正覆盖成功后重置连续失败计数
   
   这个用例只有连续成功建满连接池。删除 `client.go` 中的 `reconnectAttempts = 0` 后,我在 exact Head 
mutant 上执行 `go test -count=50 . -run 
"TestTCPClientReconnect(StopsAfterMaxAttempts|AttemptsIgnoreSuccessfulPoolFills)$"`
 仍全部通过,因此它没有覆盖本 PR 的关键行为。错误实现会把成功前的失败次数带到后续重连,导致‘失败 -> 成功 -> 
再次失败’时提前耗尽上限并留下未填满的连接池。请把建连结果做成可控序列,至少覆盖失败、成功、再次失败和再次成功;断言每次成功都会重置连续失败计数,并确认删除重置语句后测试稳定失败。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to