Skip to content

Commit a44715e

Browse files
committed
Make redis cache update synchronous and stabilize websocket test
1 parent 570c113 commit a44715e

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

adapters/redis/storage.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,10 @@ func (s *Store) GetState(ctx context.Context, userID core.UserID) (core.UserStat
169169
return core.UserState{}, err
170170
}
171171

172-
// Update cache asynchronously (don't block the response)
173-
go func() {
174-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
175-
defer cancel()
176-
if err := s.updateStateCache(ctx, userID, state); err != nil {
177-
// Cache update failures are not critical, just log them
178-
// We don't fail the main operation for cache issues
179-
}
180-
}()
172+
// Update cache (best-effort); keep it synchronous for determinism.
173+
ctxCache, cancel := context.WithTimeout(context.Background(), 1*time.Second)
174+
defer cancel()
175+
_ = s.updateStateCache(ctxCache, userID, state)
181176

182177
return state, nil
183178
}

adapters/websocket/adapter_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ func TestHandlerStreamsEvents(t *testing.T) {
2525
}
2626
defer conn.Close()
2727

28+
// ensure subscriber goroutine is ready
29+
time.Sleep(10 * time.Millisecond)
30+
2831
ev := core.NewPointsAdded("alice", core.MetricXP, 5, 5)
2932
hub.Broadcast(context.Background(), ev)
3033

31-
_ = conn.SetReadDeadline(time.Now().Add(time.Second))
34+
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
3235
_, msg, err := conn.ReadMessage()
3336
if err != nil {
3437
t.Fatalf("read message: %v", err)

0 commit comments

Comments
 (0)