fix(backend): serialize SQLite test connections and await async normalize
This commit is contained in:
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 {
|
||||
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{})
|
||||
mr := newMockRedis()
|
||||
sessionStore := auth.NewSessionStore()
|
||||
@@ -1361,4 +1368,30 @@ func TestHandleAdminMediaByID_Normalize(t *testing.T) {
|
||||
if w.Code != http.StatusAccepted {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user