Skip to content

Commit

Permalink
Fix concurrent SQLite tests (FerretDB#3222)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored and yonarw committed Aug 31, 2023
1 parent f85498f commit 58c898c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
31 changes: 30 additions & 1 deletion integration/setup/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"net/url"
"os"
"path"
"path/filepath"

"github.com/stretchr/testify/require"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/FerretDB/FerretDB/internal/handlers/registry"
"github.com/FerretDB/FerretDB/internal/util/observability"
"github.com/FerretDB/FerretDB/internal/util/state"
"github.com/FerretDB/FerretDB/internal/util/testutil"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

Expand Down Expand Up @@ -131,6 +133,33 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger) string
panic("not reached")
}

// use per-test directory to prevent handler's/backend's metadata registry
// read databases owned by concurrent tests
sqliteURL := *sqliteURLF
if sqliteURL != "" {
u, err := url.Parse(sqliteURL)
require.NoError(tb, err)

require.True(tb, u.Path == "")
require.True(tb, u.Opaque != "")

u.Opaque = path.Join(u.Opaque, testutil.DatabaseName(tb)) + "/"
sqliteURL = u.String()

dir, err := filepath.Abs(u.Opaque)
require.NoError(tb, err)
require.NoError(tb, os.MkdirAll(dir, 0o777))

tb.Cleanup(func() {
if tb.Failed() {
tb.Logf("Keeping %s (%s) for debugging.", dir, sqliteURL)
return
}

require.NoError(tb, os.RemoveAll(dir))
})
}

p, err := state.NewProvider("")
require.NoError(tb, err)

Expand All @@ -140,7 +169,7 @@ func setupListener(tb testtb.TB, ctx context.Context, logger *zap.Logger) string
StateProvider: p,

PostgreSQLURL: *postgreSQLURLF,
SQLiteURL: *sqliteURLF,
SQLiteURL: sqliteURL,
HANAURL: *hanaURLF,

TestOpts: registry.TestOpts{
Expand Down
17 changes: 0 additions & 17 deletions integration/setup/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package setup
import (
"context"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"time"

Expand Down Expand Up @@ -79,21 +77,6 @@ func Startup() {
zap.S().Fatalf("Unknown target backend %q.", *targetBackendF)
}

if *sqliteURLF != "" {
u, err := url.Parse(*sqliteURLF)
if err != nil {
zap.S().Fatalf("Failed to parse -sqlite-url %s: %s", u, err)
}

must.BeTrue(u.Path == "")
must.BeTrue(u.Opaque != "")

dir := must.NotFail(filepath.Abs(u.Opaque))
zap.S().Infof("Recreating %s", dir)
_ = os.Remove(dir)
must.NoError(os.MkdirAll(dir, 0o777))
}

if u := *targetURLF; u != "" {
client, err := makeClient(ctx, u)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions internal/util/testutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ var (
)

// DatabaseName returns a stable FerretDB database name for that test.
//
// It should be called only once per test.
func DatabaseName(tb testtb.TB) string {
tb.Helper()

Expand All @@ -61,8 +59,6 @@ func DatabaseName(tb testtb.TB) string {
}

// CollectionName returns a stable FerretDB collection name for that test.
//
// It should be called only once per test.
func CollectionName(tb testtb.TB) string {
tb.Helper()

Expand Down

0 comments on commit 58c898c

Please sign in to comment.