-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_example_test.go
89 lines (69 loc) · 1.37 KB
/
retry_example_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
//go:build !race
// +build !race
package lo
import (
"fmt"
"sync"
"time"
)
func ExampleNewDebounce() {
i := 0
calls := []int{}
mu := sync.Mutex{}
debounce, cancel := NewDebounce(time.Millisecond, func() {
mu.Lock()
defer mu.Unlock()
calls = append(calls, i)
})
debounce()
i++
time.Sleep(5 * time.Millisecond)
debounce()
i++
debounce()
i++
debounce()
i++
time.Sleep(5 * time.Millisecond)
cancel()
fmt.Printf("%v", calls)
// Output: [1 4]
}
func ExampleAttempt() {
count1, err1 := Attempt(2, func(i int) error {
if i == 0 {
return fmt.Errorf("error")
}
return nil
})
count2, err2 := Attempt(2, func(i int) error {
if i < 10 {
return fmt.Errorf("error")
}
return nil
})
fmt.Printf("%v %v\n", count1, err1)
fmt.Printf("%v %v\n", count2, err2)
// Output:
// 2 <nil>
// 2 error
}
func ExampleAttemptWithDelay() {
count1, time1, err1 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
if i == 0 {
return fmt.Errorf("error")
}
return nil
})
count2, time2, err2 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
if i < 10 {
return fmt.Errorf("error")
}
return nil
})
fmt.Printf("%v %v %v\n", count1, time1.Truncate(time.Millisecond), err1)
fmt.Printf("%v %v %v\n", count2, time2.Truncate(time.Millisecond), err2)
// Output:
// 2 1ms <nil>
// 2 1ms error
}