diff --git a/wait/http.go b/wait/http.go index ff1031b041..77528f44b3 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: defaultPollInterval(), } } @@ -99,6 +101,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 +181,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 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 e10c79a4bb..aabd754e58 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: defaultPollInterval(), } } @@ -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) 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 +}