From b45a5ba5a3fcbb67aa996cd9b10c11580a7f5479 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Mon, 26 Sep 2016 15:54:28 -0400 Subject: [PATCH] Verify that baggage is passed through to the child span (#44) --- span_test.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/span_test.go b/span_test.go index 73c6ec88..204897e8 100644 --- a/span_test.go +++ b/span_test.go @@ -1,8 +1,10 @@ package jaeger import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/opentracing/opentracing-go" + "github.com/stretchr/testify/assert" ) func TestBaggageIterator(t *testing.T) { @@ -12,20 +14,28 @@ func TestBaggageIterator(t *testing.T) { sp1 := tracer.StartSpan("s1").(*span) sp1.SetBaggageItem("Some_Key", "12345") sp1.SetBaggageItem("Some-other-key", "42") + expectedBaggage := map[string]string{"some-key": "12345", "some-other-key": "42"} + assertBaggage(t, sp1, expectedBaggage) - b := make(map[string]string) - sp1.Context().ForeachBaggageItem(func(k, v string) bool { - b[k] = v - return true - }) - assert.Equal(t, map[string]string{"some-key": "12345", "some-other-key": "42"}, b) + b := extractBaggage(sp1, false) // break out early + assert.Equal(t, 1, len(b), "only one baggage item should be extracted") - b = make(map[string]string) - sp1.Context().ForeachBaggageItem(func(k, v string) bool { + sp2 := tracer.StartSpan("s2", opentracing.ChildOf(sp1.Context())) + assertBaggage(t, sp2, expectedBaggage) // child inherits the same baggage +} + +func assertBaggage(t *testing.T, sp opentracing.Span, expected map[string]string) { + b := extractBaggage(sp, true) + assert.Equal(t, expected, b) +} + +func extractBaggage(sp opentracing.Span, allItems bool) map[string]string { + b := make(map[string]string) + sp.Context().ForeachBaggageItem(func(k, v string) bool { b[k] = v - return false // break out early + return allItems }) - assert.Equal(t, 1, len(b), "only one baggage item should be extracted") + return b } func TestSpanProperties(t *testing.T) {