Codebase list golang-github-go-kit-kit / 2a81195
Add tests for the gprc-OT bridge Ben Sigelman 7 years ago
1 changed file(s) with 55 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package opentracing_test
1
2 import (
3 "testing"
4
5 "google.golang.org/grpc/metadata"
6
7 kitot "github.com/go-kit/kit/tracing/opentracing"
8 "github.com/go-kit/kit/transport/grpc"
9 "github.com/opentracing/opentracing-go"
10 "github.com/opentracing/opentracing-go/mocktracer"
11 "golang.org/x/net/context"
12 )
13
14 func TestTraceGRPCRequestRoundtrip(t *testing.T) {
15 tracer := mocktracer.New()
16
17 // Initialize the ctx with a Span to inject.
18 beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
19 defer beforeSpan.Finish()
20 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
21
22 var toGRPCFunc grpc.RequestFunc = kitot.ToGRPCRequest(tracer)
23 md := metadata.Pairs()
24 // Call the RequestFunc.
25 afterCtx := toGRPCFunc(beforeCtx, &md)
26
27 // The Span should not have changed.
28 afterSpan := opentracing.SpanFromContext(afterCtx)
29 if beforeSpan != afterSpan {
30 t.Errorf("Should not swap in a new span")
31 }
32
33 // No spans should have finished yet.
34 if want, have := 0, len(tracer.FinishedSpans); want != have {
35 t.Errorf("Want %v span(s), found %v", want, have)
36 }
37
38 // Use FromGRPCRequest to verify that we can join with the trace given MD.
39 var fromGRPCFunc grpc.RequestFunc = kitot.FromGRPCRequest(tracer, "joined")
40 joinCtx := fromGRPCFunc(afterCtx, &md)
41 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
42
43 if joinedSpan.SpanID == beforeSpan.SpanID {
44 t.Error("SpanID should have changed", joinedSpan.SpanID, beforeSpan.SpanID)
45 }
46
47 // Check that the parent/child relationship is as expected for the joined span.
48 if want, have := beforeSpan.SpanID, joinedSpan.ParentID; want != have {
49 t.Errorf("Want ParentID %q, have %q", want, have)
50 }
51 if want, have := "joined", joinedSpan.OperationName; want != have {
52 t.Errorf("Want %q, have %q", want, have)
53 }
54 }