/usr/share/gocode/src/gopkg.in/eapache/go-resiliency.v1/retrier/retrier_test.go is in golang-gopkg-eapache-go-resiliency.v1-dev 0.0~git20150213.0.6800482-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | package retrier
import (
"testing"
"time"
)
var i int
func genWork(returns []error) func() error {
i = 0
return func() error {
i++
if i > len(returns) {
return nil
}
return returns[i-1]
}
}
func TestRetrier(t *testing.T) {
r := New([]time.Duration{0, 10 * time.Millisecond}, WhitelistClassifier{errFoo})
err := r.Run(genWork([]error{errFoo, errFoo}))
if err != nil {
t.Error(err)
}
if i != 3 {
t.Error("run wrong number of times")
}
err = r.Run(genWork([]error{errFoo, errBar}))
if err != errBar {
t.Error(err)
}
if i != 2 {
t.Error("run wrong number of times")
}
err = r.Run(genWork([]error{errBar, errBaz}))
if err != errBar {
t.Error(err)
}
if i != 1 {
t.Error("run wrong number of times")
}
}
func TestRetrierNone(t *testing.T) {
r := New(nil, nil)
i = 0
err := r.Run(func() error {
i++
return errFoo
})
if err != errFoo {
t.Error(err)
}
if i != 1 {
t.Error("run wrong number of times")
}
i = 0
err = r.Run(func() error {
i++
return nil
})
if err != nil {
t.Error(err)
}
if i != 1 {
t.Error("run wrong number of times")
}
}
func TestRetrierJitter(t *testing.T) {
r := New([]time.Duration{0, 10 * time.Millisecond, 4 * time.Hour}, nil)
if r.calcSleep(0) != 0 {
t.Error("Incorrect sleep calculated")
}
if r.calcSleep(1) != 10*time.Millisecond {
t.Error("Incorrect sleep calculated")
}
if r.calcSleep(2) != 4*time.Hour {
t.Error("Incorrect sleep calculated")
}
r.SetJitter(0.25)
for i := 0; i < 20; i++ {
if r.calcSleep(0) != 0 {
t.Error("Incorrect sleep calculated")
}
slp := r.calcSleep(1)
if slp < 7500*time.Microsecond || slp > 12500*time.Microsecond {
t.Error("Incorrect sleep calculated")
}
slp = r.calcSleep(2)
if slp < 3*time.Hour || slp > 5*time.Hour {
t.Error("Incorrect sleep calculated")
}
}
r.SetJitter(-1)
if r.jitter != 0.25 {
t.Error("Invalid jitter value accepted")
}
r.SetJitter(2)
if r.jitter != 0.25 {
t.Error("Invalid jitter value accepted")
}
}
func ExampleRetrier() {
r := New(ConstantBackoff(3, 100*time.Millisecond), nil)
err := r.Run(func() error {
// do some work
return nil
})
if err != nil {
// handle the case where the work failed three times
}
}
|