generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretry_test.go
69 lines (56 loc) · 1.26 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package backoff_test
import (
"context"
"errors"
"time"
. "github.com/dogmatiq/linger/backoff"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("func Retry()", func() {
It("returns nil if the function succeeds", func() {
count := 0
n, err := Retry(
context.Background(),
Constant(1*time.Nanosecond),
func(context.Context) error {
if count == 3 {
return nil
}
count++
return errors.New("<error>")
},
)
Expect(err).ShouldNot(HaveOccurred())
Expect(n).To(BeNumerically("==", 3))
})
It("returns an error if the context is canceled", func() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
n, err := Retry(
ctx,
Linear(10*time.Millisecond),
func(context.Context) error {
return errors.New("<error>")
},
)
Expect(err).To(Equal(context.DeadlineExceeded))
Expect(n).To(BeNumerically("==", 1))
})
It("uses the default strategy if none is provided", func() {
count := 0
n, err := Retry(
context.Background(),
nil,
func(context.Context) error {
if count == 1 {
return nil
}
count++
return errors.New("<error>")
},
)
Expect(err).ShouldNot(HaveOccurred())
Expect(n).To(BeNumerically("==", 1))
})
})