Skip to content

Commit

Permalink
Merge pull request #200 from mniak/PollInterval
Browse files Browse the repository at this point in the history
Poll interval
  • Loading branch information
gianarb authored Sep 18, 2020
2 parents 27d8969 + 1a81473 commit 9d93888
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
10 changes: 9 additions & 1 deletion wait/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +46,7 @@ func NewHTTPStrategy(path string) *HTTPStrategy {
TLSConfig: nil,
Method: http.MethodGet,
Body: nil,
PollInterval: defaultPollInterval(),
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions wait/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ 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
func NewLogStrategy(log string) *LogStrategy {
return &LogStrategy{
startupTimeout: defaultStartupTimeout(),
Log: log,
PollInterval: 100 * time.Millisecond,
Occurrence: 1,
PollInterval: defaultPollInterval(),
}

}
Expand Down
10 changes: 9 additions & 1 deletion wait/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ type StrategyTarget interface {
func defaultStartupTimeout() time.Duration {
return 60 * time.Second
}

func defaultPollInterval() time.Duration {
return 100 * time.Millisecond
}

0 comments on commit 9d93888

Please sign in to comment.