...

Source file src/go.opencensus.io/metric/metricproducer/manager_test.go

Documentation: go.opencensus.io/metric/metricproducer

     1  // Copyright 2019, 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 metricproducer
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.opencensus.io/metric/metricdata"
    21  )
    22  
    23  type testProducer struct {
    24  	name string
    25  }
    26  
    27  var (
    28  	myProd1 = newTestProducer("foo")
    29  	myProd2 = newTestProducer("bar")
    30  	myProd3 = newTestProducer("foobar")
    31  	pm      = GlobalManager()
    32  )
    33  
    34  func newTestProducer(name string) *testProducer {
    35  	return &testProducer{name}
    36  }
    37  
    38  func (mp *testProducer) Read() []*metricdata.Metric {
    39  	return nil
    40  }
    41  
    42  func TestAdd(t *testing.T) {
    43  	pm.AddProducer(myProd1)
    44  	pm.AddProducer(myProd2)
    45  
    46  	got := pm.GetAll()
    47  	want := []*testProducer{myProd1, myProd2}
    48  	checkSlice(got, want, t)
    49  	deleteAll()
    50  }
    51  
    52  func TestAddExisting(t *testing.T) {
    53  	pm.AddProducer(myProd1)
    54  	pm.AddProducer(myProd2)
    55  	pm.AddProducer(myProd1)
    56  
    57  	got := pm.GetAll()
    58  	want := []*testProducer{myProd2, myProd1}
    59  	checkSlice(got, want, t)
    60  	deleteAll()
    61  }
    62  
    63  func TestAddNil(t *testing.T) {
    64  	pm.AddProducer(nil)
    65  
    66  	got := pm.GetAll()
    67  	want := []*testProducer{}
    68  	checkSlice(got, want, t)
    69  	deleteAll()
    70  }
    71  
    72  func TestDelete(t *testing.T) {
    73  	pm.AddProducer(myProd1)
    74  	pm.AddProducer(myProd2)
    75  	pm.AddProducer(myProd3)
    76  	pm.DeleteProducer(myProd2)
    77  
    78  	got := pm.GetAll()
    79  	want := []*testProducer{myProd1, myProd3}
    80  	checkSlice(got, want, t)
    81  	deleteAll()
    82  }
    83  
    84  func TestDeleteNonExisting(t *testing.T) {
    85  	pm.AddProducer(myProd1)
    86  	pm.AddProducer(myProd3)
    87  	pm.DeleteProducer(myProd2)
    88  
    89  	got := pm.GetAll()
    90  	want := []*testProducer{myProd1, myProd3}
    91  	checkSlice(got, want, t)
    92  	deleteAll()
    93  }
    94  
    95  func TestDeleteNil(t *testing.T) {
    96  	pm.AddProducer(myProd1)
    97  	pm.AddProducer(myProd3)
    98  	pm.DeleteProducer(nil)
    99  
   100  	got := pm.GetAll()
   101  	want := []*testProducer{myProd1, myProd3}
   102  	checkSlice(got, want, t)
   103  	deleteAll()
   104  }
   105  
   106  func TestGetAllNil(t *testing.T) {
   107  	got := pm.GetAll()
   108  	want := []*testProducer{}
   109  	checkSlice(got, want, t)
   110  	deleteAll()
   111  }
   112  
   113  func TestImmutableProducerList(t *testing.T) {
   114  	pm.AddProducer(myProd1)
   115  	pm.AddProducer(myProd2)
   116  
   117  	producersToMutate := pm.GetAll()
   118  	producersToMutate[0] = myProd3
   119  	got := pm.GetAll()
   120  	want := []*testProducer{myProd1, myProd2}
   121  	checkSlice(got, want, t)
   122  	deleteAll()
   123  }
   124  
   125  func checkSlice(got []Producer, want []*testProducer, t *testing.T) {
   126  	gotLen := len(got)
   127  	wantLen := len(want)
   128  	if gotLen != wantLen {
   129  		t.Errorf("got len: %d want: %d\n", gotLen, wantLen)
   130  	} else {
   131  		gotMap := map[Producer]struct{}{}
   132  		for i := 0; i < gotLen; i++ {
   133  			gotMap[got[i]] = struct{}{}
   134  		}
   135  		for i := 0; i < wantLen; i++ {
   136  			delete(gotMap, want[i])
   137  		}
   138  		if len(gotMap) > 0 {
   139  			t.Errorf("got %v, want %v\n", got, want)
   140  		}
   141  	}
   142  }
   143  
   144  func deleteAll() {
   145  	pm.DeleteProducer(myProd1)
   146  	pm.DeleteProducer(myProd2)
   147  	pm.DeleteProducer(myProd3)
   148  }
   149  

View as plain text