This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
mock_agent_test.go
227 lines (188 loc) · 5.34 KB
/
mock_agent_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
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
// Copyright 2020, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ocagent_test
import (
"fmt"
"net"
"sync"
"testing"
"time"
"google.golang.org/grpc"
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
)
func makeMockAgent(t *testing.T) *mockAgent {
return &mockAgent{configsToSend: make(chan *agenttracepb.UpdatedLibraryConfig), t: t, wg: new(sync.WaitGroup)}
}
type mockAgent struct {
t *testing.T
spans []*tracepb.Span
mu sync.Mutex
wg *sync.WaitGroup
traceNodes []*commonpb.Node
receivedConfigs []*agenttracepb.CurrentLibraryConfig
resource *resourcepb.Resource
configsToSend chan *agenttracepb.UpdatedLibraryConfig
closeConfigsToSendOnce sync.Once
address string
stopFunc func() error
stopOnce sync.Once
}
var _ agenttracepb.TraceServiceServer = (*mockAgent)(nil)
func (ma *mockAgent) Config(tscs agenttracepb.TraceService_ConfigServer) error {
ma.mu.Lock()
ma.wg.Add(1)
ma.mu.Unlock()
defer func() {
ma.mu.Lock()
ma.wg.Done()
ma.mu.Unlock()
}()
in, err := tscs.Recv()
if err != nil {
return err
}
if in == nil || in.Node == nil {
return fmt.Errorf("the first message must contain the node identifier")
}
ma.receivedConfigs = append(ma.receivedConfigs, in)
// Push down all the configs
for cfg := range ma.configsToSend {
// Push down configs
if err := tscs.Send(cfg); err != nil {
return err
}
// And then get back the config sent back by the client library
back, err := tscs.Recv()
if err != nil {
return err
}
ma.receivedConfigs = append(ma.receivedConfigs, back)
}
// Just for the sake of draining any configs
// that the client-side exporter is still sending.
for {
back, err := tscs.Recv()
if err != nil {
return err
}
ma.receivedConfigs = append(ma.receivedConfigs, back)
}
}
func (ma *mockAgent) Export(tses agenttracepb.TraceService_ExportServer) error {
in, err := tses.Recv()
if err != nil {
return err
}
ma.mu.Lock()
ma.wg.Add(1)
ma.mu.Unlock()
defer func() {
ma.mu.Lock()
ma.wg.Done()
ma.mu.Unlock()
}()
// The first trace message should contain the node identifier.
if in == nil || in.Node == nil {
return fmt.Errorf("the first message must contain the node identifier")
}
ma.traceNodes = append(ma.traceNodes, in.Node)
// Now that we have the node identifier, let's start receiving spans.
for {
req, err := tses.Recv()
if err != nil {
return err
}
ma.mu.Lock()
ma.resource = req.Resource
ma.spans = append(ma.spans, req.Spans...)
ma.traceNodes = append(ma.traceNodes, req.Node)
ma.mu.Unlock()
}
}
func (ma *mockAgent) transitionToReceivingClientConfigs() {
// Since we are done sending all the configs, close the configsChannel
// so that the state can transition to receiving all the client configs.
ma.closeConfigsToSendOnce.Do(func() {
close(ma.configsToSend)
})
}
var errAlreadyStopped = fmt.Errorf("already stopped")
func (ma *mockAgent) stop() error {
var err = errAlreadyStopped
ma.stopOnce.Do(func() {
ma.transitionToReceivingClientConfigs()
if ma.stopFunc != nil {
err = ma.stopFunc()
}
})
// Give it sometime to shutdown.
<-time.After(160 * time.Millisecond)
ma.mu.Lock()
ma.wg.Wait()
ma.mu.Unlock()
return err
}
// runMockAgent is a helper function to create a mockAgent
func runMockAgent(t *testing.T) *mockAgent {
return runMockAgentAtAddr(t, ":0")
}
func runMockAgentAtAddr(t *testing.T, addr string) *mockAgent {
var deferFuncs []func() error
ln, err := net.Listen("tcp", addr)
if err != nil {
t.Fatalf("Failed to get an address: %v", err)
}
deferFuncs = append(deferFuncs, ln.Close)
srv := grpc.NewServer()
ma := makeMockAgent(t)
agenttracepb.RegisterTraceServiceServer(srv, ma)
go func() {
_ = srv.Serve(ln)
}()
deferFunc := func() error {
srv.Stop()
return ln.Close()
}
_, agentPortStr, _ := net.SplitHostPort(ln.Addr().String())
ma.address = "localhost:" + agentPortStr
ma.stopFunc = deferFunc
return ma
}
func (ma *mockAgent) getSpans() []*tracepb.Span {
ma.mu.Lock()
spans := append([]*tracepb.Span{}, ma.spans...)
ma.mu.Unlock()
return spans
}
func (ma *mockAgent) getReceivedConfigs() []*agenttracepb.CurrentLibraryConfig {
ma.mu.Lock()
receivedConfigs := append([]*agenttracepb.CurrentLibraryConfig{}, ma.receivedConfigs...)
ma.mu.Unlock()
return receivedConfigs
}
func (ma *mockAgent) getTraceNodes() []*commonpb.Node {
ma.mu.Lock()
traceNodes := append([]*commonpb.Node{}, ma.traceNodes...)
ma.mu.Unlock()
return traceNodes
}
func (ma *mockAgent) getResource() *resourcepb.Resource {
ma.mu.Lock()
resource := ma.resource
ma.mu.Unlock()
return resource
}