-
Notifications
You must be signed in to change notification settings - Fork 67
/
grpc_server.go
248 lines (199 loc) 路 7.06 KB
/
grpc_server.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package mocks
import (
"context"
"fmt"
"log"
"net"
"sync"
"github.com/avast/retry-go"
"github.com/kubeshop/tracetest/agent/client"
"github.com/kubeshop/tracetest/agent/proto"
"google.golang.org/grpc"
)
type GrpcServerMock struct {
proto.UnimplementedOrchestratorServer
port int
triggerChannel chan *proto.TriggerRequest
pollingChannel chan *proto.PollingRequest
otlpConnectionTestChannel chan *proto.OTLPConnectionTestRequest
terminationChannel chan *proto.ShutdownRequest
dataStoreTestChannel chan *proto.DataStoreConnectionTestRequest
lastTriggerResponse *proto.TriggerResponse
lastPollingResponse *proto.PollingResponse
lastOtlpConnectionResponse *proto.OTLPConnectionTestResponse
lastDataStoreConnectionResponse *proto.DataStoreConnectionTestResponse
server *grpc.Server
}
func NewGrpcServer() *GrpcServerMock {
server := &GrpcServerMock{
triggerChannel: make(chan *proto.TriggerRequest),
pollingChannel: make(chan *proto.PollingRequest),
terminationChannel: make(chan *proto.ShutdownRequest),
dataStoreTestChannel: make(chan *proto.DataStoreConnectionTestRequest),
otlpConnectionTestChannel: make(chan *proto.OTLPConnectionTestRequest),
}
var wg sync.WaitGroup
wg.Add(1)
err := retry.Do(func() error {
return server.start(&wg, 0)
}, retry.Attempts(client.ReconnectRetryAttempts), retry.Delay(client.ReconnectRetryAttemptDelay))
if err != nil {
log.Fatal(err)
}
wg.Wait()
return server
}
func (s *GrpcServerMock) Addr() string {
return fmt.Sprintf("localhost:%d", s.port)
}
func (s *GrpcServerMock) start(wg *sync.WaitGroup, port int) error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
s.port = lis.Addr().(*net.TCPAddr).Port
server := grpc.NewServer()
proto.RegisterOrchestratorServer(server, s)
s.server = server
wg.Done()
go func() {
if err := server.Serve(lis); err != nil {
log.Fatal("failed to serve: %w", err)
}
}()
return nil
}
func (s *GrpcServerMock) Connect(ctx context.Context, req *proto.ConnectRequest) (*proto.AgentConfiguration, error) {
return &proto.AgentConfiguration{
Configuration: &proto.SessionConfiguration{
BatchTimeout: 1000,
},
Identification: &proto.AgentIdentification{
Token: "token",
},
}, nil
}
func (s *GrpcServerMock) RegisterStopRequestAgent(id *proto.AgentIdentification, stream proto.Orchestrator_RegisterStopRequestAgentServer) error {
if id.Token != "token" {
return fmt.Errorf("could not validate token")
}
return nil
}
func (s *GrpcServerMock) RegisterTriggerAgent(id *proto.AgentIdentification, stream proto.Orchestrator_RegisterTriggerAgentServer) error {
if id.Token != "token" {
return fmt.Errorf("could not validate token")
}
for {
triggerRequest := <-s.triggerChannel
err := stream.Send(triggerRequest)
if err != nil {
log.Println("could not send trigger request to agent: %w", err)
}
}
}
func (s *GrpcServerMock) SendTriggerResult(ctx context.Context, result *proto.TriggerResponse) (*proto.Empty, error) {
if result.AgentIdentification == nil || result.AgentIdentification.Token != "token" {
return nil, fmt.Errorf("could not validate token")
}
s.lastTriggerResponse = result
return &proto.Empty{}, nil
}
func (s *GrpcServerMock) RegisterPollerAgent(id *proto.AgentIdentification, stream proto.Orchestrator_RegisterPollerAgentServer) error {
if id.Token != "token" {
return fmt.Errorf("could not validate token")
}
for {
pollerRequest := <-s.pollingChannel
err := stream.Send(pollerRequest)
if err != nil {
log.Println("could not send polling request to agent: %w", err)
}
}
}
func (s *GrpcServerMock) RegisterDataStoreConnectionTestAgent(id *proto.AgentIdentification, stream proto.Orchestrator_RegisterDataStoreConnectionTestAgentServer) error {
if id.Token != "token" {
return fmt.Errorf("could not validate token")
}
for {
dsTestRequest := <-s.dataStoreTestChannel
err := stream.Send(dsTestRequest)
if err != nil {
log.Println("could not send polling request to agent: %w", err)
}
}
}
func (s *GrpcServerMock) RegisterOTLPConnectionTestListener(id *proto.AgentIdentification, stream proto.Orchestrator_RegisterOTLPConnectionTestListenerServer) error {
if id.Token != "token" {
return fmt.Errorf("could not validate token")
}
for {
testRequest := <-s.otlpConnectionTestChannel
err := stream.Send(testRequest)
if err != nil {
log.Println("could not send polling request to agent: %w", err)
}
}
}
func (s *GrpcServerMock) SendOTLPConnectionTestResult(ctx context.Context, result *proto.OTLPConnectionTestResponse) (*proto.Empty, error) {
if result.AgentIdentification == nil || result.AgentIdentification.Token != "token" {
return nil, fmt.Errorf("could not validate token")
}
s.lastOtlpConnectionResponse = result
return &proto.Empty{}, nil
}
func (s *GrpcServerMock) SendDataStoreConnectionTestResult(ctx context.Context, result *proto.DataStoreConnectionTestResponse) (*proto.Empty, error) {
if result.AgentIdentification == nil || result.AgentIdentification.Token != "token" {
return nil, fmt.Errorf("could not validate token")
}
s.lastDataStoreConnectionResponse = result
return &proto.Empty{}, nil
}
func (s *GrpcServerMock) SendPolledSpans(ctx context.Context, result *proto.PollingResponse) (*proto.Empty, error) {
if result.AgentIdentification == nil || result.AgentIdentification.Token != "token" {
return nil, fmt.Errorf("could not validate token")
}
s.lastPollingResponse = result
return &proto.Empty{}, nil
}
func (s *GrpcServerMock) RegisterShutdownListener(_ *proto.AgentIdentification, stream proto.Orchestrator_RegisterShutdownListenerServer) error {
for {
shutdownRequest := <-s.terminationChannel
err := stream.Send(shutdownRequest)
if err != nil {
log.Println("could not send polling request to agent: %w", err)
}
}
}
// Test methods
func (s *GrpcServerMock) SendTriggerRequest(request *proto.TriggerRequest) {
s.triggerChannel <- request
}
func (s *GrpcServerMock) SendPollingRequest(request *proto.PollingRequest) {
s.pollingChannel <- request
}
func (s *GrpcServerMock) SendDataStoreConnectionTestRequest(request *proto.DataStoreConnectionTestRequest) {
s.dataStoreTestChannel <- request
}
func (s *GrpcServerMock) SendOTLPConnectionTestRequest(request *proto.OTLPConnectionTestRequest) {
s.otlpConnectionTestChannel <- request
}
func (s *GrpcServerMock) GetLastTriggerResponse() *proto.TriggerResponse {
return s.lastTriggerResponse
}
func (s *GrpcServerMock) GetLastPollingResponse() *proto.PollingResponse {
return s.lastPollingResponse
}
func (s *GrpcServerMock) GetLastOTLPConnectionResponse() *proto.OTLPConnectionTestResponse {
return s.lastOtlpConnectionResponse
}
func (s *GrpcServerMock) GetLastDataStoreConnectionResponse() *proto.DataStoreConnectionTestResponse {
return s.lastDataStoreConnectionResponse
}
func (s *GrpcServerMock) TerminateConnection(reason string) {
s.terminationChannel <- &proto.ShutdownRequest{
Reason: reason,
}
}
func (s *GrpcServerMock) Stop() {
s.server.Stop()
}