1// Copyright (c) 2017 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package jaeger
16
17import (
18	"testing"
19
20	"github.com/opentracing/opentracing-go/ext"
21	"github.com/stretchr/testify/assert"
22)
23
24func TestZipkinPropagator(t *testing.T) {
25	tracer, tCloser := NewTracer("x", NewConstSampler(true), NewNullReporter(), TracerOptions.ZipkinSharedRPCSpan(true))
26	defer tCloser.Close()
27
28	carrier := &TestZipkinSpan{}
29	sp := tracer.StartSpan("y")
30
31	// Note: we intentionally use string as format, as that's what TChannel would need to do
32	if err := tracer.Inject(sp.Context(), "zipkin-span-format", carrier); err != nil {
33		t.Fatalf("Inject failed: %+v", err)
34	}
35	sp1 := sp.(*Span)
36	assert.Equal(t, sp1.context.traceID, TraceID{Low: carrier.traceID})
37	assert.Equal(t, sp1.context.spanID, SpanID(carrier.spanID))
38	assert.Equal(t, sp1.context.parentID, SpanID(carrier.parentID))
39	assert.Equal(t, sp1.context.samplingState.flags(), carrier.flags)
40
41	sp2ctx, err := tracer.Extract("zipkin-span-format", carrier)
42	if err != nil {
43		t.Fatalf("Extract failed: %+v", err)
44	}
45	sp2 := tracer.StartSpan("x", ext.RPCServerOption(sp2ctx))
46	sp3 := sp2.(*Span)
47	assert.Equal(t, sp1.context.traceID, sp3.context.traceID)
48	assert.Equal(t, sp1.context.spanID, sp3.context.spanID)
49	assert.Equal(t, sp1.context.parentID, sp3.context.parentID)
50	assert.Equal(t, sp1.context.samplingState.flags(), sp3.context.samplingState.flags())
51}
52
53// TestZipkinSpan is a mock-up of TChannel's internal Span struct
54type TestZipkinSpan struct {
55	traceID  uint64
56	parentID uint64
57	spanID   uint64
58	flags    byte
59}
60
61func (s TestZipkinSpan) TraceID() uint64              { return s.traceID }
62func (s TestZipkinSpan) ParentID() uint64             { return s.parentID }
63func (s TestZipkinSpan) SpanID() uint64               { return s.spanID }
64func (s TestZipkinSpan) Flags() byte                  { return s.flags }
65func (s *TestZipkinSpan) SetTraceID(traceID uint64)   { s.traceID = traceID }
66func (s *TestZipkinSpan) SetSpanID(spanID uint64)     { s.spanID = spanID }
67func (s *TestZipkinSpan) SetParentID(parentID uint64) { s.parentID = parentID }
68func (s *TestZipkinSpan) SetFlags(flags byte)         { s.flags = flags }
69