|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "runtime"
|
9 |
| - "strconv" |
10 | 9 | "strings"
|
11 | 10 | "time"
|
12 | 11 |
|
@@ -144,41 +143,34 @@ func RemoveCRCHome(crcHome string) error {
|
144 | 143 | return fmt.Errorf("folder %s not removed as per request: %s present", crcHome, keepFile)
|
145 | 144 | }
|
146 | 145 |
|
147 |
| -// Based on the number of iterations for a given timeout in seconds the function returns the duration of echa loop |
148 |
| -// and the extra time in case required to complete the timeout |
149 |
| -func GetRetryParametersFromTimeoutInSeconds(iterations, timeout int) (time.Duration, time.Duration, error) { |
150 |
| - iterationDuration, err := |
151 |
| - time.ParseDuration(strconv.Itoa(timeout/iterations) + "s") |
152 |
| - if err != nil { |
153 |
| - return 0, 0, err |
154 |
| - } |
155 |
| - extraTime := timeout % iterations |
156 |
| - if extraTime != 0 { |
157 |
| - extraTimeDuration, err := |
158 |
| - time.ParseDuration(strconv.Itoa(extraTime) + "s") |
159 |
| - if err != nil { |
160 |
| - return 0, 0, err |
161 |
| - } |
162 |
| - return iterationDuration, extraTimeDuration, nil |
163 |
| - } |
164 |
| - return iterationDuration, 0, nil |
| 146 | +// MatchWithRetry will execute match function with expression as arg |
| 147 | +// for #iterations with a timeout |
| 148 | +func MatchWithRetry(expression string, match func(string) error, iterations, timeoutInSeconds int) error { |
| 149 | + return MatchRepetitionsWithRetry(expression, match, 1, iterations, timeoutInSeconds) |
165 | 150 | }
|
166 | 151 |
|
167 |
| -func MatchWithRetry(expression string, match func(string) error, retryCount, timeout int) error { |
168 |
| - iterationDuration, extraDuration, err := |
169 |
| - GetRetryParametersFromTimeoutInSeconds(retryCount, timeout) |
170 |
| - if err != nil { |
171 |
| - return err |
172 |
| - } |
173 |
| - for i := 0; i < retryCount; i++ { |
174 |
| - err := match(expression) |
175 |
| - if err == nil { |
176 |
| - return nil |
| 152 | +// MatchRepetitionsWithRetry will execute match function with expression as arg |
| 153 | +// for #iterations with a timeout, expression should be matched # matchRepetitions in a row |
| 154 | +func MatchRepetitionsWithRetry(expression string, match func(string) error, matchRepetitions int, iterations, timeoutInSeconds int) error { |
| 155 | + timeout := time.After(time.Duration(timeoutInSeconds) * time.Second) |
| 156 | + tick := time.NewTicker(time.Duration(timeoutInSeconds/iterations) * time.Second) |
| 157 | + matchRepetition := 0 |
| 158 | + for { |
| 159 | + select { |
| 160 | + case <-timeout: |
| 161 | + tick.Stop() |
| 162 | + return fmt.Errorf("not found: %s. Timeout", expression) |
| 163 | + case <-tick.C: |
| 164 | + if err := match(expression); err == nil { |
| 165 | + matchRepetition++ |
| 166 | + if matchRepetition == matchRepetitions { |
| 167 | + tick.Stop() |
| 168 | + return nil |
| 169 | + } |
| 170 | + } else { |
| 171 | + // repetions should be matched in a row, otherwise reset the counter |
| 172 | + matchRepetition = 0 |
| 173 | + } |
177 | 174 | }
|
178 |
| - time.Sleep(iterationDuration) |
179 |
| - } |
180 |
| - if extraDuration != 0 { |
181 |
| - time.Sleep(extraDuration) |
182 | 175 | }
|
183 |
| - return fmt.Errorf("not found: %s. Timeout", expression) |
184 | 176 | }
|
0 commit comments