Skip to content

Commit

Permalink
fix: data race in dockertest (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Dec 4, 2024
1 parent 640d0e5 commit a6cf140
Showing 1 changed file with 18 additions and 38 deletions.
56 changes: 18 additions & 38 deletions sqlcon/dockertest/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,37 @@ import (
"github.com/ory/x/stringsx"
)

// atexit := atexit.NewOnExit()
// atexit.Add(func() {
// dockertest.KillAll()
// })
// atexit.Exit(testMain(m))

// func WrapCleanup

type dockerPool interface {
Purge(r *dockertest.Resource) error
Run(repository, tag string, env []string) (*dockertest.Resource, error)
RunWithOptions(opts *dockertest.RunOptions, hcOpts ...func(*dc.HostConfig)) (*dockertest.Resource, error)
}

var (
resources = []*dockertest.Resource{}
pool dockerPool
resources []*dockertest.Resource
mux sync.Mutex
)

func getPool() (dockerPool, error) {
if pool != nil {
return pool, nil
}
func init() {
var err error
pool, err = dockertest.NewPool("")
return pool, err
}

// KillAllTestDatabases deletes all test databases.
func KillAllTestDatabases() {
pool, err := getPool()
if err != nil {
panic(err)
}
}

// KillAllTestDatabases deletes all test databases.
func KillAllTestDatabases() {
mux.Lock()
defer mux.Unlock()
for _, r := range resources {
if err := pool.Purge(r); err != nil {
log.Printf("Failed to purge resource: %s", err)
}
}

resources = []*dockertest.Resource{}
resources = nil
}

// Register sets up OnExit.
Expand Down Expand Up @@ -154,14 +143,11 @@ func ConnectPop(t require.TestingT, url string) (c *pop.Connection) {
// ## PostgreSQL ##

func startPostgreSQL(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.Run("postgres", stringsx.Coalesce(version, "11.8"), []string{"PGUSER=postgres", "POSTGRES_PASSWORD=secret", "POSTGRES_DB=postgres"})
resource, err := pool.Run("postgres", stringsx.Coalesce(version, "16"), []string{"PGUSER=postgres", "POSTGRES_PASSWORD=secret", "POSTGRES_DB=postgres"})
if err == nil {
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
}
return resource, err
}
Expand Down Expand Up @@ -235,14 +221,9 @@ func ConnectToTestPostgreSQLPop(t testing.TB) *pop.Connection {
// ## MySQL ##

func startMySQL(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "mysql",
Tag: stringsx.Coalesce(version, "8.0.26"),
Tag: stringsx.Coalesce(version, "8.0"),
Env: []string{
"MYSQL_ROOT_PASSWORD=secret",
"MYSQL_ROOT_HOST=%",
Expand All @@ -251,7 +232,9 @@ func startMySQL(version string) (*dockertest.Resource, error) {
if err != nil {
return nil, err
}
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
return resource, nil
}

Expand Down Expand Up @@ -327,18 +310,15 @@ func ConnectToTestMySQLPop(t testing.TB) *pop.Connection {
// ## CockroachDB

func startCockroachDB(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "cockroachdb/cockroach",
Tag: stringsx.Coalesce(version, "v20.2.5"),
Tag: stringsx.Coalesce(version, "latest-v24.2"),
Cmd: []string{"start-single-node", "--insecure"},
})
if err == nil {
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
}
return resource, err
}
Expand Down

0 comments on commit a6cf140

Please sign in to comment.