From 5847d52cc96543698bab4884ad86ca4c69110bb8 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Wed, 6 Dec 2023 21:10:12 +0100 Subject: [PATCH] filters/tracing: add allocations test for stateBagToTag (#2779) Followup on https://github.com/zalando/skipper/pull/2775 Signed-off-by: Alexander Yastrebov --- filters/tracing/statebagtotag_test.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/filters/tracing/statebagtotag_test.go b/filters/tracing/statebagtotag_test.go index af487cdcbd..66b1a4c3b7 100644 --- a/filters/tracing/statebagtotag_test.go +++ b/filters/tracing/statebagtotag_test.go @@ -18,8 +18,7 @@ func TestStateBagToTag(t *testing.T) { span := tracingtest.NewSpan("start_span") req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) - ctx := &filtertest.Context{FRequest: req, FStateBag: make(map[string]interface{})} - ctx.StateBag()["item"] = "val" + ctx := &filtertest.Context{FRequest: req, FStateBag: map[string]interface{}{"item": "val"}} f, err := NewStateBagToTag().CreateFilter([]interface{}{"item", "tag"}) require.NoError(t, err) @@ -29,6 +28,24 @@ func TestStateBagToTag(t *testing.T) { assert.Equal(t, "val", span.Tags["tag"]) } +func TestStateBagToTagAllocs(t *testing.T) { + req := &http.Request{Header: http.Header{}} + + span := tracingtest.NewSpan("start_span") + req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) + ctx := &filtertest.Context{FRequest: req, FStateBag: map[string]interface{}{"item": "val"}} + + f, err := NewStateBagToTag().CreateFilter([]interface{}{"item", "tag"}) + require.NoError(t, err) + + allocs := testing.AllocsPerRun(100, func() { + f.Request(ctx) + }) + if allocs != 0.0 { + t.Errorf("Expected zero allocations, got %f", allocs) + } +} + func TestStateBagToTag_CreateFilter(t *testing.T) { for _, ti := range []struct { msg string