-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
53 lines (48 loc) · 932 Bytes
/
examples_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
package retry
import (
"fmt"
"time"
)
func ExampleRetryer() {
noSleepingInTests := func(_ time.Duration) time.Duration { return 0 }
fmt.Println("> Happy path")
i := 0
var err error
for r := New(3, noSleepingInTests); r.Next(err); {
i++
fmt.Println("Attempt", i)
err = nil
if i < 2 {
err = fmt.Errorf("connection error")
}
}
fmt.Println("Err:", err)
fmt.Println("> Unhappy path")
i = 0
for r := New(3, noSleepingInTests); r.Next(err); {
i++
fmt.Println("Attempt", i)
err = fmt.Errorf("connection error")
}
fmt.Println("Err:", err)
// Output: > Happy path
// Attempt 1
// Attempt 2
// Err: <nil>
// > Unhappy path
// Attempt 1
// Attempt 2
// Attempt 3
// Err: connection error
}
func ExampleExpDuration() {
df := ExpDuration(time.Millisecond)
for i := time.Duration(1); i <= 5; i++ {
fmt.Println(df(i).Round(time.Millisecond))
}
// Output: 1ms
// 3ms
// 7ms
// 20ms
// 55ms
}