diff --git a/tests/db_config.go b/tests/db_config.go index b4d82a0..db6ffb3 100644 --- a/tests/db_config.go +++ b/tests/db_config.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "strings" + "time" "github.com/SaadAhmedGit/formify/internal/config" "github.com/SaadAhmedGit/formify/internal/models" @@ -35,7 +36,17 @@ func createConnection() (*sqlx.DB, error) { connStr := fmt.Sprintf("host=%s port=%d user=%s "+ "password=%s dbname=%s sslmode=disable", env.TEST_DB_HOST, env.TEST_DB_PORT, env.TEST_DB_USER, env.TEST_DB_PASSWORD, env.TEST_DB_NAME) - db, err := sqlx.Connect("postgres", connStr) + + // https://github.com/lib/pq/issues/835#issuecomment-1774313464 + var db *sqlx.DB + for i := 0; i < 20; i++ { + db, err = sqlx.Connect("postgres", connStr) + if err == nil { + break + } + + time.Sleep(500 * time.Millisecond) + } if err != nil { log.Printf("Failed to connect to database. %s\n", err.Error()) return db, err