diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index fdd4757..fa87d92 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "sync" + "sync/atomic" "testing" "github.com/labstack/echo/v4" @@ -21,7 +22,10 @@ import ( "github.com/treytartt/honeydue-api/internal/validator" ) -var i18nOnce sync.Once +var ( + i18nOnce sync.Once + testDBCounter uint64 +) // SetupTestDB creates an in-memory SQLite database for testing func SetupTestDB(t *testing.T) *gorm.DB { @@ -30,7 +34,16 @@ func SetupTestDB(t *testing.T) *gorm.DB { i18n.Init() }) - db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{ + // SQLite in-memory + GORM connection pool needs `cache=shared` so all + // pool connections see the same DB. With plain ":memory:", each new + // connection gets its own empty database, which makes errgroup-driven + // parallel reads explode with "no such table" once a goroutine pulls a + // fresh connection. The DSN is uniqued per test invocation (atomic + // counter) so tests don't bleed state into each other through the shared + // cache namespace. + id := atomic.AddUint64(&testDBCounter, 1) + dsn := fmt.Sprintf("file:testdb_%d?mode=memory&cache=shared&_journal=memory", id) + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) require.NoError(t, err)