Skip to content

Commit

Permalink
Merge pull request #1727 from josephschorr/pg-retry
Browse files Browse the repository at this point in the history
Add a retry to PG connections to reduce test flakiness
  • Loading branch information
josephschorr authored Jan 30, 2024
2 parents d44c24a + 1269296 commit ee69f26
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions internal/testserver/datastore/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -127,11 +128,31 @@ func (b *postgresTester) NewDatabase(t testing.TB) string {
)
}

const (
retryCount = 3
timeBetweenRetries = 100 * time.Millisecond
)

func (b *postgresTester) NewDatastore(t testing.TB, initFunc InitFunc) datastore.Datastore {
connectStr := b.NewDatabase(t)

migrationDriver, err := pgmigrations.NewAlembicPostgresDriver(context.Background(), connectStr)
require.NoError(t, err)
var migrationDriver *pgmigrations.AlembicPostgresDriver
for i := 0; i < retryCount; i++ {
md, err := pgmigrations.NewAlembicPostgresDriver(context.Background(), connectStr)
if err == nil {
migrationDriver = md
break
}

if i == retryCount-1 {
require.NoError(t, err, "got error when trying to create migration driver")
}

time.Sleep(timeBetweenRetries)
}

require.NotNil(t, migrationDriver, "failed to create migration driver")

ctx := context.WithValue(context.Background(), migrate.BackfillBatchSize, uint64(1000))
require.NoError(t, pgmigrations.DatabaseMigrations.Run(ctx, migrationDriver, b.targetMigration, migrate.LiveRun))

Expand Down

0 comments on commit ee69f26

Please sign in to comment.