Skip to content

Commit

Permalink
test for sql wait strategy
Browse files Browse the repository at this point in the history
The wait.ForSQL wait strategy does not have a testcase and I forgot
about how to use it as well.

I decided to write it in preparation for the work/discussion ongoing
here: #166
  • Loading branch information
Gianluca Arbezzano committed Jun 16, 2020
1 parent 09610fb commit 7623f53
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
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)
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) {
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)
}

0 comments on commit 7623f53

Please sign in to comment.