-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathha_test.go
55 lines (46 loc) · 990 Bytes
/
ha_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
package ha
import (
"testing"
"time"
"golang.org/x/net/context"
"github.com/stretchr/testify/assert"
)
func TestRestartTimes(t *testing.T) {
times := 0
Watch(func() {
times += 1
panic("")
}, RestartTimes(100))
assert.Equal(t, 100, times)
}
func TestRestartDelay(t *testing.T) {
const num = 100
var tps []time.Time
Watch(func() {
tps = append(tps, time.Now())
panic("")
}, RestartDelay(num*time.Millisecond), RestartTimes(10))
for i := range tps {
if i == 0 {
continue
}
assert.Equal(t, int(tps[i].Sub(tps[i-1]).Seconds()*1000/num), 1)
}
}
func TestOnStop(t *testing.T) {
errLogged := false
Watch(func() {
panic("")
}, RestartTimes(1), OnStop(func(err error) {
errLogged = true
}))
assert.True(t, errLogged)
}
func TestCancelCtx(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
start := time.Now()
Watch(func() {
panic("")
}, CancelCtx(ctx))
assert.Equal(t, int(time.Now().Sub(start).Seconds()), 2)
}