...

Source file src/go.opentelemetry.io/otel/propagation/propagation_test.go

Documentation: go.opentelemetry.io/otel/propagation

     1  // Copyright The OpenTelemetry Authors
     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  
    15  package propagation_test
    16  
    17  import (
    18  	"context"
    19  	"sort"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"go.opentelemetry.io/otel/propagation"
    26  )
    27  
    28  type ctxKeyType uint
    29  
    30  var ctxKey ctxKeyType
    31  
    32  type carrier []string
    33  
    34  func (c *carrier) Keys() []string { return nil }
    35  
    36  func (c *carrier) Get(string) string { return "" }
    37  
    38  func (c *carrier) Set(setter, _ string) {
    39  	*c = append(*c, setter)
    40  }
    41  
    42  type propagator struct {
    43  	Name string
    44  }
    45  
    46  func (p propagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
    47  	carrier.Set(p.Name, "")
    48  }
    49  
    50  func (p propagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
    51  	v := ctx.Value(ctxKey)
    52  	if v == nil {
    53  		ctx = context.WithValue(ctx, ctxKey, []string{p.Name})
    54  	} else {
    55  		orig := v.([]string)
    56  		ctx = context.WithValue(ctx, ctxKey, append(orig, p.Name))
    57  	}
    58  	return ctx
    59  }
    60  
    61  func (p propagator) Fields() []string { return []string{p.Name} }
    62  
    63  func TestCompositeTextMapPropagatorFields(t *testing.T) {
    64  	a, b1, b2 := propagator{"a"}, propagator{"b"}, propagator{"b"}
    65  
    66  	want := map[string]struct{}{
    67  		"a": {},
    68  		"b": {},
    69  	}
    70  	got := propagation.NewCompositeTextMapPropagator(a, b1, b2).Fields()
    71  	if len(got) != len(want) {
    72  		t.Fatalf("invalid fields from composite: %v (want %v)", got, want)
    73  	}
    74  	for _, v := range got {
    75  		if _, ok := want[v]; !ok {
    76  			t.Errorf("invalid field returned from composite: %q", v)
    77  		}
    78  	}
    79  }
    80  
    81  func TestCompositeTextMapPropagatorInject(t *testing.T) {
    82  	a, b := propagator{"a"}, propagator{"b"}
    83  
    84  	c := make(carrier, 0, 2)
    85  	propagation.NewCompositeTextMapPropagator(a, b).Inject(context.Background(), &c)
    86  
    87  	if got := strings.Join([]string(c), ","); got != "a,b" {
    88  		t.Errorf("invalid inject order: %s", got)
    89  	}
    90  }
    91  
    92  func TestCompositeTextMapPropagatorExtract(t *testing.T) {
    93  	a, b := propagator{"a"}, propagator{"b"}
    94  
    95  	ctx := context.Background()
    96  	ctx = propagation.NewCompositeTextMapPropagator(a, b).Extract(ctx, nil)
    97  
    98  	v := ctx.Value(ctxKey)
    99  	if v == nil {
   100  		t.Fatal("no composite extraction")
   101  	}
   102  	if got := strings.Join(v.([]string), ","); got != "a,b" {
   103  		t.Errorf("invalid extract order: %s", got)
   104  	}
   105  }
   106  
   107  func TestMapCarrierGet(t *testing.T) {
   108  	carrier := propagation.MapCarrier{
   109  		"foo": "bar",
   110  		"baz": "qux",
   111  	}
   112  
   113  	assert.Equal(t, carrier.Get("foo"), "bar")
   114  	assert.Equal(t, carrier.Get("baz"), "qux")
   115  }
   116  
   117  func TestMapCarrierSet(t *testing.T) {
   118  	carrier := make(propagation.MapCarrier)
   119  	carrier.Set("foo", "bar")
   120  	carrier.Set("baz", "qux")
   121  
   122  	assert.Equal(t, carrier["foo"], "bar")
   123  	assert.Equal(t, carrier["baz"], "qux")
   124  }
   125  
   126  func TestMapCarrierKeys(t *testing.T) {
   127  	carrier := propagation.MapCarrier{
   128  		"foo": "bar",
   129  		"baz": "qux",
   130  	}
   131  
   132  	keys := carrier.Keys()
   133  	sort.Strings(keys)
   134  	assert.Equal(t, []string{"baz", "foo"}, keys)
   135  }
   136  

View as plain text