-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_examples_test.go
More file actions
77 lines (63 loc) · 1.48 KB
/
runner_examples_test.go
File metadata and controls
77 lines (63 loc) · 1.48 KB
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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package retry
import (
"context"
"errors"
"fmt"
"time"
)
func ExampleRunner_never() {
onAttempt := func(a Attempt[int]) {
fmt.Println("Attempt.Result", a.Result, "Attempt.Next", a.Next)
}
// no Config or PolicyFactory means this runner will never
// retry anything
runner, _ := NewRunner[int](
WithOnAttempt(onAttempt),
)
result, _ := runner.Run(
context.Background(),
AddValue(1234, func() error {
fmt.Println("executing task ...")
return nil
}),
)
fmt.Println("Run result", result)
// Output:
// executing task ...
// Attempt.Result 1234 Attempt.Next 0s
// Run result 1234
}
func ExampleRunner_constant() {
onAttempt := func(a Attempt[int]) {
fmt.Println("Attempt.Result", a.Result, "Attempt.Next", a.Next)
}
runner, _ := NewRunner(
WithOnAttempt(onAttempt),
WithPolicyFactory[int](Config{
Interval: 10 * time.Millisecond,
}),
)
attempts := 0
result, _ := runner.Run(
context.Background(),
func(_ context.Context) (int, error) {
fmt.Println("executing task ...")
attempts++
if attempts < 3 {
return -1, errors.New("task error")
}
return 1234, nil
},
)
fmt.Println("Run result", result)
// Output:
// executing task ...
// Attempt.Result -1 Attempt.Next 10ms
// executing task ...
// Attempt.Result -1 Attempt.Next 10ms
// executing task ...
// Attempt.Result 1234 Attempt.Next 0s
// Run result 1234
}