Skip to content

Commit b3bf252

Browse files
committed
fix: tests
1 parent 9377baf commit b3bf252

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

backend/internal/download/manager_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httptest"
77
"os"
88
"path/filepath"
9+
"sync"
910
"testing"
1011
"time"
1112

@@ -205,10 +206,13 @@ func TestManagerProgressCallback(t *testing.T) {
205206
manager, database, _, cleanup := setupTestManager(t, 1)
206207
defer cleanup()
207208

208-
// Track progress callbacks
209+
// Track progress callbacks with mutex protection
210+
var mu sync.Mutex
209211
progressCalls := 0
210212
var lastStatus models.ISOStatus
211213
manager.SetProgressCallback(func(isoID string, progress int, status models.ISOStatus) {
214+
mu.Lock()
215+
defer mu.Unlock()
212216
progressCalls++
213217
lastStatus = status
214218
})
@@ -246,12 +250,17 @@ func TestManagerProgressCallback(t *testing.T) {
246250
time.Sleep(2 * time.Second)
247251

248252
// Verify we got progress callbacks
249-
if progressCalls == 0 {
253+
mu.Lock()
254+
calls := progressCalls
255+
status := lastStatus
256+
mu.Unlock()
257+
258+
if calls == 0 {
250259
t.Error("Expected progress callbacks, got none")
251260
}
252261

253-
if lastStatus != models.StatusComplete {
254-
t.Errorf("Last status should be 'complete', got: %s", lastStatus)
262+
if status != models.StatusComplete {
263+
t.Errorf("Last status should be 'complete', got: %s", status)
255264
}
256265
}
257266

backend/internal/ws/hub.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ws
33
import (
44
"encoding/json"
55
"log/slog"
6+
"sync"
67

78
"linux-iso-manager/internal/models"
89
)
@@ -31,6 +32,9 @@ type Hub struct {
3132
// Registered clients
3233
clients map[*Client]bool
3334

35+
// Protects clients map for concurrent read access
36+
mu sync.RWMutex
37+
3438
// Inbound messages from clients
3539
broadcast chan []byte
3640

@@ -56,18 +60,27 @@ func (h *Hub) Run() {
5660
for {
5761
select {
5862
case client := <-h.register:
63+
h.mu.Lock()
5964
h.clients[client] = true
60-
slog.Debug("websocket client connected", slog.Int("total_clients", len(h.clients)))
65+
count := len(h.clients)
66+
h.mu.Unlock()
67+
slog.Debug("websocket client connected", slog.Int("total_clients", count))
6168

6269
case client := <-h.unregister:
70+
h.mu.Lock()
6371
if _, ok := h.clients[client]; ok {
6472
delete(h.clients, client)
6573
close(client.send)
66-
slog.Debug("websocket client disconnected", slog.Int("total_clients", len(h.clients)))
74+
count := len(h.clients)
75+
h.mu.Unlock()
76+
slog.Debug("websocket client disconnected", slog.Int("total_clients", count))
77+
} else {
78+
h.mu.Unlock()
6779
}
6880

6981
case message := <-h.broadcast:
7082
// Broadcast to all connected clients
83+
h.mu.Lock()
7184
for client := range h.clients {
7285
select {
7386
case client.send <- message:
@@ -77,6 +90,7 @@ func (h *Hub) Run() {
7790
delete(h.clients, client)
7891
}
7992
}
93+
h.mu.Unlock()
8094
}
8195
}
8296
}
@@ -113,5 +127,7 @@ func (h *Hub) BroadcastProgress(isoID string, progress int, status models.ISOSta
113127

114128
// ClientCount returns the number of connected clients.
115129
func (h *Hub) ClientCount() int {
130+
h.mu.RLock()
131+
defer h.mu.RUnlock()
116132
return len(h.clients)
117133
}

0 commit comments

Comments
 (0)