diff --git a/datastore/mongo_test.go b/datastore/mongo_test.go index a59bec5..9a2cd6c 100644 --- a/datastore/mongo_test.go +++ b/datastore/mongo_test.go @@ -2,6 +2,9 @@ package datastore import ( "context" + "fmt" + "os" + "sync/atomic" "testing" "time" @@ -10,12 +13,36 @@ import ( "github.com/stretchr/testify/require" ) -func TestNew(t *testing.T) { - mc := containers.NewMongoTestContainer(context.Background(), t, 5) +var ( + mongoURI string // the one MongoDB container shared by all tests of the package + dbSeq atomic.Int64 +) + +// TestMain starts a single MongoDB container for the whole package. A container per test made the +// package take 20-30s on a healthy runner and went over the 60s limit on a slow one. +func TestMain(m *testing.M) { + os.Exit(runTests(m)) +} + +func runTests(m *testing.M) int { + mc, err := containers.NewMongoTestContainerE(context.Background(), 5) + if err != nil { + fmt.Fprintf(os.Stderr, "can't start mongo container: %v\n", err) + return 1 + } defer mc.Close(context.Background()) //nolint:errcheck + mongoURI = mc.URI + return m.Run() +} +// testDBName returns a database name no other test uses, so tests sharing the container stay isolated +func testDBName() string { + return fmt.Sprintf("test_ureadability_%d", dbSeq.Add(1)) +} + +func TestNew(t *testing.T) { t.Run("valid connection", func(t *testing.T) { - server, err := New(mc.URI, "test_ureadability", 0) + server, err := New(mongoURI, "test_ureadability", 0) require.NoError(t, err) assert.NotNil(t, server) assert.NotNil(t, server.client) @@ -23,7 +50,7 @@ func TestNew(t *testing.T) { }) t.Run("with delay", func(t *testing.T) { - server, err := New(mc.URI, "test_ureadability", 10*time.Millisecond) + server, err := New(mongoURI, "test_ureadability", 10*time.Millisecond) require.NoError(t, err) assert.NotNil(t, server) }) @@ -42,10 +69,7 @@ func TestNew(t *testing.T) { } func TestGetStores(t *testing.T) { - mc := containers.NewMongoTestContainer(context.Background(), t, 5) - defer mc.Close(context.Background()) //nolint:errcheck - - server, err := New(mc.URI, "test_ureadability", 0) + server, err := New(mongoURI, testDBName(), 0) require.NoError(t, err) stores := server.GetStores() diff --git a/datastore/rules_test.go b/datastore/rules_test.go index 26aa32f..6be76aa 100644 --- a/datastore/rules_test.go +++ b/datastore/rules_test.go @@ -6,7 +6,6 @@ import ( "math/rand/v2" "testing" - "github.com/go-pkgz/testutils/containers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/v2/bson" @@ -284,10 +283,7 @@ func TestRuleString(t *testing.T) { func setupRules(t *testing.T) RulesDAO { t.Helper() - mc := containers.NewMongoTestContainer(context.Background(), t, 5) - t.Cleanup(func() { mc.Close(context.Background()) }) //nolint:errcheck - - server, err := New(mc.URI, "test_ureadability", 0) + server, err := New(mongoURI, testDBName(), 0) require.NoError(t, err) stores := server.GetStores() return stores.Rules diff --git a/datastore/summaries_test.go b/datastore/summaries_test.go index 86068fc..544122b 100644 --- a/datastore/summaries_test.go +++ b/datastore/summaries_test.go @@ -5,7 +5,6 @@ import ( "testing" "time" - "github.com/go-pkgz/testutils/containers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -68,10 +67,7 @@ func TestSummariesTTLIndex(t *testing.T) { func setupStores(t *testing.T) Stores { t.Helper() - mc := containers.NewMongoTestContainer(context.Background(), t, 5) - t.Cleanup(func() { mc.Close(context.Background()) }) //nolint:errcheck - - server, err := New(mc.URI, "test_ureadability", 0) + server, err := New(mongoURI, testDBName(), 0) require.NoError(t, err) return server.GetStores() }