forked from DataDog/dd-trace-go
/
testing.go
65 lines (55 loc) · 1.92 KB
/
testing.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
package tracer
import (
"fmt"
"net/http"
"reflect"
"testing"
"github.com/DataDog/dd-trace-go/tracer/ext"
"github.com/stretchr/testify/assert"
)
// CopySpan returns a new span with the same fields of the copied one.
// This function is necessary because the usual assignment copies the mutex address
// and then the use of the copied span can conflict with the original one when concurent calls.
func CopySpan(span *Span, trc *Tracer) *Span {
newSpan := NewSpan(span.Name, span.Service, span.Resource, span.SpanID, span.TraceID, span.ParentID, trc)
newSpan.Type = ext.SQLType
newSpan.Meta = span.Meta
return newSpan
}
// Test strict equality between the most important fields of the two spans
func CompareSpan(t *testing.T, expectedSpan, actualSpan *Span, debug ...bool) {
if len(debug) > 0 && debug[0] {
fmt.Printf("-> ExpectedSpan: \n%s\n\n", expectedSpan)
}
assert := assert.New(t)
assert.Equal(expectedSpan.Name, actualSpan.Name)
assert.Equal(expectedSpan.Service, actualSpan.Service)
assert.Equal(expectedSpan.Resource, actualSpan.Resource)
assert.Equal(expectedSpan.Type, actualSpan.Type)
assert.True(reflect.DeepEqual(expectedSpan.Meta, actualSpan.Meta))
}
// Return a Tracer with a DummyTransport
func GetTestTracer() (*Tracer, *DummyTransport) {
transport := &DummyTransport{}
tracer := NewTracerTransport(transport)
return tracer, transport
}
// dummyTransport is a transport that just buffers spans and encoding
type DummyTransport struct {
traces [][]*Span
services map[string]Service
}
func (t *DummyTransport) SendTraces(traces [][]*Span) (*http.Response, error) {
t.traces = append(t.traces, traces...)
return nil, nil
}
func (t *DummyTransport) SendServices(services map[string]Service) (*http.Response, error) {
t.services = services
return nil, nil
}
func (t *DummyTransport) Traces() [][]*Span {
traces := t.traces
t.traces = nil
return traces
}
func (t *DummyTransport) SetHeader(key, value string) {}