From 2b9403ebd858d2806d3b8f63791986b3d2fcb473 Mon Sep 17 00:00:00 2001 From: Andre Soares Date: Thu, 21 May 2020 09:07:58 -0300 Subject: [PATCH 1/4] Add PollInterval to sql waiter --- wait/sql.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wait/sql.go b/wait/sql.go index e10c79a4bb..e632ea10d1 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -16,6 +16,7 @@ func ForSQL(port nat.Port, driver string, url func(nat.Port) string) *waitForSql URL: url, Driver: driver, startupTimeout: defaultStartupTimeout(), + PollInterval: 100 * time.Millisecond, } } @@ -24,6 +25,7 @@ type waitForSql struct { Driver string Port nat.Port startupTimeout time.Duration + PollInterval time.Duration } //Timeout sets the maximum waiting time for the strategy after which it'll give up and return an error @@ -32,13 +34,19 @@ func (w *waitForSql) Timeout(duration time.Duration) *waitForSql { return w } +//WithPollInterval can be used to override the default polling interval of 100 milliseconds +func (w *waitForSql) WithPollInterval(pollInterval time.Duration) *waitForSql { + w.PollInterval = pollInterval + return w +} + //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(w.PollInterval) defer ticker.Stop() port, err := target.MappedPort(ctx, w.Port) From 7d05d891f8bb2095bda8ed80299fd2f0489350fa Mon Sep 17 00:00:00 2001 From: Andre Soares Date: Fri, 18 Sep 2020 09:29:12 -0300 Subject: [PATCH 2/4] Add PollInterval to HttpStrategy --- wait/http.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/wait/http.go b/wait/http.go index ff1031b041..ff0d02cf1d 100644 --- a/wait/http.go +++ b/wait/http.go @@ -31,6 +31,7 @@ type HTTPStrategy struct { TLSConfig *tls.Config // TLS config for HTTPS Method string // http method Body io.Reader // http request body + PollInterval time.Duration } // NewHTTPStrategy constructs a HTTP strategy waiting on port 80 and status code 200 @@ -45,6 +46,7 @@ func NewHTTPStrategy(path string) *HTTPStrategy { TLSConfig: nil, Method: http.MethodGet, Body: nil, + PollInterval: 100 * time.Second, } } @@ -89,6 +91,7 @@ func (ws *HTTPStrategy) WithAllowInsecure(allowInsecure bool) *HTTPStrategy { return ws } + func (ws *HTTPStrategy) WithMethod(method string) *HTTPStrategy { ws.Method = method return ws @@ -99,6 +102,12 @@ func (ws *HTTPStrategy) WithBody(reqdata io.Reader) *HTTPStrategy { return ws } +// WithPollInterval can be used to override the default polling interval of 100 milliseconds +func (ws *HTTPStrategy) WithPollInterval(pollInterval time.Duration) *HTTPStrategy { + ws.PollInterval = pollInterval + return ws +} + // ForHTTP is a convenience method similar to Wait.java // https://github.com/testcontainers/testcontainers-java/blob/1d85a3834bd937f80aad3a4cec249c027f31aeb4/core/src/main/java/org/testcontainers/containers/wait/strategy/Wait.java func ForHTTP(path string) *HTTPStrategy { @@ -173,7 +182,7 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge select { case <-ctx.Done(): return ctx.Err() - case <-time.After(time.Second / 10): + case <-time.After(ws.PollInterval): req, err := http.NewRequestWithContext(ctx, ws.Method, endpoint, ws.Body) if err != nil { return err From 6a1560f4dd685935da817a45141286964b6a1224 Mon Sep 17 00:00:00 2001 From: Andre Soares Date: Fri, 18 Sep 2020 09:37:28 -0300 Subject: [PATCH 3/4] Use a tenth of a second as default interval --- wait/http.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wait/http.go b/wait/http.go index ff0d02cf1d..ba89dca62d 100644 --- a/wait/http.go +++ b/wait/http.go @@ -46,7 +46,7 @@ func NewHTTPStrategy(path string) *HTTPStrategy { TLSConfig: nil, Method: http.MethodGet, Body: nil, - PollInterval: 100 * time.Second, + PollInterval: time.Second / 10, } } @@ -91,7 +91,6 @@ func (ws *HTTPStrategy) WithAllowInsecure(allowInsecure bool) *HTTPStrategy { return ws } - func (ws *HTTPStrategy) WithMethod(method string) *HTTPStrategy { ws.Method = method return ws From 1a81473e30f1a59fa250eec85e3b4fa592dca9c1 Mon Sep 17 00:00:00 2001 From: Andre Soares Date: Fri, 18 Sep 2020 09:41:30 -0300 Subject: [PATCH 4/4] Add defaultPollInterval() --- wait/http.go | 2 +- wait/log.go | 4 ++-- wait/sql.go | 2 +- wait/wait.go | 4 ++++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/wait/http.go b/wait/http.go index ba89dca62d..77528f44b3 100644 --- a/wait/http.go +++ b/wait/http.go @@ -46,7 +46,7 @@ func NewHTTPStrategy(path string) *HTTPStrategy { TLSConfig: nil, Method: http.MethodGet, Body: nil, - PollInterval: time.Second / 10, + PollInterval: defaultPollInterval(), } } diff --git a/wait/log.go b/wait/log.go index 1173624423..8aa3050309 100644 --- a/wait/log.go +++ b/wait/log.go @@ -17,8 +17,8 @@ type LogStrategy struct { // additional properties Log string - PollInterval time.Duration Occurrence int + PollInterval time.Duration } // NewLogStrategy constructs a HTTP strategy waiting on port 80 and status code 200 @@ -26,8 +26,8 @@ func NewLogStrategy(log string) *LogStrategy { return &LogStrategy{ startupTimeout: defaultStartupTimeout(), Log: log, - PollInterval: 100 * time.Millisecond, Occurrence: 1, + PollInterval: defaultPollInterval(), } } diff --git a/wait/sql.go b/wait/sql.go index e632ea10d1..aabd754e58 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -16,7 +16,7 @@ func ForSQL(port nat.Port, driver string, url func(nat.Port) string) *waitForSql URL: url, Driver: driver, startupTimeout: defaultStartupTimeout(), - PollInterval: 100 * time.Millisecond, + PollInterval: defaultPollInterval(), } } diff --git a/wait/wait.go b/wait/wait.go index f71b9e999e..9439edeae6 100644 --- a/wait/wait.go +++ b/wait/wait.go @@ -22,3 +22,7 @@ type StrategyTarget interface { func defaultStartupTimeout() time.Duration { return 60 * time.Second } + +func defaultPollInterval() time.Duration { + return 100 * time.Millisecond +}