...

Source file src/go.opentelemetry.io/otel/sdk/internal/internaltest/text_map_carrier.go

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

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/internaltest/text_map_carrier.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 // import "go.opentelemetry.io/otel/sdk/internal/internaltest"
    19  
    20  import (
    21  	"sync"
    22  	"testing"
    23  
    24  	"go.opentelemetry.io/otel/propagation"
    25  )
    26  
    27  // TextMapCarrier is a storage medium for a TextMapPropagator used in testing.
    28  // The methods of a TextMapCarrier are concurrent safe.
    29  type TextMapCarrier struct {
    30  	mtx sync.Mutex
    31  
    32  	gets []string
    33  	sets [][2]string
    34  	data map[string]string
    35  }
    36  
    37  var _ propagation.TextMapCarrier = (*TextMapCarrier)(nil)
    38  
    39  // NewTextMapCarrier returns a new *TextMapCarrier populated with data.
    40  func NewTextMapCarrier(data map[string]string) *TextMapCarrier {
    41  	copied := make(map[string]string, len(data))
    42  	for k, v := range data {
    43  		copied[k] = v
    44  	}
    45  	return &TextMapCarrier{data: copied}
    46  }
    47  
    48  // Keys returns the keys for which this carrier has a value.
    49  func (c *TextMapCarrier) Keys() []string {
    50  	c.mtx.Lock()
    51  	defer c.mtx.Unlock()
    52  
    53  	result := make([]string, 0, len(c.data))
    54  	for k := range c.data {
    55  		result = append(result, k)
    56  	}
    57  	return result
    58  }
    59  
    60  // Get returns the value associated with the passed key.
    61  func (c *TextMapCarrier) Get(key string) string {
    62  	c.mtx.Lock()
    63  	defer c.mtx.Unlock()
    64  	c.gets = append(c.gets, key)
    65  	return c.data[key]
    66  }
    67  
    68  // GotKey tests if c.Get has been called for key.
    69  func (c *TextMapCarrier) GotKey(t *testing.T, key string) bool {
    70  	c.mtx.Lock()
    71  	defer c.mtx.Unlock()
    72  	for _, k := range c.gets {
    73  		if k == key {
    74  			return true
    75  		}
    76  	}
    77  	t.Errorf("TextMapCarrier.Get(%q) has not been called", key)
    78  	return false
    79  }
    80  
    81  // GotN tests if n calls to c.Get have been made.
    82  func (c *TextMapCarrier) GotN(t *testing.T, n int) bool {
    83  	c.mtx.Lock()
    84  	defer c.mtx.Unlock()
    85  	if len(c.gets) != n {
    86  		t.Errorf("TextMapCarrier.Get was called %d times, not %d", len(c.gets), n)
    87  		return false
    88  	}
    89  	return true
    90  }
    91  
    92  // Set stores the key-value pair.
    93  func (c *TextMapCarrier) Set(key, value string) {
    94  	c.mtx.Lock()
    95  	defer c.mtx.Unlock()
    96  	c.sets = append(c.sets, [2]string{key, value})
    97  	c.data[key] = value
    98  }
    99  
   100  // SetKeyValue tests if c.Set has been called for the key-value pair.
   101  func (c *TextMapCarrier) SetKeyValue(t *testing.T, key, value string) bool {
   102  	c.mtx.Lock()
   103  	defer c.mtx.Unlock()
   104  	var vals []string
   105  	for _, pair := range c.sets {
   106  		if key == pair[0] {
   107  			if value == pair[1] {
   108  				return true
   109  			}
   110  			vals = append(vals, pair[1])
   111  		}
   112  	}
   113  	if len(vals) > 0 {
   114  		t.Errorf("TextMapCarrier.Set called with %q and %v values, but not %s", key, vals, value)
   115  	}
   116  	t.Errorf("TextMapCarrier.Set(%q,%q) has not been called", key, value)
   117  	return false
   118  }
   119  
   120  // SetN tests if n calls to c.Set have been made.
   121  func (c *TextMapCarrier) SetN(t *testing.T, n int) bool {
   122  	c.mtx.Lock()
   123  	defer c.mtx.Unlock()
   124  	if len(c.sets) != n {
   125  		t.Errorf("TextMapCarrier.Set was called %d times, not %d", len(c.sets), n)
   126  		return false
   127  	}
   128  	return true
   129  }
   130  
   131  // Reset zeros out the recording state and sets the carried values to data.
   132  func (c *TextMapCarrier) Reset(data map[string]string) {
   133  	copied := make(map[string]string, len(data))
   134  	for k, v := range data {
   135  		copied[k] = v
   136  	}
   137  
   138  	c.mtx.Lock()
   139  	defer c.mtx.Unlock()
   140  
   141  	c.gets = nil
   142  	c.sets = nil
   143  	c.data = copied
   144  }
   145  

View as plain text