-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat_test.go
181 lines (145 loc) · 4.66 KB
/
heartbeat_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package discovery
import (
"context"
"slices"
"testing"
"time"
"connectrpc.com/connect"
"github.com/google/uuid"
"github.com/overmindtech/sdp-go"
)
type testHeartbeatClient struct {
// Requests will be sent to this channel
Requests chan *connect.Request[sdp.SubmitSourceHeartbeatRequest]
// Responses should be sent here
Responses chan *connect.Response[sdp.SubmitSourceHeartbeatResponse]
}
func (t testHeartbeatClient) SubmitSourceHeartbeat(ctx context.Context, req *connect.Request[sdp.SubmitSourceHeartbeatRequest]) (*connect.Response[sdp.SubmitSourceHeartbeatResponse], error) {
t.Requests <- req
return <-t.Responses, nil
}
func TestHeartbeats(t *testing.T) {
name := t.Name()
u := uuid.New()
version := "v0.0.0-test"
engineType := "aws"
requests := make(chan *connect.Request[sdp.SubmitSourceHeartbeatRequest], 1)
responses := make(chan *connect.Response[sdp.SubmitSourceHeartbeatResponse], 1)
heartbeatOptions := HeartbeatOptions{
ManagementClient: testHeartbeatClient{
Requests: requests,
Responses: responses,
},
}
ec := EngineConfig{
SourceName: name,
SourceUUID: u,
Version: version,
EngineType: engineType,
HeartbeatOptions: &heartbeatOptions,
}
e, _ := NewEngine(&ec)
if err := e.AddAdapters(
&TestAdapter{
ReturnScopes: []string{"test"},
ReturnType: "test-type",
ReturnName: "test-name",
},
&TestAdapter{
ReturnScopes: []string{"test"},
ReturnType: "test-type2",
ReturnName: "test-name2",
},
); err != nil {
t.Fatalf("unexpected error adding adapters: %v", err)
}
t.Run("sendHeartbeat when healthy", func(t *testing.T) {
ec.HeartbeatOptions.HealthCheck = func() error {
return nil
}
responses <- &connect.Response[sdp.SubmitSourceHeartbeatResponse]{
Msg: &sdp.SubmitSourceHeartbeatResponse{},
}
err := e.SendHeartbeat(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
req := <-requests
if reqUUID, err := uuid.FromBytes(req.Msg.GetUUID()); err == nil {
if reqUUID != u {
t.Errorf("expected uuid %v, got %v", u, reqUUID)
}
} else {
t.Errorf("error parsing uuid: %v", err)
}
if req.Msg.GetVersion() != version {
t.Errorf("expected version %v, got %v", version, req.Msg.GetVersion())
}
if req.Msg.GetName() != name {
t.Errorf("expected name %v, got %v", name, req.Msg.GetName())
}
if req.Msg.GetType() != engineType {
t.Errorf("expected type %v, got %v", engineType, req.Msg.GetType())
}
if req.Msg.GetManaged() != sdp.SourceManaged_LOCAL {
t.Errorf("expected managed %v, got %v", sdp.SourceManaged_LOCAL, req.Msg.GetManaged())
}
if req.Msg.GetError() != "" {
t.Errorf("expected no error, got %v", req.Msg.GetError())
}
reqAvailableScopes := req.Msg.GetAvailableScopes()
if len(reqAvailableScopes) != 1 {
t.Errorf("expected 1 scope, got %v", len(reqAvailableScopes))
}
if !slices.Contains(reqAvailableScopes, "test") {
t.Errorf("expected scope 'test' to be present in the response")
}
reqAdapterMetadata := req.Msg.GetAdapterMetadata()
if len(reqAdapterMetadata) != 2 {
t.Errorf("expected 2 adapter metadata, got %v", len(reqAdapterMetadata))
}
})
t.Run("sendHeartbeat when unhealthy", func(t *testing.T) {
e.EngineConfig.HeartbeatOptions.HealthCheck = func() error {
return ErrNoHealthcheckDefined
}
responses <- &connect.Response[sdp.SubmitSourceHeartbeatResponse]{
Msg: &sdp.SubmitSourceHeartbeatResponse{},
}
err := e.SendHeartbeat(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
req := <-requests
if req.Msg.GetError() != ErrNoHealthcheckDefined.Error() {
t.Errorf("expected error %v, got %v", ErrNoHealthcheckDefined, req.Msg.GetError())
}
})
t.Run("startSendingHeartbeats", func(t *testing.T) {
e.EngineConfig.HeartbeatOptions.Frequency = time.Millisecond * 250
e.EngineConfig.HeartbeatOptions.HealthCheck = func() error {
return nil
}
ctx, cancel := context.WithCancel(context.Background())
start := time.Now()
responses <- &connect.Response[sdp.SubmitSourceHeartbeatResponse]{
Msg: &sdp.SubmitSourceHeartbeatResponse{},
}
e.StartSendingHeartbeats(ctx)
// Get the initial heartbeat
<-requests
// Get two
responses <- &connect.Response[sdp.SubmitSourceHeartbeatResponse]{
Msg: &sdp.SubmitSourceHeartbeatResponse{},
}
<-requests
cancel()
// Make sure that took the expected amount of time
if elapsed := time.Since(start); elapsed < time.Millisecond*250 {
t.Errorf("expected to take at least 500ms, took %v", elapsed)
}
if elapsed := time.Since(start); elapsed > time.Millisecond*500 {
t.Errorf("expected to take at most 750ms, took %v", elapsed)
}
})
}