Codebase list golang-github-go-kit-kit / 578f3fa
Add tests for endpoint.go Ben Sigelman 7 years ago
1 changed file(s) with 95 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package opentracing_test
1
2 import (
3 "testing"
4
5 "github.com/go-kit/kit/endpoint"
6 kitot "github.com/go-kit/kit/tracing/opentracing"
7 "github.com/opentracing/opentracing-go"
8 "github.com/opentracing/opentracing-go/mocktracer"
9 "golang.org/x/net/context"
10 )
11
12 func TestTraceServer(t *testing.T) {
13 tracer := mocktracer.New()
14
15 // Initialize the ctx with a nameless Span.
16 contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
17 ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
18
19 var innerEndpoint endpoint.Endpoint
20 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
21 return struct{}{}, nil
22 }
23 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(innerEndpoint)
24 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
25 t.Fatal(err)
26 }
27 if want, have := 1, len(tracer.FinishedSpans); want != have {
28 t.Fatalf("Want %v span(s), found %v", want, have)
29 }
30
31 endpointSpan := tracer.FinishedSpans[0]
32 // Test that the op name is updated
33 if want, have := "testOp", endpointSpan.OperationName; want != have {
34 t.Fatalf("Want %q, have %q", want, have)
35 }
36 // ... and that the ID is unmodified.
37 if want, have := contextSpan.SpanID, endpointSpan.SpanID; want != have {
38 t.Errorf("Want SpanID %q, have %q", want, have)
39 }
40 }
41
42 func TestTraceServerNoContextSpan(t *testing.T) {
43 tracer := mocktracer.New()
44
45 var innerEndpoint endpoint.Endpoint
46 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
47 return struct{}{}, nil
48 }
49 tracedEndpoint := kitot.TraceServer(tracer, "testOp")(innerEndpoint)
50 // Empty/background context:
51 if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
52 t.Fatal(err)
53 }
54 // tracedEndpoint created a new Span:
55 if want, have := 1, len(tracer.FinishedSpans); want != have {
56 t.Fatalf("Want %v span(s), found %v", want, have)
57 }
58
59 endpointSpan := tracer.FinishedSpans[0]
60 if want, have := "testOp", endpointSpan.OperationName; want != have {
61 t.Fatalf("Want %q, have %q", want, have)
62 }
63 }
64
65 func TestTraceClient(t *testing.T) {
66 tracer := mocktracer.New()
67
68 // Initialize the ctx with a parent Span.
69 parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
70 defer parentSpan.Finish()
71 ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
72
73 var innerEndpoint endpoint.Endpoint
74 innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
75 return struct{}{}, nil
76 }
77 tracedEndpoint := kitot.TraceClient(tracer, "testOp")(innerEndpoint)
78 if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
79 t.Fatal(err)
80 }
81 // tracedEndpoint created a new Span:
82 if want, have := 1, len(tracer.FinishedSpans); want != have {
83 t.Fatalf("Want %v span(s), found %v", want, have)
84 }
85
86 endpointSpan := tracer.FinishedSpans[0]
87 if want, have := "testOp", endpointSpan.OperationName; want != have {
88 t.Fatalf("Want %q, have %q", want, have)
89 }
90 // ... and that the parent ID is set appropriately.
91 if want, have := parentSpan.SpanID, endpointSpan.ParentID; want != have {
92 t.Errorf("Want ParentID %q, have %q", want, have)
93 }
94 }