Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions datastore/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package datastore

import (
"context"
"fmt"
"os"
"sync/atomic"
"testing"
"time"

Expand All @@ -10,20 +13,44 @@ 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)
assert.Equal(t, "test_ureadability", server.dbName)
})

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)
})
Expand All @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions datastore/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions datastore/summaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/go-pkgz/testutils/containers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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()
}
Loading