/
try_test.go
106 lines (99 loc) 路 2.24 KB
/
try_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// https://medium.com/@matryer/retrying-in-golang-quicktip-f688d00e650a#.ylrrg0mn3
// https://github.com/matryer/try/blob/master/try_test.go
package epazote
import (
"errors"
"fmt"
"log"
"testing"
)
func TestTryExample(t *testing.T) {
SomeFunction := func() (string, error) {
return "", nil
}
err := Try(func(attempt int) (bool, error) {
var err error
_, err = SomeFunction()
return attempt < 5, err // try 5 times
})
if err != nil {
log.Fatalln("error:", err)
}
}
func TestTryExamplePanic(t *testing.T) {
SomeFunction := func() (string, error) {
panic("test panic")
}
err := Try(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
_, err = SomeFunction()
return
})
if err.Error() != "panic: test panic" {
t.Errorf("Expecting: %s, got: %s", "panic: test panic", err.Error())
}
}
func TestTryDoSuccessful(t *testing.T) {
callCount := 0
err := Try(func(attempt int) (bool, error) {
callCount++
return attempt < 5, nil
})
if err != nil {
t.Error(err)
}
if callCount != 1 {
t.Error("Expecting callcount = 1")
}
}
func TestTryDoFailed(t *testing.T) {
theErr := errors.New("something went wrong")
callCount := 0
err := Try(func(attempt int) (bool, error) {
callCount++
return attempt < 5, theErr
})
if err.Error() != theErr.Error() {
t.Errorf("Expecting: %s Got: %s", theErr.Error(), err.Error())
}
if callCount != 5 {
t.Error("Expecting callCount to be 5")
}
}
func TestTryPanics(t *testing.T) {
theErr := errors.New("something went wrong")
callCount := 0
err := Try(func(attempt int) (retry bool, err error) {
retry = attempt < 5
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
callCount++
if attempt > 2 {
panic("I don't like three")
}
err = theErr
return
})
if err.Error() != "panic: I don't like three" {
t.Errorf("Expecting: %s Got: %s", "panic: I don't like three", err.Error())
}
if callCount != 5 {
t.Error("Expecting callCount to be 5")
}
}
func TestRetryLimit(t *testing.T) {
err := Try(func(attempt int) (bool, error) {
return true, errors.New("nope")
})
if err == nil {
t.Error("Expecting an error")
}
}