-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
75 lines (64 loc) · 1.47 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
70
71
72
73
74
75
package gretry
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"time"
)
func TestBase(t *testing.T) {
r := New(WithRecovery(), WithBaseDelay(100*time.Millisecond))
err := r.EnsureRetryTimes(10, func() error {
fmt.Println(time.Now())
return CreateRetryErr(errors.New("haha"))
})
t.Log(err)
}
func TestBackoff(t *testing.T) {
bo := &Backoff{
MinDelay: time.Second,
MaxDelay: time.Second * 5,
Factor: 2,
}
r := New(WithRecovery(), WithBaseDelay(time.Second), WithBackoff(bo))
err := r.EnsureRetryTimes(5, func() error {
fmt.Println(time.Now())
return CreateRetryErr(errors.New("haha"))
})
t.Log(err)
}
func TestPanic(t *testing.T) {
r := New(WithRecovery())
err := r.Ensure(func() error {
panic("haha")
return nil
})
t.Log(err)
}
func TestContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
r := New(WithCtx(ctx), WithBaseDelay(time.Second))
err := r.Ensure(func() error {
t.Log(time.Now())
return CreateRetryErrMsg("haha")
})
if !reflect.DeepEqual(err, ctx.Err()) {
t.Errorf("got: %v, expect: %v", err, ctx.Err())
}
}
func TestContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second*2, func() {
cancel()
})
r := New(WithCtx(ctx))
err := r.Ensure(func() error {
t.Log(time.Now())
return CreateRetryErrMsg("haha")
})
if !reflect.DeepEqual(err, ctx.Err()) {
t.Errorf("got: %v, expect: %v", err, ctx.Err())
}
}