-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathliveness_test.go
56 lines (46 loc) · 1.42 KB
/
liveness_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
package ctrl
import (
"context"
"os"
"testing"
"time"
"github.com/metal-toolbox/rivets/v2/events"
"github.com/metal-toolbox/rivets/v2/events/registry"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
func TestStartLivenessCheckin(t *testing.T) {
// Ignore can be removed if this ever gets fixed,
//
// https://github.com/census-instrumentation/opencensus-go/issues/1191
defer goleak.VerifyNone(t, []goleak.Option{goleak.IgnoreCurrent()}...)
srv := startJetStreamServer(t)
defer shutdownJetStream(t, srv)
natsConn, _ := jetStreamContext(t, srv) // nc is closed on evJS.Close(), js needs no cleanup
evJS := events.NewJetstreamFromConn(natsConn)
defer evJS.Close()
hostname, _ := os.Hostname()
n := &NatsController{
hostname: hostname,
logger: logrus.New(),
stream: evJS,
checkinInterval: checkinInterval,
}
n.liveness = NewNatsLiveness(n.natsConfig, n.stream, n.logger, n.hostname, checkinInterval)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
n.liveness.StartLivenessCheckin(ctx)
var ts time.Time
var errLastContact error
// wait for checkin routine to complete - 5 seconds should be enough?
for i := 0; i <= 5; i++ {
ts, errLastContact = registry.LastContact(n.liveness.ControllerID())
if errLastContact == nil {
break
}
time.Sleep(1 * time.Second)
}
assert.Nil(t, errLastContact)
assert.NotZero(t, ts)
}