-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_example_test.go
57 lines (48 loc) · 1.16 KB
/
backoff_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
// Copyright (c) 2019 Hervé Gouchet. All rights reserved.
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
package backoff_test
import (
"context"
"errors"
"fmt"
"time"
"github.com/rvflash/backoff"
)
func ExampleRetry() {
n, err := backoff.Retry(context.Background(), Task)
if err != nil {
fmt.Println("err:", n)
}
fmt.Println("n:", n)
// Output: Job done.
// n: 0
}
func ExampleRetryN() {
n, err := backoff.RetryN(context.Background(), 3, TaskInErr)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("n:", n)
// Output: err: backoff: maximum execution number exhausted: oops
// n: 3
}
func ExampleRetryUntil() {
n, err := backoff.RetryUntil(context.Background(), time.Now().Add(time.Second), TaskInErr)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("n:", n)
// Output: err: backoff: context deadline exceeded
// n: 2
}
// Task implements the backoff.Func interface.
func Task(context.Context) error {
fmt.Println("Job done.")
return nil
}
var errTask = errors.New("oops")
// Task implements the backoff.Func interface.
func TaskInErr(context.Context) error {
return errTask
}