This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
segment_test.go
211 lines (195 loc) · 5.2 KB
/
segment_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
//
// Copyright 2022 SkyAPM org
//
// 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 go2sky
import (
"context"
"fmt"
"sync"
"testing"
"time"
agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
)
func TestSyncSegment(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
mr := MockReporter{
WaitGroup: wg,
}
tracer, _ := NewTracer("segmentTest", WithReporter(&mr))
ctx := context.Background()
span, ctx, _ := tracer.CreateEntrySpan(ctx, "entry", MockExtractor)
eSpan, _ := tracer.CreateExitSpan(ctx, "exit", "localhost:8080", MockInjector)
eSpan.End()
span.End()
wg.Wait()
if err := mr.Verify(2); err != nil {
t.Error(err)
}
if eSpan.IsValid() {
t.Error("exit span is still valid")
}
eSpan.End() // not be panic
if span.IsValid() {
t.Error("entry span is still valid")
}
span.End() // not be panic
}
func TestAsyncSingleSegment(t *testing.T) {
reportWg := &sync.WaitGroup{}
exitWg := &sync.WaitGroup{}
reportWg.Add(1)
exitWg.Add(2)
mr := MockReporter{
WaitGroup: reportWg,
}
tracer, _ := NewTracer("segmentTest", WithReporter(&mr))
ctx := context.Background()
span, ctx, _ := tracer.CreateEntrySpan(ctx, "entry", MockExtractor)
go func() {
eSpan, _ := tracer.CreateExitSpan(ctx, "exit", "localhost:8080", MockInjector)
eSpan.End()
exitWg.Done()
}()
go func() {
eSpan, _ := tracer.CreateExitSpan(ctx, "exit", "localhost:8080", MockInjector)
eSpan.End()
exitWg.Done()
}()
exitWg.Wait()
span.End()
reportWg.Wait()
if err := mr.Verify(3); err != nil {
t.Error(err)
}
}
func TestAsyncMultipleSegments(t *testing.T) {
reportWg := &sync.WaitGroup{}
reportWg.Add(1)
mr := MockReporter{
WaitGroup: reportWg,
}
tracer, _ := NewTracer("segmentTest", WithReporter(&mr))
ctx := context.Background()
span, ctx, _ := tracer.CreateEntrySpan(ctx, "entry", MockExtractor)
span.End()
reportWg.Wait()
reportWg.Add(2)
go func() {
oSpan, subCtx, _ := tracer.CreateLocalSpan(ctx)
eSpan, _ := tracer.CreateExitSpan(subCtx, "exit", "localhost:8080", MockInjector)
eSpan.End()
oSpan.End()
}()
go func() {
oSpan, subCtx, _ := tracer.CreateLocalSpan(ctx)
eSpan, _ := tracer.CreateExitSpan(subCtx, "exit", "localhost:8080", MockInjector)
eSpan.End()
oSpan.End()
}()
reportWg.Wait()
if err := mr.Verify(1, 2, 2); err != nil {
t.Error(err)
}
}
func TestReportedSpan(t *testing.T) {
reportWg := &sync.WaitGroup{}
reportWg.Add(1)
mr := MockReporter{
WaitGroup: reportWg,
}
tracer, _ := NewTracer("service", WithInstance("instance"), WithReporter(&mr))
ctx := context.Background()
span, err := tracer.CreateExitSpan(ctx, "exit", "localhost:9999", MockInjector)
if err != nil {
t.Error(err)
}
span.SetSpanLayer(agentv3.SpanLayer_Http)
span.Error(time.Now(), "error")
span.Log(time.Now(), "log")
span.Tag(TagURL, "http://localhost:9999/exit")
span.SetComponent(1)
span.End()
reportWg.Wait()
if err := mr.Verify(1); err != nil {
t.Error(err)
}
reportSpan := mr.Message[0][0]
if reportSpan.StartTime() < 0 || reportSpan.StartTime() > reportSpan.EndTime() {
t.Error("errors are not start/end time")
}
if reportSpan.OperationName() != "exit" {
t.Error("error are not set operation name")
}
if reportSpan.Peer() != "localhost:9999" {
t.Error("error are not set peer")
}
if reportSpan.SpanType() != agentv3.SpanType_Exit {
t.Error("error are not set span type")
}
if reportSpan.SpanLayer() != agentv3.SpanLayer_Http {
t.Error("error are not set span layer")
}
if !reportSpan.IsError() {
t.Error("error are not set isError")
}
if len(reportSpan.Tags()) != 1 {
t.Error("error are not set tag")
}
if len(reportSpan.Logs()) != 2 {
t.Error("error are not set log")
}
if reportSpan.ComponentID() != 1 {
t.Error("error are not set component id")
}
}
func MockExtractor(key string) (c string, e error) {
return
}
func MockInjector(key, value string) (e error) {
return
}
type Segment []ReportedSpan
type MockReporter struct {
Reporter
Message []Segment
*sync.WaitGroup
sync.Mutex
}
func (r *MockReporter) Boot(service string, serviceInstance string, cdsWatchers []AgentConfigChangeWatcher) {
}
func (r *MockReporter) Send(spans []ReportedSpan) {
r.Mutex.Lock()
defer r.Mutex.Unlock()
r.Message = append(r.Message, spans)
r.WaitGroup.Done()
}
func (r *MockReporter) Verify(mm ...int) error {
if len(mm) != len(r.Message) {
return fmt.Errorf("message size mismatch. expected %d actual %d", len(mm), len(r.Message))
}
for i, m := range mm {
if m != len(r.Message[i]) {
return fmt.Errorf("span size mismatch. expected %d actual %d", m, len(r.Message[i]))
}
if i > 0 {
s := r.Message[i][len(r.Message[i])-1]
if len(s.Refs()) == 0 {
return fmt.Errorf("span refs mismatch. expected %d actual %d", 1, len(s.Refs()))
}
}
}
return nil
}