-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
64 lines (56 loc) · 994 Bytes
/
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
package iters
import (
"context"
"fmt"
"time"
)
func ExampleRetry() {
start := time.Now()
for attempt, delay := range Retry(context.Background(), Trim(Repeat(time.Millisecond*100), 10)) {
fmt.Println(attempt, delay)
}
fmt.Println(time.Since(start) > time.Second)
// Output:
// 0 0s
// 1 100ms
// 2 100ms
// 3 100ms
// 4 100ms
// 5 100ms
// 6 100ms
// 7 100ms
// 8 100ms
// 9 100ms
// 10 100ms
// true
}
func ExampleRetry_break() {
for attempt, delay := range Retry(context.Background(), Exponential(time.Millisecond, time.Second, 2)) {
fmt.Println(attempt, delay)
if attempt == 5 {
break
}
}
// Output:
// 0 0s
// 1 1ms
// 2 2ms
// 3 4ms
// 4 8ms
// 5 16ms
}
func ExampleRetry_ctx() {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
for range Retry(ctx, Repeat(time.Millisecond*100)) {
fmt.Print()
}
fmt.Println("stopped")
close(done)
}()
cancel()
<-done
// Output:
// stopped
}