...

Source file src/go.opencensus.io/stats/view/collector_test.go

Documentation: go.opencensus.io/stats/view

     1  // Copyright 2017, OpenCensus 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 view
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.opencensus.io/tag"
    22  )
    23  
    24  func TestEncodeDecodeTags(t *testing.T) {
    25  	ctx := context.Background()
    26  	type testData struct {
    27  		m    *tag.Map
    28  		keys []tag.Key
    29  		want map[tag.Key][]byte
    30  	}
    31  
    32  	k1 = tag.MustNewKey("/encodedecodetest/k1")
    33  	k2 = tag.MustNewKey("/encodedecodetest/k2")
    34  	k3 = tag.MustNewKey("/encodedecodetest/k3")
    35  
    36  	ctx1, _ := tag.New(ctx)
    37  	ctx2, _ := tag.New(ctx, tag.Insert(k2, "v2"))
    38  	ctx3, _ := tag.New(ctx, tag.Insert(k1, "v1"), tag.Insert(k2, "v2"))
    39  	ctx4, _ := tag.New(ctx, tag.Insert(k1, "v1"), tag.Insert(k2, "v2"), tag.Insert(k3, "v3"))
    40  
    41  	m1 := tag.FromContext(ctx1)
    42  	m2 := tag.FromContext(ctx2)
    43  	m3 := tag.FromContext(ctx3)
    44  	m4 := tag.FromContext(ctx4)
    45  
    46  	tests := []testData{
    47  		{
    48  			m1,
    49  			[]tag.Key{k1},
    50  			nil,
    51  		},
    52  		{
    53  			m2,
    54  			[]tag.Key{},
    55  			nil,
    56  		},
    57  		{
    58  			m2,
    59  			[]tag.Key{k1},
    60  			nil,
    61  		},
    62  		{
    63  			m2,
    64  			[]tag.Key{k2},
    65  			map[tag.Key][]byte{
    66  				k2: []byte("v2"),
    67  			},
    68  		},
    69  		{
    70  			m3,
    71  			[]tag.Key{k1},
    72  			map[tag.Key][]byte{
    73  				k1: []byte("v1"),
    74  			},
    75  		},
    76  		{
    77  			m3,
    78  			[]tag.Key{k1, k2},
    79  			map[tag.Key][]byte{
    80  				k1: []byte("v1"),
    81  				k2: []byte("v2"),
    82  			},
    83  		},
    84  		{
    85  			m4,
    86  			[]tag.Key{k3, k1},
    87  			map[tag.Key][]byte{
    88  				k1: []byte("v1"),
    89  				k3: []byte("v3"),
    90  			},
    91  		},
    92  	}
    93  
    94  	for label, tt := range tests {
    95  		tags := decodeTags(encodeWithKeys(tt.m, tt.keys), tt.keys)
    96  		if got, want := len(tags), len(tt.want); got != want {
    97  			t.Fatalf("%d: len(decoded) = %v; not %v", label, got, want)
    98  		}
    99  
   100  		for _, tag := range tags {
   101  			if _, ok := tt.want[tag.Key]; !ok {
   102  				t.Errorf("%d: missing key %v", label, tag.Key)
   103  			}
   104  			if got, want := tag.Value, string(tt.want[tag.Key]); got != want {
   105  				t.Errorf("%d: got value %q; want %q", label, got, want)
   106  			}
   107  		}
   108  	}
   109  }
   110  

View as plain text