server: add a connections leak unit test
Signed-off-by: fupan.lfp <fupan.lfp@antfin.com>
This commit is contained in:
parent
9ece5ae787
commit
4f8e36d414
@ -216,6 +216,13 @@ func (s *Server) delConnection(c *serverConn) {
|
||||
delete(s.connections, c)
|
||||
}
|
||||
|
||||
func (s *Server) countConnection() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
return len(s.connections)
|
||||
}
|
||||
|
||||
func (s *Server) closeIdleConns() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
@ -459,6 +459,53 @@ func TestServerRequestTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerConnectionsLeak(t *testing.T) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
server = mustServer(t)(NewServer())
|
||||
addr, listener = newTestListener(t)
|
||||
client, cleanup = newTestClient(t, addr)
|
||||
)
|
||||
defer cleanup()
|
||||
defer listener.Close()
|
||||
|
||||
connectionCountBefore := server.countConnection()
|
||||
|
||||
go server.Serve(ctx, listener)
|
||||
|
||||
registerTestingService(server, &testingServer{})
|
||||
|
||||
tp := &testPayload{}
|
||||
// do a regular call
|
||||
if err := client.Call(ctx, serviceName, "Test", tp, tp); err != nil {
|
||||
t.Fatalf("unexpected error during test call: %v", err)
|
||||
}
|
||||
|
||||
connectionCount := server.countConnection()
|
||||
if connectionCount != 1 {
|
||||
t.Fatalf("unexpected connection count: %d, expected: %d", connectionCount, 1)
|
||||
}
|
||||
|
||||
// close the client, so that server gets EOF
|
||||
if err := client.Close(); err != nil {
|
||||
t.Fatalf("unexpected error while closing client: %v", err)
|
||||
}
|
||||
|
||||
// server should eventually close the client connection
|
||||
maxAttempts := 20
|
||||
for i := 1; i <= maxAttempts; i++ {
|
||||
connectionCountAfter := server.countConnection()
|
||||
if connectionCountAfter == connectionCountBefore {
|
||||
break
|
||||
}
|
||||
if i == maxAttempts {
|
||||
t.Fatalf("expected number of connections to be equal %d after client close, got %d connections",
|
||||
connectionCountBefore, connectionCountAfter)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRoundTrip(b *testing.B) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
|
Loading…
Reference in New Issue
Block a user