func TestInMemoryRecorderTags(t *testing.T) {
	recorder := testutils.NewInMemoryRecorder("unit-test")
	recorder.SetTag("tag1", "hello")
	if len(recorder.GetTags()) != 1 {
		t.Fatal("Tag not stored")
	}
	if recorder.GetTags()["tag1"] != "hello" {
		t.Fatal("tag1 != hello")
	}
}
func TestSpanPropagator(t *testing.T) {
	const op = "test"
	recorder := testutils.NewInMemoryRecorder()
	tracer := standardtracer.New(recorder)

	sp := tracer.StartTrace(op)
	sp.SetTraceAttribute("foo", "bar")

	textCtx, textAttrs := tracer.PropagateSpanAsText(sp)
	binCtx, binAttrs := tracer.PropagateSpanAsBinary(sp)

	sp1, err := tracer.JoinTraceFromText(op, textCtx, textAttrs)
	if err != nil {
		t.Fatal(err)
	}
	sp2, err := tracer.JoinTraceFromBinary(op, binCtx, binAttrs)
	if err != nil {
		t.Fatal(err)
	}
	sp.Finish()
	for _, sp := range []opentracing.Span{sp1, sp2} {
		sp.Finish()
	}

	spans := recorder.GetSpans()
	if a, e := len(spans), 3; a != e {
		t.Fatalf("expected %d spans, got %d", e, a)
	}

	exp := spans[0]
	exp.Duration = time.Duration(123)
	exp.Start = time.Time{}.Add(1)

	for i, sp := range spans[1:] {
		if a, e := sp.ParentSpanID, exp.SpanID; a != e {
			t.Errorf("%d: ParentSpanID %d does not match expectation %d", i, a, e)
		} else {
			// Prepare for comparison.
			sp.SpanID, sp.ParentSpanID = exp.SpanID, 0
			sp.Duration, sp.Start = exp.Duration, exp.Start
		}
		if a, e := sp.TraceID, exp.TraceID; a != e {
			t.Errorf("%d: TraceID changed from %d to %d", i, e, a)
		}
		if !reflect.DeepEqual(exp, sp) {
			t.Errorf("%d: wanted %+v, got %+v", i, spew.Sdump(exp), spew.Sdump(sp))
		}
	}
}
func TestInMemoryRecorderSpans(t *testing.T) {
	recorder := testutils.NewInMemoryRecorder()
	var apiRecorder standardtracer.SpanRecorder = recorder
	span := &standardtracer.RawSpan{
		StandardContext: &standardtracer.StandardContext{},
		Operation:       "test-span",
		Start:           time.Now(),
		Duration:        -1,
	}
	apiRecorder.RecordSpan(span)
	if len(recorder.GetSpans()) != 1 {
		t.Fatal("No spans recorded")
	}
	if recorder.GetSpans()[0] != span {
		t.Fatal("Span not recorded")
	}
}
func TestInMemoryRecorderSpans(t *testing.T) {
	recorder := testutils.NewInMemoryRecorder("unit-test")
	var apiRecorder standardtracer.Recorder = recorder
	if apiRecorder.ProcessName() != "unit-test" {
		t.Fatalf("Invalid process name")
	}
	span := &standardtracer.RawSpan{
		TraceContext: &dapperish.TraceContext{},
		Operation:    "test-span",
		Start:        time.Now(),
		Duration:     -1,
	}
	apiRecorder.RecordSpan(span)
	if len(recorder.GetSpans()) != 1 {
		t.Fatal("No spans recorded")
	}
	if recorder.GetSpans()[0] != span {
		t.Fatal("Span not recorded")
	}
}
func TestPeerTags(t *testing.T) {
	if ext.PeerService != "peer.service" {
		t.Fatalf("Invalid PeerService %v", ext.PeerService)
	}
	recorder := testutils.NewInMemoryRecorder()
	tracer := standardtracer.New(recorder)
	span := tracer.StartTrace("my-trace")
	ext.PeerService.Add(span, "my-service")
	ext.PeerHostname.Add(span, "my-hostname")
	ext.PeerHostIPv4.Add(span, uint32(127<<24|1))
	ext.PeerHostIPv6.Add(span, "::")
	ext.PeerPort.Add(span, uint16(8080))
	span.Finish()
	if len(recorder.GetSpans()) != 1 {
		t.Fatal("Span not recorded")
	}
	rawSpan := recorder.GetSpans()[0]
	assert.EqualValues(t, "my-service", rawSpan.Tags[string(ext.PeerService)])
	assert.EqualValues(t, "my-hostname", rawSpan.Tags[string(ext.PeerHostname)])
	assert.EqualValues(t, uint32(127<<24|1), rawSpan.Tags[string(ext.PeerHostIPv4)])
	assert.EqualValues(t, "::", rawSpan.Tags[string(ext.PeerHostIPv6)])
	assert.EqualValues(t, 8080, rawSpan.Tags[string(ext.PeerPort)])
}