Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/eventually_test.go
4093 views
1
package util
2
3
import (
4
"testing"
5
"time"
6
7
"github.com/grafana/dskit/backoff"
8
"github.com/stretchr/testify/require"
9
)
10
11
func TestEventually(t *testing.T) {
12
var bcfg = backoff.Config{
13
MinBackoff: 10 * time.Millisecond,
14
MaxBackoff: 10 * time.Millisecond,
15
MaxRetries: 5,
16
}
17
18
t.Run("No errors", func(t *testing.T) {
19
EventuallyWithBackoff(t, func(t require.TestingT) {
20
require.True(t, true)
21
}, bcfg)
22
})
23
24
t.Run("Fails once", func(t *testing.T) {
25
var runs int
26
27
EventuallyWithBackoff(t, func(t require.TestingT) {
28
if runs > 0 {
29
return
30
}
31
runs++
32
33
require.True(t, false)
34
}, bcfg)
35
})
36
37
t.Run("Always fails", func(t *testing.T) {
38
var et eventuallyT
39
40
defer func() {
41
err := recover()
42
if err == nil {
43
require.Fail(t, "expected panic")
44
}
45
46
_, aborted := err.(testAbort)
47
if !aborted {
48
require.Fail(t, "expected test abort")
49
}
50
51
require.Len(t, et.errors, 1)
52
}()
53
54
EventuallyWithBackoff(&et, func(t require.TestingT) {
55
require.True(t, false)
56
}, bcfg)
57
})
58
}
59
60