fix(backend): serialize SQLite test connections and await async normalize
CI / test (push) Successful in 1m44s
CI / docker (push) Successful in 2m45s

This commit is contained in:
zwitschi committed 2026-08-13 13:40:03 +02:00
1 parent 568112b85a
commit 70a5159f81
1 file changed
+33
@@ -103,6 +103,13 @@ func setupTestDeps(t *testing.T) (*Dependencies, *gorm.DB, *auth.SessionStore) {
if err != nil { if err != nil {
t.Fatalf("failed to open test DB: %v", err) t.Fatalf("failed to open test DB: %v", err)
} }
// A single connection keeps the in-memory SQLite database shared across
// goroutines (each :memory: connection would otherwise be a separate DB).
sqlDB, err := gormDB.DB()
if err != nil {
t.Fatalf("failed to get sql.DB: %v", err)
}
sqlDB.SetMaxOpenConns(1)
gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{}, &db.Media{}, &db.ScheduleOverride{}, &db.User{}) gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{}, &db.Media{}, &db.ScheduleOverride{}, &db.User{})
mr := newMockRedis() mr := newMockRedis()
sessionStore := auth.NewSessionStore() sessionStore := auth.NewSessionStore()
@@ -1361,4 +1368,30 @@ func TestHandleAdminMediaByID_Normalize(t *testing.T) {
if w.Code != http.StatusAccepted { if w.Code != http.StatusAccepted {
t.Errorf("status = %d, want 202", w.Code) t.Errorf("status = %d, want 202", w.Code)
} }
// Wait for the asynchronous normalization goroutine to finish before
// t.Cleanup restores media.NormalizeVideo, avoiding a data race on the
// package-level mock variable.
var updated db.Media
deadline := time.Now().Add(5 * time.Second)
for {
gormDB.First(&updated, 1)
if updated.Status != media.StatusNormalizing {
break
}
if time.Now().After(deadline) {
t.Fatal("normalization did not complete within 5s")
}
time.Sleep(10 * time.Millisecond)
}
if updated.Status != media.StatusReady {
t.Errorf("Status = %q, want ready", updated.Status)
}
if updated.DurationSeconds != 90 {
t.Errorf("DurationSeconds = %d, want 90", updated.DurationSeconds)
}
if !strings.HasSuffix(updated.Path, ".normalized.mp4") {
t.Errorf("Path = %q, want .normalized.mp4 suffix", updated.Path)
}
} }