Wait for clients to be terminated before destroying hub in tests.

This should help with the flaky test failures on CI.
This commit is contained in:
Joachim Bauch 2021-04-23 15:12:09 +02:00
parent cc0004d60c
commit 8052df4ef5
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 18 additions and 5 deletions

18
hub.go
View file

@ -105,8 +105,10 @@ type Hub struct {
info *HelloServerMessageServer
infoInternal *HelloServerMessageServer
stopped int32
stopChan chan bool
stopped int32
stopChan chan bool
readPumpActive uint32
writePumpActive uint32
roomUpdated chan *BackendServerRoomRequest
roomDeleted chan *BackendServerRoomRequest
@ -1858,6 +1860,14 @@ func (h *Hub) serveWs(w http.ResponseWriter, r *http.Request) {
}
h.processNewClient(client)
go client.WritePump()
go client.ReadPump()
go func(h *Hub) {
atomic.AddUint32(&h.writePumpActive, 1)
defer atomic.AddUint32(&h.writePumpActive, ^uint32(0))
client.WritePump()
}(h)
go func(h *Hub) {
atomic.AddUint32(&h.readPumpActive, 1)
defer atomic.AddUint32(&h.readPumpActive, ^uint32(0))
client.ReadPump()
}(h)
}

View file

@ -147,7 +147,9 @@ func WaitForHub(ctx context.Context, t *testing.T, h *Hub) {
h.ru.Lock()
rooms := len(h.rooms)
h.ru.Unlock()
if clients == 0 && rooms == 0 && sessions == 0 {
readActive := atomic.LoadUint32(&h.readPumpActive)
writeActive := atomic.LoadUint32(&h.writePumpActive)
if clients == 0 && rooms == 0 && sessions == 0 && readActive == 0 && writeActive == 0 {
break
}
@ -163,6 +165,7 @@ func WaitForHub(ctx context.Context, t *testing.T, h *Hub) {
time.Sleep(time.Millisecond)
}
}
}
func validateBackendChecksum(t *testing.T, f func(http.ResponseWriter, *http.Request, *BackendClientRequest) *BackendClientResponse) func(http.ResponseWriter, *http.Request) {