From 5bd9c59bf95bc43952c41f325edd5105d6ecbc1c Mon Sep 17 00:00:00 2001 From: Zvi Cahana Date: Thu, 24 May 2018 09:47:55 +0300 Subject: [PATCH] Fix baggage/debug header tests to not defer in loops This patch fixes the unit tests for baggage/debug headers so that the "defer closer.Close()" statement is not executed in a for loop, but rather within a nested test function execution. Signed-off-by: Zvi Cahana --- propagation_test.go | 120 ++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/propagation_test.go b/propagation_test.go index 795b6838..fb419af2 100644 --- a/propagation_test.go +++ b/propagation_test.go @@ -191,33 +191,33 @@ func TestJaegerBaggageHeader(t *testing.T) { } for _, testcase := range testcases { - - metricsFactory, metrics := initMetrics() - tracer, closer := NewTracer("DOOP", - NewConstSampler(true), - NewNullReporter(), - TracerOptions.Metrics(metrics), - ) - defer closer.Close() - - h := http.Header{} - h.Add(JaegerBaggageHeader, "key1=value1, key 2=value two") - - ctx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(h)) - require.NoError(t, err) - - sp := tracer.StartSpan("root", testcase.refFunc(ctx)).(*Span) - - assert.Equal(t, "value1", sp.BaggageItem("key1")) - assert.Equal(t, "value two", sp.BaggageItem("key 2")) - - // ensure that traces.started counter is incremented, not traces.joined - testutils.AssertCounterMetrics(t, metricsFactory, - testutils.ExpectedMetric{ - Name: "jaeger.traces", Tags: map[string]string{"state": "started", "sampled": "y"}, Value: 1, - }, - ) - + t.Run("", func(t *testing.T) { + metricsFactory, metrics := initMetrics() + tracer, closer := NewTracer("DOOP", + NewConstSampler(true), + NewNullReporter(), + TracerOptions.Metrics(metrics), + ) + defer closer.Close() + + h := http.Header{} + h.Add(JaegerBaggageHeader, "key1=value1, key 2=value two") + + ctx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(h)) + require.NoError(t, err) + + sp := tracer.StartSpan("root", testcase.refFunc(ctx)).(*Span) + + assert.Equal(t, "value1", sp.BaggageItem("key1")) + assert.Equal(t, "value two", sp.BaggageItem("key 2")) + + // ensure that traces.started counter is incremented, not traces.joined + testutils.AssertCounterMetrics(t, metricsFactory, + testutils.ExpectedMetric{ + Name: "jaeger.traces", Tags: map[string]string{"state": "started", "sampled": "y"}, Value: 1, + }, + ) + }) } } @@ -258,38 +258,38 @@ func TestDebugCorrelationID(t *testing.T) { } for _, testcase := range testcases { - - metricsFactory, metrics := initMetrics() - tracer, closer := NewTracer("DOOP", - NewConstSampler(true), - NewNullReporter(), - TracerOptions.Metrics(metrics), - ) - defer closer.Close() - - h := http.Header{} - val := "value1" - h.Add(JaegerDebugHeader, val) - ctx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(h)) - require.NoError(t, err) - assert.EqualValues(t, 0, ctx.(SpanContext).parentID) - assert.EqualValues(t, val, ctx.(SpanContext).debugID) - sp := tracer.StartSpan("root", testcase.refFunc(ctx)).(*Span) - assert.EqualValues(t, 0, sp.context.parentID) - assert.True(t, sp.context.traceID.IsValid()) - assert.True(t, sp.context.IsSampled()) - assert.True(t, sp.context.IsDebug()) - - tag := findDomainTag(sp, JaegerDebugHeader) - assert.NotNil(t, tag) - assert.Equal(t, val, tag.value) - - // ensure that traces.started counter is incremented, not traces.joined - testutils.AssertCounterMetrics(t, metricsFactory, - testutils.ExpectedMetric{ - Name: "jaeger.traces", Tags: map[string]string{"state": "started", "sampled": "y"}, Value: 1, - }, - ) - + t.Run("", func(t *testing.T) { + metricsFactory, metrics := initMetrics() + tracer, closer := NewTracer("DOOP", + NewConstSampler(true), + NewNullReporter(), + TracerOptions.Metrics(metrics), + ) + defer closer.Close() + + h := http.Header{} + val := "value1" + h.Add(JaegerDebugHeader, val) + ctx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(h)) + require.NoError(t, err) + assert.EqualValues(t, 0, ctx.(SpanContext).parentID) + assert.EqualValues(t, val, ctx.(SpanContext).debugID) + sp := tracer.StartSpan("root", testcase.refFunc(ctx)).(*Span) + assert.EqualValues(t, 0, sp.context.parentID) + assert.True(t, sp.context.traceID.IsValid()) + assert.True(t, sp.context.IsSampled()) + assert.True(t, sp.context.IsDebug()) + + tag := findDomainTag(sp, JaegerDebugHeader) + assert.NotNil(t, tag) + assert.Equal(t, val, tag.value) + + // ensure that traces.started counter is incremented, not traces.joined + testutils.AssertCounterMetrics(t, metricsFactory, + testutils.ExpectedMetric{ + Name: "jaeger.traces", Tags: map[string]string{"state": "started", "sampled": "y"}, Value: 1, + }, + ) + }) } }