-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
143 lines (120 loc) · 2.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package lo
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAttempt(t *testing.T) {
is := assert.New(t)
err := fmt.Errorf("failed")
iter1, err1 := Attempt(42, func(i int) error {
return nil
})
iter2, err2 := Attempt(42, func(i int) error {
if i == 5 {
return nil
}
return err
})
iter3, err3 := Attempt(2, func(i int) error {
if i == 5 {
return nil
}
return err
})
iter4, err4 := Attempt(0, func(i int) error {
if i < 42 {
return err
}
return nil
})
is.Equal(iter1, 1)
is.Equal(err1, nil)
is.Equal(iter2, 6)
is.Equal(err2, nil)
is.Equal(iter3, 2)
is.Equal(err3, err)
is.Equal(iter4, 43)
is.Equal(err4, nil)
}
func TestAttemptWithDelay(t *testing.T) {
is := assert.New(t)
err := fmt.Errorf("failed")
iter1, dur1, err1 := AttemptWithDelay(42, 10*time.Millisecond, func(i int, d time.Duration) error {
return nil
})
iter2, dur2, err2 := AttemptWithDelay(42, 10*time.Millisecond, func(i int, d time.Duration) error {
if i == 5 {
return nil
}
return err
})
iter3, dur3, err3 := AttemptWithDelay(2, 10*time.Millisecond, func(i int, d time.Duration) error {
if i == 5 {
return nil
}
return err
})
iter4, dur4, err4 := AttemptWithDelay(0, 10*time.Millisecond, func(i int, d time.Duration) error {
if i < 10 {
return err
}
return nil
})
is.Equal(iter1, 1)
is.Greater(dur1, 0*time.Millisecond)
is.Less(dur1, 1*time.Millisecond)
is.Equal(err1, nil)
is.Equal(iter2, 6)
is.Greater(dur2, 50*time.Millisecond)
is.Less(dur2, 60*time.Millisecond)
is.Equal(err2, nil)
is.Equal(iter3, 2)
is.Greater(dur3, 10*time.Millisecond)
is.Less(dur3, 20*time.Millisecond)
is.Equal(err3, err)
is.Equal(iter4, 11)
is.Greater(dur4, 100*time.Millisecond)
is.Less(dur4, 115*time.Millisecond)
is.Equal(err4, nil)
}
func TestDebounce(t *testing.T) {
f1 := func() {
println("1. Called once after 10ms when func stopped invoking!")
}
f2 := func() {
println("2. Called once after 10ms when func stopped invoking!")
}
f3 := func() {
println("3. Called once after 10ms when func stopped invoking!")
}
d1, _ := NewDebounce(10*time.Millisecond, f1)
// execute 3 times
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
d1()
}
time.Sleep(20 * time.Millisecond)
}
d2, _ := NewDebounce(10*time.Millisecond, f2)
// execute once because it is always invoked and only last invoke is worked after 100ms
for i := 0; i < 3; i++ {
for j := 0; j < 5; j++ {
d2()
}
time.Sleep(5 * time.Millisecond)
}
time.Sleep(10 * time.Millisecond)
// execute once because it is canceled after 200ms.
d3, cancel := NewDebounce(10*time.Millisecond, f3)
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
d3()
}
time.Sleep(20 * time.Millisecond)
if i == 0 {
cancel()
}
}
}