@@ -11,6 +11,18 @@ type Eventually struct {
11
11
maxAttempts int
12
12
}
13
13
14
+ // New creates a new Eventually with the given options. This can be useful if you want to reuse the same
15
+ // configuration for multiple functions. For example:
16
+ //
17
+ // e := eventually.New(eventually.WithMaxAttempts(10))
18
+ //
19
+ // The returned Eventually has the following defaults unless otherwise specified:
20
+ //
21
+ // Timeout: 10 seconds
22
+ // Interval: 100 milliseconds
23
+ // MaxAttempts: 0 (unlimited)
24
+ //
25
+ // If you don't need to reuse the same configuration, you can use the [Must] and [Should] functions directly.
14
26
func New (options ... Option ) * Eventually {
15
27
e := & Eventually {
16
28
timeout : 10 * time .Second ,
@@ -25,13 +37,27 @@ func New(options ...Option) *Eventually {
25
37
return e
26
38
}
27
39
40
+ // Must will keep retrying the given function f until the testing.TB passed to
41
+ // it does not fail or one of the following conditions is met:
42
+ //
43
+ // - the timeout is reached
44
+ // - the maximum number of attempts is reached
45
+ //
46
+ // If f does not succed, Must will halt the test calling t.Fatalf.
28
47
func (e * Eventually ) Must (t testing.TB , f func (t testing.TB )) {
29
48
t .Helper ()
30
49
31
50
r := e .retryableT (t )
32
51
keepTrying (t , r , f , t .Fatalf )
33
52
}
34
53
54
+ // Should will keep retrying the given function f until the testing.TB passed to
55
+ // it does not fail or one of the following conditions is met:
56
+ //
57
+ // - the timeout is reached
58
+ // - the maximum number of attempts is reached
59
+ //
60
+ // If f does not succed, Should will fail the test calling t.Errorf.
35
61
func (e * Eventually ) Should (t testing.TB , f func (t testing.TB )) {
36
62
t .Helper ()
37
63
@@ -48,6 +74,60 @@ func (e *Eventually) retryableT(t testing.TB) *retryableT {
48
74
}
49
75
}
50
76
77
+ // Option is a function that can be used to configure an Eventually.
78
+ type Option func (* Eventually )
79
+
80
+ // WithTimeout sets the timeout for an Eventually.
81
+ func WithTimeout (timeout time.Duration ) Option {
82
+ return func (e * Eventually ) {
83
+ e .timeout = timeout
84
+ }
85
+ }
86
+
87
+ // WithInterval sets the interval Eventually will wait between attempts.
88
+ func WithInterval (interval time.Duration ) Option {
89
+ return func (e * Eventually ) {
90
+ e .interval = interval
91
+ }
92
+ }
93
+
94
+ // WithMaxAttempts sets the maximum number of attempts an Eventually will make.
95
+ func WithMaxAttempts (attempts int ) Option {
96
+ return func (e * Eventually ) {
97
+ e .maxAttempts = attempts
98
+ }
99
+ }
100
+
101
+ // Must will keep retrying the given function f until the testing.TB passed to
102
+ // it does not fail or one of the following conditions is met:
103
+ //
104
+ // - the timeout is reached
105
+ // - the maximum number of attempts is reached
106
+ //
107
+ // If f does not succed, Must will halt the test calling t.Fatalf.
108
+ // Must behaviour can be changed by passing options to it.
109
+ func Must (t testing.TB , f func (t testing.TB ), options ... Option ) {
110
+ t .Helper ()
111
+
112
+ e := New (options ... )
113
+ e .Must (t , f )
114
+ }
115
+
116
+ // Should will keep retrying the given function f until the testing.TB passed to
117
+ // it does not fail or one of the following conditions is met:
118
+ //
119
+ // - the timeout is reached
120
+ // - the maximum number of attempts is reached
121
+ //
122
+ // If f does not succed, Should will fail the test calling t.Errorf.
123
+ // Should behaviour can be changed by passing options to it.
124
+ func Should (t testing.TB , f func (t testing.TB ), options ... Option ) {
125
+ t .Helper ()
126
+
127
+ e := New (options ... )
128
+ e .Should (t , f )
129
+ }
130
+
51
131
type failNowPanic struct {}
52
132
53
133
type retryableT struct {
@@ -99,40 +179,6 @@ func (r *retryableT) Fatalf(format string, args ...any) {
99
179
r .FailNow ()
100
180
}
101
181
102
- type Option func (* Eventually )
103
-
104
- func WithTimeout (timeout time.Duration ) Option {
105
- return func (e * Eventually ) {
106
- e .timeout = timeout
107
- }
108
- }
109
-
110
- func WithInterval (interval time.Duration ) Option {
111
- return func (e * Eventually ) {
112
- e .interval = interval
113
- }
114
- }
115
-
116
- func WithMaxAttempts (attempts int ) Option {
117
- return func (e * Eventually ) {
118
- e .maxAttempts = attempts
119
- }
120
- }
121
-
122
- func Must (t testing.TB , f func (t testing.TB ), options ... Option ) {
123
- t .Helper ()
124
-
125
- e := New (options ... )
126
- e .Must (t , f )
127
- }
128
-
129
- func Should (t testing.TB , f func (t testing.TB ), options ... Option ) {
130
- t .Helper ()
131
-
132
- e := New (options ... )
133
- e .Should (t , f )
134
- }
135
-
136
182
func keepTrying (t testing.TB , retryable * retryableT , f func (t testing.TB ), failf func (format string , args ... any )) {
137
183
t .Helper ()
138
184
0 commit comments