This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
forked from facebookarchive/dvara
/
healthcheck_test.go
83 lines (65 loc) · 1.91 KB
/
healthcheck_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
package dvara
import (
"errors"
"testing"
"time"
"github.com/facebookgo/mgotest"
)
type FakeReplicaSet struct {
handleFailureCalled bool
CheckReturnsError bool
}
func (frs *FakeReplicaSet) Check(timeout time.Duration) error {
if frs.CheckReturnsError {
return errors.New("Failed")
}
return nil
}
func (frs *FakeReplicaSet) HandleFailure() {
frs.handleFailureCalled = true
}
func TestEnsureRestartIsCalled(t *testing.T) {
frs := FakeReplicaSet{CheckReturnsError: true}
hc := HealthChecker{
HealthCheckInterval: time.Millisecond,
FailedHealthCheckThreshold: 2,
}
go hc.HealthCheck(&frs, nil)
time.Sleep(5 * time.Millisecond)
hc.Cancel = true
if frs.handleFailureCalled == false {
t.Fatalf("Restart function not called :( %s", frs)
}
}
func TestEnsureRestartIsNotCalled(t *testing.T) {
frs := FakeReplicaSet{CheckReturnsError: false}
hc := HealthChecker{
HealthCheckInterval: time.Millisecond,
FailedHealthCheckThreshold: 2,
}
go hc.HealthCheck(&frs, nil)
time.Sleep(5 * time.Millisecond)
hc.Cancel = true
if frs.handleFailureCalled == true {
t.Fatalf("Restart function not called :( %s", frs)
}
}
func TestChecksWithReplicaSets(t *testing.T) {
t.Parallel()
standalone := mgotest.NewStartedServer(t)
defer standalone.Stop()
rs := mgotest.NewReplicaSet(3, t)
defer rs.Stop()
if err := checkReplSetStatus(rs.Addrs(), "rs"); err != nil {
t.Error("check should pass if all members are in the replica set:", err)
}
if err := checkReplSetStatus([]string{standalone.URL()}, "rs"); err == nil {
t.Error("expected failure if single server running in standalone")
}
if err := checkReplSetStatus(append(rs.Addrs(), standalone.URL()), "rs"); err != nil {
t.Error("check should ignore standalone if there are other healthy members:", err)
}
if err := checkReplSetStatus(rs.Addrs(), "rs-alt"); err == nil {
t.Error("check should fail if members are in a different replica set")
}
}