Zipkin: added TestFromContext
Bas van Beek
8 years ago
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | func TestToGRPCContext(t *testing.T) { | |
64 | const ( | |
65 | hostport = "5.5.5.5:5555" | |
66 | serviceName = "foo-service" | |
67 | methodName = "foo-method" | |
68 | traceID int64 = 12 | |
69 | spanID int64 = 34 | |
70 | parentSpanID int64 = 56 | |
71 | ) | |
72 | ||
73 | md := metadata.MD{ | |
74 | "x-b3-traceid": []string{strconv.FormatInt(traceID, 16)}, | |
75 | "x-b3-spanid": []string{strconv.FormatInt(spanID, 16)}, | |
76 | "x-b3-parentspanid": []string{strconv.FormatInt(parentSpanID, 16)}, | |
77 | } | |
78 | ||
79 | newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName) | |
80 | toContext := zipkin.ToGRPCContext(newSpan, log.NewLogfmtLogger(ioutil.Discard)) | |
81 | ||
82 | ctx := toContext(context.Background(), &md) | |
83 | val := ctx.Value(zipkin.SpanContextKey) | |
84 | if val == nil { | |
85 | t.Fatalf("%s returned no value", zipkin.SpanContextKey) | |
86 | } | |
87 | span, ok := val.(*zipkin.Span) | |
63 | func TestFromContext(t *testing.T) { | |
64 | const ( | |
65 | hostport = "5.5.5.5:5555" | |
66 | serviceName = "foo-service" | |
67 | methodName = "foo-method" | |
68 | traceID int64 = 14 | |
69 | spanID int64 = 36 | |
70 | parentSpanID int64 = 58 | |
71 | ) | |
72 | ||
73 | ctx := context.WithValue( | |
74 | context.Background(), | |
75 | zipkin.SpanContextKey, | |
76 | zipkin.NewSpan(hostport, serviceName, methodName, traceID, spanID, parentSpanID), | |
77 | ) | |
78 | ||
79 | span, ok := zipkin.FromContext(ctx) | |
88 | 80 | if !ok { |
89 | t.Fatalf("%s was not a Span object", zipkin.SpanContextKey) | |
90 | } | |
91 | ||
81 | t.Fatalf("expected a context value in %q", zipkin.SpanContextKey) | |
82 | } | |
83 | if span == nil { | |
84 | t.Fatal("expected a Zipkin span object") | |
85 | } | |
92 | 86 | for want, haveFunc := range map[int64]func() int64{ |
93 | 87 | traceID: span.TraceID, |
94 | 88 | spanID: span.SpanID, |
104 | 98 | } |
105 | 99 | } |
106 | 100 | |
101 | func TestToGRPCContext(t *testing.T) { | |
102 | const ( | |
103 | hostport = "5.5.5.5:5555" | |
104 | serviceName = "foo-service" | |
105 | methodName = "foo-method" | |
106 | traceID int64 = 12 | |
107 | spanID int64 = 34 | |
108 | parentSpanID int64 = 56 | |
109 | ) | |
110 | ||
111 | md := metadata.MD{ | |
112 | "x-b3-traceid": []string{strconv.FormatInt(traceID, 16)}, | |
113 | "x-b3-spanid": []string{strconv.FormatInt(spanID, 16)}, | |
114 | "x-b3-parentspanid": []string{strconv.FormatInt(parentSpanID, 16)}, | |
115 | } | |
116 | ||
117 | newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName) | |
118 | toContext := zipkin.ToGRPCContext(newSpan, log.NewLogfmtLogger(ioutil.Discard)) | |
119 | ||
120 | ctx := toContext(context.Background(), &md) | |
121 | val := ctx.Value(zipkin.SpanContextKey) | |
122 | if val == nil { | |
123 | t.Fatalf("%s returned no value", zipkin.SpanContextKey) | |
124 | } | |
125 | span, ok := val.(*zipkin.Span) | |
126 | if !ok { | |
127 | t.Fatalf("%s was not a Span object", zipkin.SpanContextKey) | |
128 | } | |
129 | ||
130 | for want, haveFunc := range map[int64]func() int64{ | |
131 | traceID: span.TraceID, | |
132 | spanID: span.SpanID, | |
133 | parentSpanID: span.ParentSpanID, | |
134 | } { | |
135 | if have := haveFunc(); want != have { | |
136 | name := runtime.FuncForPC(reflect.ValueOf(haveFunc).Pointer()).Name() | |
137 | name = strings.Split(name, "ยท")[0] | |
138 | toks := strings.Split(name, ".") | |
139 | name = toks[len(toks)-1] | |
140 | t.Errorf("%s: want %d, have %d", name, want, have) | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
107 | 145 | func TestToRequest(t *testing.T) { |
108 | 146 | const ( |
109 | 147 | hostport = "5.5.5.5:5555" |