Skip to content

Commit 455793b

Browse files
committed
Add documentation
1 parent 0b20ff8 commit 455793b

File tree

1 file changed

+80
-34
lines changed

1 file changed

+80
-34
lines changed

eventually.go

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ type Eventually struct {
1111
maxAttempts int
1212
}
1313

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.
1426
func New(options ...Option) *Eventually {
1527
e := &Eventually{
1628
timeout: 10 * time.Second,
@@ -25,13 +37,27 @@ func New(options ...Option) *Eventually {
2537
return e
2638
}
2739

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.
2847
func (e *Eventually) Must(t testing.TB, f func(t testing.TB)) {
2948
t.Helper()
3049

3150
r := e.retryableT(t)
3251
keepTrying(t, r, f, t.Fatalf)
3352
}
3453

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.
3561
func (e *Eventually) Should(t testing.TB, f func(t testing.TB)) {
3662
t.Helper()
3763

@@ -48,6 +74,60 @@ func (e *Eventually) retryableT(t testing.TB) *retryableT {
4874
}
4975
}
5076

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+
51131
type failNowPanic struct{}
52132

53133
type retryableT struct {
@@ -99,40 +179,6 @@ func (r *retryableT) Fatalf(format string, args ...any) {
99179
r.FailNow()
100180
}
101181

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-
136182
func keepTrying(t testing.TB, retryable *retryableT, f func(t testing.TB), failf func(format string, args ...any)) {
137183
t.Helper()
138184

0 commit comments

Comments
 (0)