Wait for slot release in the WebSocket connection limit test

Agent removal occurs inside read-loop cleanup before the handler releases
its per-IP connection slot. The flood test could observe the first state
and retry while the slot was still occupied, causing a spurious rejection.

Wait for the guarded admission count to reach zero before reconnecting,
while preserving rejection at capacity and successful registration after
release.
This commit is contained in:
rcourtman
2026-09-24 18:27:22 +01:00
parent 27792cf17f
commit 2308db45bb
@@ -672,6 +672,7 @@ func TestHandleWebSocket_RejectsPerIPConnectionFlood(t *testing.T) {
t.Fatalf("Dial first connection: %v", err)
}
defer firstConn.Close()
remoteIP := normalizeWebSocketRemoteIP(firstConn.LocalAddr().String())
wsWriteMessage(t, firstConn, mustNewMessage(t, MsgTypeAgentRegister, "", AgentRegisterPayload{
AgentID: "a1",
@@ -699,6 +700,13 @@ func TestHandleWebSocket_RejectsPerIPConnectionFlood(t *testing.T) {
firstConn.Close()
waitFor(t, 2*time.Second, func() bool { return !s.IsAgentConnected("a1") })
// The read loop removes the agent before the handler's deferred slot release.
// Wait for admission capacity itself before testing that it can be reused.
waitFor(t, 2*time.Second, func() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.ipConnCounts[remoteIP] == 0
})
thirdConn, _, err := dialAgentExecWebSocket(t, ts.URL)
if err != nil {