From e6121896af2519600aa289ba7b0fba58c2e77b9b Mon Sep 17 00:00:00 2001 From: Lakhan Samani Date: Sat, 11 Jul 2026 19:15:50 +0530 Subject: [PATCH] test: stop integration TempDir cleanup racing with fire-and-forget goroutines make test-all-db flaked on macOS: TestLogout (and any integration test) could fail during teardown with 'TempDir RemoveAll: directory not empty'. The SQLite test DB lives in the temp dir and uses WAL; the fire-and-forget LogEvent/AddSession goroutines from Login/SignUp/Logout can create a -wal/-shm sidecar file exactly as t.TempDir() force-removes the dir, failing an already-passed test. Manage the dir ourselves and remove it best-effort (retry, ignore errors) so cleanup-race noise can't fail the suite. Test-only; green in CI (Linux) already, this fixes the local full-suite run. --- internal/integration_tests/test_helper.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/integration_tests/test_helper.go b/internal/integration_tests/test_helper.go index a09619cb..c1bcab83 100644 --- a/internal/integration_tests/test_helper.go +++ b/internal/integration_tests/test_helper.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -216,7 +217,24 @@ func initTestSetup(t *testing.T, cfg *config.Config) *testSetup { } if cfg.DatabaseType == constants.DbTypeSqlite || cfg.DatabaseType == constants.DbTypeLibSQL { - cfg.DatabaseURL = filepath.Join(t.TempDir(), "authorizer_integration.db") + // Not t.TempDir(): its RemoveAll fails the test if the dir is non-empty. + // The SQLite DB uses WAL, and the fire-and-forget goroutines from + // LogEvent/AddSession (see the storage-close cleanup below) can create a + // -wal/-shm sidecar file mid-teardown — a benign race that would + // otherwise fail an already-passed test. Manage the dir ourselves and + // remove it best-effort. Registered here so it runs AFTER the storage + // Close cleanup (t.Cleanup is LIFO; the closer is registered later). + dbDir, mkErr := os.MkdirTemp("", "authorizer-it-*") + require.NoError(t, mkErr) + t.Cleanup(func() { + for i := 0; i < 3; i++ { + if err := os.RemoveAll(dbDir); err == nil { + return + } + time.Sleep(50 * time.Millisecond) + } + }) + cfg.DatabaseURL = filepath.Join(dbDir, "authorizer_integration.db") } // Initialize storage provider first as it's required by other providers