cleanup: flakey tests and race conditions

This commit is contained in:
Jay
2026-06-04 12:07:03 -04:00
parent b5b0ec77ba
commit dda3bfd5b7
4 changed files with 15 additions and 8 deletions
+8 -1
View File
@@ -16,8 +16,11 @@ func TestAuthManager(t *testing.T) {
p, envoy := newMockEnvoy(t, WithEmbassyObserver(obs))
signed := []byte(`{"kind":22242}`)
var mu sync.Mutex
var calledWith string
callback := func(challenge string) ([]byte, error) {
mu.Lock()
defer mu.Unlock()
calledWith = challenge
return signed, nil
}
@@ -28,7 +31,11 @@ func TestAuthManager(t *testing.T) {
p.connect()
p.receive([]byte(envelope.EncloseAuthChallenge("abc")))
Eventually(t, func() bool { return calledWith == "abc" },
Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return calledWith == "abc"
},
"callback not called with challenge")
Eventually(t, func() bool {
+2 -2
View File
@@ -139,8 +139,6 @@ func (p *EventPublisher) Close() {
p.wg.Wait()
}
// trySend sends the EVENT envelope. It does not modify entry.sent — callers
// are responsible for setting it under p.mu before and/or after this call.
func (p *EventPublisher) trySend(entry *pendingEntry) error {
entry.mu.Lock()
defer entry.mu.Unlock()
@@ -212,9 +210,11 @@ func (p *EventPublisher) handleEvents() {
p.mu.Lock()
var toSend []*pendingEntry
for _, e := range p.pending {
e.mu.Lock()
if !e.sent {
toSend = append(toSend, e)
}
e.mu.Unlock()
}
p.mu.Unlock()
+3 -1
View File
@@ -364,7 +364,9 @@ func TestEventPublisher(t *testing.T) {
}, "Publish did not return a send error")
assert.ErrorContains(t, err, "send failed")
assert.Len(t, EventsOf[PublishSendFailed](obs), 1)
// on connect event handler may race with Publish()
assert.GreaterOrEqual(t, len(EventsOf[PublishSendFailed](obs)), 1)
})
t.Run("disconnect then send failure on reconnect", func(t *testing.T) {
+2 -4
View File
@@ -333,15 +333,13 @@ func TestRequestManager_Stream(t *testing.T) {
p.connect()
Eventually(t, envoy.IsConnected, "envoy should be reconnected")
// prevent activate() race between Stream and on-connect handler
time.Sleep(20 * time.Millisecond)
filters := [][]byte{[]byte(`{}`)}
id, _, _, err := m.Stream(filters)
assert.NoError(t, err)
Eventually(t, func() bool {
return len(EventsOf[ReqSendFailed](obs)) == 1
// on connect event handler may race with Stream()
return len(EventsOf[ReqSendFailed](obs)) >= 1
}, "expected ReqSendFailed observable")
failed := EventsOf[ReqSendFailed](obs)
assert.Equal(t, id, failed[0].SubID)