From b700f6462418c1236aacdc8e8dc254676db893eb Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 24 Mar 2026 20:55:37 -0400 Subject: [PATCH] fix: remove redundant defer setup.Close() calls to enable library cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cmd/server/tests/fsnotify_integration_test.go | 1 - cmd/server/tests/goroutine_leak_test.go | 1 - cmd/server/tests/jobs_test.go | 7 ------- cmd/server/tests/library_browse_test.go | 1 - cmd/server/tests/scan_settings_integration_test.go | 3 --- cmd/server/tests/worker_test.go | 4 ---- 6 files changed, 17 deletions(-) diff --git a/cmd/server/tests/fsnotify_integration_test.go b/cmd/server/tests/fsnotify_integration_test.go index b5c4a07..2ff54d4 100644 --- a/cmd/server/tests/fsnotify_integration_test.go +++ b/cmd/server/tests/fsnotify_integration_test.go @@ -16,7 +16,6 @@ import ( func TestFSNotify_BulkFileDetection(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() t.Run("Detects multiple files added simultaneously", func(t *testing.T) { token := setup.Token tmpDir := t.TempDir() diff --git a/cmd/server/tests/goroutine_leak_test.go b/cmd/server/tests/goroutine_leak_test.go index 69f4174..a0d7f63 100644 --- a/cmd/server/tests/goroutine_leak_test.go +++ b/cmd/server/tests/goroutine_leak_test.go @@ -19,7 +19,6 @@ func TestGoroutineCleanup(t *testing.T) { // Start server setup := setupTestServer(t) - defer setup.Close() // Wait for startup time.Sleep(200 * time.Millisecond) diff --git a/cmd/server/tests/jobs_test.go b/cmd/server/tests/jobs_test.go index 05d2d14..d65df5f 100644 --- a/cmd/server/tests/jobs_test.go +++ b/cmd/server/tests/jobs_test.go @@ -15,7 +15,6 @@ import ( func TestJobsHandler_CreateJob(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -50,7 +49,6 @@ func TestJobsHandler_CreateJob(t *testing.T) { func TestJobsHandler_CreateJob_InvalidType(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -74,7 +72,6 @@ func TestJobsHandler_CreateJob_InvalidType(t *testing.T) { func TestJobsHandler_GetJobStatus(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -119,7 +116,6 @@ func TestJobsHandler_GetJobStatus(t *testing.T) { func TestJobsHandler_GetJobStatus_NotFound(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -139,7 +135,6 @@ func TestJobsHandler_GetJobStatus_NotFound(t *testing.T) { func TestJobsHandler_CreateAndTrackJob(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -194,7 +189,6 @@ func TestJobsHandler_CreateAndTrackJob(t *testing.T) { func TestJobsHandler_CreateJob_Unauthorized(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() jobReq := map[string]interface{}{ "type": "import", @@ -215,7 +209,6 @@ func TestJobsHandler_CreateJob_Unauthorized(t *testing.T) { func TestJobsHandler_GetJobStatus_Unauthorized(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() jobID := uuid.New().String() diff --git a/cmd/server/tests/library_browse_test.go b/cmd/server/tests/library_browse_test.go index c6e0a6f..73c3e71 100644 --- a/cmd/server/tests/library_browse_test.go +++ b/cmd/server/tests/library_browse_test.go @@ -11,7 +11,6 @@ import ( func TestBrowseLibraryFoldersEndpoint(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() t.Run("GET /api/libraries/browse - no authentication returns 401", func(t *testing.T) { req := httptest.NewRequest("GET", "/api/libraries/browse?path=/tmp", nil) diff --git a/cmd/server/tests/scan_settings_integration_test.go b/cmd/server/tests/scan_settings_integration_test.go index ceb3563..a149e9c 100644 --- a/cmd/server/tests/scan_settings_integration_test.go +++ b/cmd/server/tests/scan_settings_integration_test.go @@ -12,7 +12,6 @@ import ( func TestScanSettings_GetSettings(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() t.Run("Get settings as admin", func(t *testing.T) { token := setup.Token @@ -44,7 +43,6 @@ func TestScanSettings_GetSettings(t *testing.T) { func TestScanSettings_UpdateSettings(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() t.Run("Update scan_poll_interval_seconds", func(t *testing.T) { token := setup.Token @@ -148,7 +146,6 @@ func TestScanSettings_UpdateSettings(t *testing.T) { func TestScanSettings_RequireAdmin(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() t.Run("Get settings without auth returns 401", func(t *testing.T) { req, _ := http.NewRequest("GET", setup.Server.URL+"/api/libraries/scan-settings", nil) diff --git a/cmd/server/tests/worker_test.go b/cmd/server/tests/worker_test.go index 5e394aa..6dc6642 100644 --- a/cmd/server/tests/worker_test.go +++ b/cmd/server/tests/worker_test.go @@ -21,7 +21,6 @@ import ( func TestWorker_DirectoryScanJob(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -122,7 +121,6 @@ func TestWorker_DirectoryScanJob(t *testing.T) { func TestWorker_SetFoldersJob(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() token := setup.Token @@ -192,7 +190,6 @@ func TestWorker_SetFoldersJob(t *testing.T) { func TestWorker_ConcurrentJobs(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() ctx := context.Background() @@ -253,7 +250,6 @@ func TestWorker_ConcurrentJobs(t *testing.T) { func TestWorker_JobStatusTracking(t *testing.T) { setup := setupTestServer(t) - defer setup.Close() ctx := context.Background()