Problem: Tests were calling `defer setup.Close()` which was interfering with the library cleanup added in the previous commit. The execution order was: 1. setupTestServer() registers t.Cleanup() with library deletion code 2. Test calls defer setup.Close() 3. Test finishes: - defer setup.Close() runs FIRST → closes DB pool - t.Cleanup() runs SECOND → tries to delete libraries but DB is closed! This prevented "Job Status Test Library" and other test libraries from being cleaned up, leaving residual data in the database after tests. Root Cause: The setupTestServer() function already handles cleanup via t.Cleanup(), which calls setup.Close() at the end. The explicit defer calls were redundant and caused the database pool to close before library cleanup could execute. Solution: Removed all 17 occurrences of `defer setup.Close()` from test files: - worker_test.go: 4 tests - jobs_test.go: 7 tests - scan_settings_integration_test.go: 3 tests - library_browse_test.go: 1 test - goroutine_leak_test.go: 1 test - fsnotify_integration_test.go: 1 test Now setupTestServer()'s t.Cleanup() function properly: 1. Deletes "test" libraries (while DB is still connected) 2. Then calls setup.Close() to close connections This ensures all test libraries are cleaned up, leaving a clean database after `make test-integration` completes. Files changed: - cmd/server/tests/worker_test.go: Removed 4 defer calls - cmd/server/tests/jobs_test.go: Removed 7 defer calls - cmd/server/tests/scan_settings_integration_test.go: Removed 3 defer calls - cmd/server/tests/library_browse_test.go: Removed 1 defer call - cmd/server/tests/goroutine_leak_test.go: Removed 1 defer call - cmd/server/tests/fsnotify_integration_test.go: Removed 1 defer call
46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestGoroutineCleanup verifies that background goroutines can be properly shut down
|
|
func TestGoroutineCleanup(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping goroutine leak test in short mode")
|
|
}
|
|
|
|
// Baseline
|
|
time.Sleep(100 * time.Millisecond)
|
|
initialGoroutines := runtime.NumGoroutine()
|
|
t.Logf("Initial goroutine count: %d", initialGoroutines)
|
|
|
|
// Start server
|
|
setup := setupTestServer(t)
|
|
|
|
// Wait for startup
|
|
time.Sleep(200 * time.Millisecond)
|
|
runningGoroutines := runtime.NumGoroutine()
|
|
t.Logf("Goroutines while running: %d (delta: +%d)", runningGoroutines, runningGoroutines-initialGoroutines)
|
|
|
|
// Close server
|
|
setup.Close()
|
|
|
|
// Wait for cleanup
|
|
time.Sleep(500 * time.Millisecond)
|
|
finalGoroutines := runtime.NumGoroutine()
|
|
t.Logf("Goroutines after shutdown: %d (delta: %d)", finalGoroutines, finalGoroutines-initialGoroutines)
|
|
|
|
// We expect some goroutines to remain because httptest.Server.Close()
|
|
// doesn't trigger app.Shutdown(). The important thing is that we CAN
|
|
// shut them down properly (verified in production via app.Shutdown())
|
|
// Allow generous tolerance for test infrastructure
|
|
tolerance := 20
|
|
if finalGoroutines > initialGoroutines+tolerance {
|
|
t.Logf("WARNING: %d goroutines still running after shutdown (may be expected for test infrastructure)",
|
|
finalGoroutines-initialGoroutines)
|
|
}
|
|
}
|