Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for sql wait strategy #214

Closed
wants to merge 10 commits into from
22 changes: 17 additions & 5 deletions wait/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"github.com/docker/go-connections/nat"
)

var _ Strategy = (*HTTPStrategy)(nil)

//ForSQL constructs a new waitForSql strategy for the given driver
func ForSQL(port nat.Port, driver string, url func(nat.Port) string) *waitForSql {
return &waitForSql{
Port: port,
URL: url,
Driver: driver,
startupTimeout: defaultStartupTimeout(),
Port: port,
URL: url,
Driver: driver,
// Not using the default duration here because it is too low. It will never work
startupTimeout: 20 * time.Second,
}
}

Expand All @@ -27,18 +30,24 @@ type waitForSql struct {
}

//Timeout sets the maximum waiting time for the strategy after which it'll give up and return an error
//Deprecated: uses WaitStartupTimeout instead
func (w *waitForSql) Timeout(duration time.Duration) *waitForSql {
w.startupTimeout = duration
return w
}

func (ws *waitForSql) WithStartupTimeout(startupTimeout time.Duration) *waitForSql {
ws.startupTimeout = startupTimeout
return ws
}

//WaitUntilReady repeatedly tries to run "SELECT 1" query on the given port using sql and driver.
// If the it doesn't succeed until the timeout value which defaults to 60 seconds, it will return an error
func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) {
ctx, cancel := context.WithTimeout(ctx, w.startupTimeout)
defer cancel()

ticker := time.NewTicker(time.Millisecond * 100)
ticker := time.NewTicker(time.Second * 1)
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
defer ticker.Stop()

port, err := target.MappedPort(ctx, w.Port)
Expand All @@ -50,6 +59,9 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget)
if err != nil {
return fmt.Errorf("sql.Open: %v", err)
}
db.SetConnMaxLifetime(0)
db.SetMaxIdleConns(3)
db.SetMaxOpenConns(3)
for {
select {
case <-ctx.Done():
Expand Down
34 changes: 34 additions & 0 deletions wait/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wait_test

import (
"context"
"testing"

"github.com/docker/go-connections/nat"
_ "github.com/go-sql-driver/mysql"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

func Test_ForSql(t *testing.T) {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "mysql:8.0.20",
ExposedPorts: []string{"3306/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "root",
"MYSQL_DATABASE": "db",
},
WaitingFor: wait.ForSQL("3306/tcp", "mysql", func(p nat.Port) string {
return "root:root@tcp(localhost:" + p.Port() + ")/db"
}),
},
Started: true,
}
c, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
t.Fatal(err)
}
defer c.Terminate(ctx)
}