Skip to content

Commit af9afbe

Browse files
adrianriobopraveenkumar
authored andcommitted
[e2e] added repetitions for MatchWithRetry
this ensures certain level of stability on the cluster state; now checking the state of the cluster the state should be matched a number of times in a row during the amount of time. This will remove states when for a small period the cluster is running but then is degraded right away
1 parent 7a7592a commit af9afbe

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

test/extended/crc/cmd/cmd.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212

1313
const (
1414
// timeout to wait for cluster to change its state
15-
clusterStateRetryCount = 15
16-
clusterStateTimeout = 600
15+
clusterStateRetryCount = 25
16+
clusterStateTimeout = 600
17+
// defines the number of times the state should be matches in a row
18+
clusterStateRepetition = 3
1719
CRCExecutableInstalled = "installed"
1820
CRCExecutableNotInstalled = "notInstalled"
1921
)
@@ -119,7 +121,7 @@ func UnsetConfigPropertySucceedsOrFails(property string, expected string) error
119121
}
120122

121123
func WaitForClusterInState(state string) error {
122-
return util.MatchWithRetry(state, CheckCRCStatus,
124+
return util.MatchRepetitionsWithRetry(state, CheckCRCStatus, clusterStateRepetition,
123125
clusterStateRetryCount, clusterStateTimeout)
124126
}
125127

test/extended/util/util.go

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"path/filepath"
88
"runtime"
9-
"strconv"
109
"strings"
1110
"time"
1211

@@ -144,41 +143,34 @@ func RemoveCRCHome(crcHome string) error {
144143
return fmt.Errorf("folder %s not removed as per request: %s present", crcHome, keepFile)
145144
}
146145

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)
165150
}
166151

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+
}
177174
}
178-
time.Sleep(iterationDuration)
179-
}
180-
if extraDuration != 0 {
181-
time.Sleep(extraDuration)
182175
}
183-
return fmt.Errorf("not found: %s. Timeout", expression)
184176
}

0 commit comments

Comments
 (0)