Skip to content

Commit

Permalink
fix: pgtesting parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Apr 4, 2024
1 parent 83ec52b commit ac3f5c4
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 41 deletions.
347 changes: 347 additions & 0 deletions components/fctl/membershipclient/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/payments/cmd/api/internal/storage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func TestMain(m *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestMain(m *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/auth/pkg/api/authorization/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Errorf("Unable to start postgres server: %s", err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/auth/pkg/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Errorf("Unable to start postgres server: %s", err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/auth/pkg/oidc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Errorf("Unable to start postgres server: %s", err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/orchestration/internal/api/v1/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()

if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/orchestration/internal/api/v2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()

if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/orchestration/internal/storage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/orchestration/internal/triggers/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()

if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/orchestration/internal/workflow/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()

if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/webhooks/pkg/storage/postgres/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion libs/go-libs/bun/bunmigrate/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestRunMigrate(t *testing.T) {
require.NoError(t, pgtesting.CreatePostgresServer())
require.NoError(t, pgtesting.CreateGlobalPostgresServer())
t.Cleanup(func() {
require.NoError(t, pgtesting.DestroyPostgresServer())
})
Expand Down
2 changes: 1 addition & 1 deletion libs/go-libs/bun/bunpaginate/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(m *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
if err := pgtesting.CreateGlobalPostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion libs/go-libs/migrations/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestMigrations(t *testing.T) {
require.NoError(t, pgtesting.CreatePostgresServer())
require.NoError(t, pgtesting.CreateGlobalPostgresServer())
t.Cleanup(func() {
require.NoError(t, pgtesting.DestroyPostgresServer())
})
Expand Down
62 changes: 36 additions & 26 deletions libs/go-libs/pgtesting/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
sharedlogging "github.com/formancehq/stack/libs/go-libs/logging"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -40,57 +41,57 @@ func (s *pgDatabase) ConnectionOptions() bunconnect.ConnectionOptions {
}
}

type pgServer struct {
type PostgresServer struct {
destroy func() error
lock sync.Mutex
db *sql.DB
port string
config config
}

func (s *pgServer) GetPort() int {
func (s *PostgresServer) GetPort() int {
v, err := strconv.ParseInt(s.port, 10, 64)
if err != nil {
panic(err)
}
return int(v)
}

func (s *pgServer) GetHost() string {
return "localhost"
func (s *PostgresServer) GetHost() string {
return "127.0.0.1"
}

func (s *pgServer) GetUsername() string {
func (s *PostgresServer) GetUsername() string {
return s.config.initialUsername
}

func (s *pgServer) GetPassword() string {
func (s *PostgresServer) GetPassword() string {
return s.config.initialUserPassword
}

func (s *pgServer) GetDSN() string {
func (s *PostgresServer) GetDSN() string {
return s.GetDatabaseDSN(s.config.initialDatabaseName)
}

func (s *pgServer) GetDatabaseDSN(databaseName string) string {
return fmt.Sprintf("postgresql://%s:%s@localhost:%s/%s?sslmode=disable", s.config.initialUsername,
s.config.initialUserPassword, s.port, databaseName)
func (s *PostgresServer) GetDatabaseDSN(databaseName string) string {
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", s.config.initialUsername,
s.config.initialUserPassword, s.GetHost(), s.port, databaseName)
}

func (s *pgServer) NewDatabase(t TestingT) *pgDatabase {
func (s *PostgresServer) NewDatabase(t TestingT) *pgDatabase {
s.lock.Lock()
defer s.lock.Unlock()

databaseName := uuid.NewString()
_, err := s.db.ExecContext(context.Background(), fmt.Sprintf(`CREATE DATABASE "%s"`, databaseName))
_, err := s.db.ExecContext(sharedlogging.TestingContext(), fmt.Sprintf(`CREATE DATABASE "%s"`, databaseName))
require.NoError(t, err)

if os.Getenv("NO_CLEANUP") != "true" {
t.Cleanup(func() {
s.lock.Lock()
defer s.lock.Unlock()

_, err := s.db.ExecContext(context.Background(), fmt.Sprintf(`DROP DATABASE "%s"`, databaseName))
_, err := s.db.ExecContext(sharedlogging.TestingContext(), fmt.Sprintf(`DROP DATABASE "%s"`, databaseName))
if err != nil {
panic(err)
}
Expand All @@ -102,7 +103,7 @@ func (s *pgServer) NewDatabase(t TestingT) *pgDatabase {
}
}

func (s *pgServer) Close() error {
func (s *PostgresServer) Close() error {
if s.db == nil {
return nil
}
Expand All @@ -118,9 +119,9 @@ func (s *pgServer) Close() error {
return nil
}

var srv *pgServer
var srv *PostgresServer

func Server() *pgServer {
func Server() *PostgresServer {
return srv
}

Expand Down Expand Up @@ -205,19 +206,29 @@ var defaultOptions = []option{
WithContext(context.Background()),
}

func CreatePostgresServer(opts ...option) error {
func CreateGlobalPostgresServer(opts ...option) error {
var err error
srv, err = CreatePostgresServer(opts...)
if err != nil {
return err
}

return nil
}

func CreatePostgresServer(opts ...option) (*PostgresServer, error) {
cfg := config{}
for _, opt := range append(defaultOptions, opts...) {
opt(&cfg)
}

if err := cfg.validate(); err != nil {
return errors.Wrap(err, "validating config")
return nil, errors.Wrap(err, "validating config")
}

pool, err := dockertest.NewPool("")
if err != nil {
return errors.Wrap(err, "unable to start docker containers pool")
return nil, errors.Wrap(err, "unable to start docker containers pool")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Expand All @@ -228,7 +239,6 @@ func CreatePostgresServer(opts ...option) error {
fmt.Sprintf("POSTGRES_PASSWORD=%s", cfg.initialUserPassword),
fmt.Sprintf("POSTGRES_DB=%s", cfg.initialDatabaseName),
},
Entrypoint: nil,
Cmd: []string{
"-c", "superuser-reserved-connections=0",
"-c", "enable_partition_pruning=on",
Expand All @@ -237,7 +247,7 @@ func CreatePostgresServer(opts ...option) error {
},
}, cfg.hostConfigOptions...)
if err != nil {
return errors.Wrap(err, "unable to start postgres server container")
return nil, errors.Wrap(err, "unable to start postgres server container")
}

go func() {
Expand All @@ -253,7 +263,7 @@ func CreatePostgresServer(opts ...option) error {
}
}()

srv = &pgServer{
srv := &PostgresServer{
port: resource.GetPort("5432/tcp"),
destroy: func() error {
return pool.Purge(resource)
Expand All @@ -264,7 +274,7 @@ func CreatePostgresServer(opts ...option) error {
try := time.Duration(0)
srv.db, err = sql.Open("postgres", srv.GetDatabaseDSN(cfg.initialDatabaseName))
if err != nil {
return err
return nil, err
}

for try*cfg.statusCheckInterval < cfg.maximumWaitingTime {
Expand All @@ -273,14 +283,14 @@ func CreatePostgresServer(opts ...option) error {
try++
select {
case <-cfg.context.Done():
return cfg.context.Err()
return nil, cfg.context.Err()
case <-time.After(cfg.statusCheckInterval):
}
continue
}

return nil
return srv, nil
}

return errors.New("timeout waiting for server ready")
return nil, errors.New("timeout waiting for server ready")
}
2 changes: 1 addition & 1 deletion libs/go-libs/pgtesting/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestMain(m *testing.M) {
if err := CreatePostgresServer(); err != nil {
if err := CreateGlobalPostgresServer(); err != nil {
log.Fatal(err)
}
code := m.Run()
Expand Down

0 comments on commit ac3f5c4

Please sign in to comment.