...

Source file src/go.opentelemetry.io/otel/internal/internaltest/text_map_propagator_test.go

Documentation: go.opentelemetry.io/otel/internal/internaltest

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/internaltest/text_map_propagator_test.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package internaltest
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  )
    24  
    25  func TestTextMapPropagatorInjectExtract(t *testing.T) {
    26  	name := "testing"
    27  	ctx := context.Background()
    28  	carrier := NewTextMapCarrier(map[string]string{name: value})
    29  	propagator := NewTextMapPropagator(name)
    30  
    31  	propagator.Inject(ctx, carrier)
    32  	// Carrier value overridden with state.
    33  	if carrier.SetKeyValue(t, name, "1,0") {
    34  		// Ensure nothing has been extracted yet.
    35  		propagator.ExtractedN(t, ctx, 0)
    36  		// Test the injection was counted.
    37  		propagator.InjectedN(t, carrier, 1)
    38  	}
    39  
    40  	ctx = propagator.Extract(ctx, carrier)
    41  	v := ctx.Value(ctxKeyType(name))
    42  	if v == nil {
    43  		t.Error("TextMapPropagator.Extract failed to extract state")
    44  	}
    45  	if s, ok := v.(state); !ok {
    46  		t.Error("TextMapPropagator.Extract did not extract proper state")
    47  	} else if s.Extractions != 1 {
    48  		t.Error("TextMapPropagator.Extract did not increment state.Extractions")
    49  	}
    50  	if carrier.GotKey(t, name) {
    51  		// Test the extraction was counted.
    52  		propagator.ExtractedN(t, ctx, 1)
    53  		// Ensure no additional injection was recorded.
    54  		propagator.InjectedN(t, carrier, 1)
    55  	}
    56  }
    57  
    58  func TestTextMapPropagatorFields(t *testing.T) {
    59  	name := "testing"
    60  	propagator := NewTextMapPropagator(name)
    61  	if got := propagator.Fields(); len(got) != 1 {
    62  		t.Errorf("TextMapPropagator.Fields returned %d fields, want 1", len(got))
    63  	} else if got[0] != name {
    64  		t.Errorf("TextMapPropagator.Fields returned %q, want %q", got[0], name)
    65  	}
    66  }
    67  
    68  func TestNewStateEmpty(t *testing.T) {
    69  	if want, got := (state{}), newState(""); got != want {
    70  		t.Errorf("newState(\"\") returned %v, want %v", got, want)
    71  	}
    72  }
    73  

View as plain text