Handle empty dashboard metrics safely

This commit is contained in:
Mikei386
2026-07-13 20:44:46 +02:00
parent b1fba3ed16
commit ea97d7dbc9
9 changed files with 30 additions and 8 deletions
+12
View File
@@ -2,6 +2,7 @@ package service
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
@@ -11,6 +12,17 @@ import (
"git.casaderoll.de/michael/urbm/internal/store"
)
func TestEmptyBackupMetricsEncodeAsJSONArray(t *testing.T) {
s := &Service{}
encoded, err := json.Marshal(s.BackupMetrics())
if err != nil {
t.Fatal(err)
}
if string(encoded) != "[]" {
t.Fatalf("empty backup metrics JSON = %s", encoded)
}
}
func TestRepositoryLockHonorsContextCancellation(t *testing.T) {
s := &Service{repositoryLocks: map[string]chan struct{}{}}
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo"}
+3 -1
View File
@@ -134,7 +134,9 @@ func (s *Service) Runs() []model.Run { return s.queue.Snapshot() }
func (s *Service) BackupMetrics() []model.BackupMetric {
s.metricsMu.RLock()
defer s.metricsMu.RUnlock()
return append([]model.BackupMetric(nil), s.backupMetrics...)
metrics := make([]model.BackupMetric, len(s.backupMetrics))
copy(metrics, s.backupMetrics)
return metrics
}
func (s *Service) Logs() map[string]any {
return map[string]any{