forked from lovoo/gcloud-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder_test.go
140 lines (122 loc) · 3.71 KB
/
recorder_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
package gcloudtracer
import (
"log"
"net"
"os"
"testing"
"time"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/timestamp"
basictracer "github.com/opentracing/basictracer-go"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/api/option"
cloudtracepb "google.golang.org/genproto/googleapis/devtools/cloudtrace/v1"
"google.golang.org/grpc"
)
var (
clientOpt option.ClientOption
mockTrace *mockTraceServer
)
func TestMain(m *testing.M) {
mockTrace = &mockTraceServer{
patchTraces: func(ctx context.Context, req *cloudtracepb.PatchTracesRequest) (*google_protobuf.Empty, error) {
return &google_protobuf.Empty{}, nil
},
}
serv := grpc.NewServer()
cloudtracepb.RegisterTraceServiceServer(serv, mockTrace)
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
log.Fatal(err)
}
go serv.Serve(lis)
conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
clientOpt = option.WithGRPCConn(conn)
os.Exit(m.Run())
}
func TestRecorder(t *testing.T) {
t.Run("recorder=success", func(t *testing.T) {
called := false
recorder, err := NewRecorder(
context.Background(),
WithProject("test_project"),
WithLogger(testLogger(func(format string, args ...interface{}) {
called = true
})),
WithClientOption(clientOpt),
)
assert.NoError(t, err)
recorder.RecordSpan(basictracer.RawSpan{
Context: basictracer.SpanContext{
SpanID: 10,
TraceID: 1,
},
Operation: "test/operation",
ParentSpanID: 9,
Start: time.Unix(1480425868, 0),
Duration: 5 * time.Second,
Tags: map[string]interface{}{
"foo": 10,
"bar": "foo",
},
})
assert.False(t, called, "logger should not be called")
assert.Equal(t, &cloudtracepb.PatchTracesRequest{
ProjectId: "test_project",
Traces: &cloudtracepb.Traces{
Traces: []*cloudtracepb.Trace{
&cloudtracepb.Trace{
ProjectId: "test_project",
TraceId: "1",
Spans: []*cloudtracepb.TraceSpan{
&cloudtracepb.TraceSpan{
SpanId: 10,
Kind: cloudtracepb.TraceSpan_SPAN_KIND_UNSPECIFIED,
Name: "test/operation",
StartTime: ×tamp.Timestamp{
Seconds: 1480425868,
},
EndTime: ×tamp.Timestamp{
Seconds: 1480425873,
},
ParentSpanId: 9,
Labels: map[string]string{
"foo": "10",
"bar": "foo",
},
},
},
},
},
},
}, mockTrace.patchTracesRequest)
})
}
type mockTraceServer struct {
listTracesRequest *cloudtracepb.ListTracesRequest
getTraceRequest *cloudtracepb.GetTraceRequest
patchTracesRequest *cloudtracepb.PatchTracesRequest
listTraces func(context.Context, *cloudtracepb.ListTracesRequest) (*cloudtracepb.ListTracesResponse, error)
getTrace func(context.Context, *cloudtracepb.GetTraceRequest) (*cloudtracepb.Trace, error)
patchTraces func(context.Context, *cloudtracepb.PatchTracesRequest) (*google_protobuf.Empty, error)
}
func (s *mockTraceServer) ListTraces(ctx context.Context, req *cloudtracepb.ListTracesRequest) (*cloudtracepb.ListTracesResponse, error) {
s.listTracesRequest = req
return s.listTraces(ctx, req)
}
func (s *mockTraceServer) GetTrace(ctx context.Context, req *cloudtracepb.GetTraceRequest) (*cloudtracepb.Trace, error) {
s.getTraceRequest = req
return s.getTrace(ctx, req)
}
func (s *mockTraceServer) PatchTraces(ctx context.Context, req *cloudtracepb.PatchTracesRequest) (*google_protobuf.Empty, error) {
s.patchTracesRequest = req
return s.patchTraces(ctx, req)
}
type testLogger func(format string, args ...interface{})
func (l testLogger) Errorf(format string, args ...interface{}) {
l(format, args...)
}