Skip to content

Commit

Permalink
Implement waitFor pattern instead of straight up sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Mar 12, 2019
1 parent 829daeb commit 8d8aabf
Showing 1 changed file with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,47 @@ func createCCRStats(host string) error {
}

// Give ES sufficient time to do the replication and produce stats
time.Sleep(300 * time.Millisecond)
checkCCRStats := func() (bool, error) {
return checkCCRStatsExists(host)
}

exists, err := waitForSuccess(checkCCRStats, 200, 5)
if err != nil {
return errors.Wrap(err, "error checking if CCR stats exist")
}

if !exists {
return fmt.Errorf("expected to find CCR stats but not found")
}

return nil
}

func checkCCRStatsExists(host string) (bool, error) {
resp, err := http.Get("http://" + host + "/_ccr/stats")
if err != nil {
return false, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}

var data struct {
FollowStats struct {
Indices []map[string]interface{} `json:"indices"`
} `json:"follow_stats"`
}
err = json.Unmarshal(body, &data)
if err != nil {
return false, err
}

return len(data.FollowStats.Indices) > 0, nil
}

func setupCCRRemote(host string) error {
remoteSettings, err := ioutil.ReadFile("ccr/_meta/test/test_remote_settings.json")
if err != nil {
Expand Down Expand Up @@ -368,3 +404,23 @@ func httpPutJSON(host, path string, body []byte) ([]byte, *http.Response, error)

return body, resp, nil
}

type checkSuccessFunction func() (bool, error)

func waitForSuccess(f checkSuccessFunction, retryIntervalMs time.Duration, numAttempts int) (bool, error) {
for numAttempts > 0 {
success, err := f()
if err != nil {
return false, err
}

if success {
return success, nil
}

time.Sleep(retryIntervalMs * time.Millisecond)
numAttempts--
}

return false, nil
}

0 comments on commit 8d8aabf

Please sign in to comment.