fix(conn-limiter): add regression test for double-close guard

Verifies the atomic.Bool CAS in serverConn.Close() releases the limiter
slot exactly once, regardless of how many times Close() is called.
This commit is contained in:
ginuerzh
2026-07-11 20:23:32 +08:00
parent 108fd6a1f5
commit d12663dfe3
+17
View File
@@ -90,6 +90,23 @@ func TestServerConn_Close_ReleasesLimiter(t *testing.T) {
}
}
func TestServerConn_Close_DoubleClose_ReleasesLimiterOnce(t *testing.T) {
c := &mockConn{}
lim := &allowLimiter{limit: 2}
lim.Allow(2) // acquire both slots
sc := &serverConn{Conn: c, limiter: lim}
sc.Close() // first close: releases one slot
sc.Close() // second close: must NOT release the second slot
if !lim.Allow(1) {
t.Fatal("first close should have released a slot")
}
if lim.Allow(1) {
t.Fatal("second close released another slot: double-release bug")
}
}
func TestServerConn_CloseRead_Supported(t *testing.T) {
inner := &connWithCloseRead{Conn: &mockConn{}}
sc := &serverConn{Conn: inner}