From 7623f539635d2cfd9eee73b69fec787fde77a347 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Tue, 16 Jun 2020 12:05:10 +0200 Subject: [PATCH 1/7] test for sql wait strategy 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 --- wait/sql.go | 22 +++++++++++++++++----- wait/sql_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 wait/sql_test.go diff --git a/wait/sql.go b/wait/sql.go index 698ccd7625..b98ebd4f3f 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -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, } } @@ -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) @@ -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(): diff --git a/wait/sql_test.go b/wait/sql_test.go new file mode 100644 index 0000000000..e9687b7242 --- /dev/null +++ b/wait/sql_test.go @@ -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) +} From 5d2ee472329d65845fdad78d7c191d294ade8486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 29 Sep 2022 18:53:19 +0200 Subject: [PATCH 2/7] fix: tabs --- wait/sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index 20f07af3df..ad2a35cbf8 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -90,7 +90,7 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) } defer db.Close() - db.SetConnMaxLifetime(0) + db.SetConnMaxLifetime(0) db.SetMaxIdleConns(3) db.SetMaxOpenConns(3) From 450b078afa3c2fa19c3be80f1de9574a2ce59526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 29 Sep 2022 18:53:55 +0200 Subject: [PATCH 3/7] fix: tabs --- wait/sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index ad2a35cbf8..ea2b35a559 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -17,7 +17,7 @@ func ForSQL(port nat.Port, driver string, url func(string, nat.Port) string) *wa Port: port, URL: url, Driver: driver, - // Not using the default duration here because it is too low. It will never work + // Not using the default duration here because it is too low. It will never work startupTimeout: defaultStartupTimeout(), PollInterval: defaultPollInterval(), query: defaultForSqlQuery, From 91896d45ae70f802d43420ed3884a8f0f358f4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 29 Sep 2022 18:54:53 +0200 Subject: [PATCH 4/7] fix: wrong resolve conflicts --- wait/sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index ea2b35a559..d9519accb7 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -18,7 +18,7 @@ func ForSQL(port nat.Port, driver string, url func(string, nat.Port) string) *wa URL: url, Driver: driver, // Not using the default duration here because it is too low. It will never work - startupTimeout: defaultStartupTimeout(), + startupTimeout: 20 * time.Second, PollInterval: defaultPollInterval(), query: defaultForSqlQuery, } From 18f86e47cf14bbf5217e60d1f32715308e8368ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 29 Sep 2022 18:55:48 +0200 Subject: [PATCH 5/7] fix: wrong resolve conflicts --- wait/sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index d9519accb7..57e0a43a6d 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -19,7 +19,7 @@ func ForSQL(port nat.Port, driver string, url func(string, nat.Port) string) *wa Driver: driver, // Not using the default duration here because it is too low. It will never work startupTimeout: 20 * time.Second, - PollInterval: defaultPollInterval(), + PollInterval: time.Second * 1, query: defaultForSqlQuery, } } From 57039329a1cbcb043cbe8acfc08f94e9ee3379b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 28 Nov 2022 13:04:29 +0100 Subject: [PATCH 6/7] fix: format --- wait/sql.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wait/sql.go b/wait/sql.go index 82719a8bca..3ca0da383d 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -17,9 +17,9 @@ const defaultForSqlQuery = "SELECT 1" // ForSQL constructs a new waitForSql strategy for the given driver func ForSQL(port nat.Port, driver string, url func(host string, port nat.Port) string) *waitForSql { return &waitForSql{ - Port: port, - URL: url, - Driver: driver, + 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, PollInterval: time.Second * 1, @@ -98,7 +98,7 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) } defer db.Close() - db.SetConnMaxLifetime(0) + db.SetConnMaxLifetime(0) db.SetMaxIdleConns(3) db.SetMaxOpenConns(3) From bb8ec0ef9da783443fb9767933824aca89b2b47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 28 Nov 2022 13:15:02 +0100 Subject: [PATCH 7/7] chore: remove extra settings for db tuning --- wait/sql.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/wait/sql.go b/wait/sql.go index 3ca0da383d..f4601dcc62 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -98,10 +98,6 @@ func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) } defer db.Close() - db.SetConnMaxLifetime(0) - db.SetMaxIdleConns(3) - db.SetMaxOpenConns(3) - for { select { case <-ctx.Done():