github.com/m3db/m3@v1.5.0/src/x/tallytest/tallytest.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package tallytest
    22  
    23  import (
    24  	"fmt"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/uber-go/tally"
    30  )
    31  
    32  // AssertCounterValue asserts that the given counter has the expected value.
    33  func AssertCounterValue(t *testing.T, expected int64, s tally.Snapshot, name string, tags map[string]string) bool {
    34  	index := flattenMetricIndex(name, tags)
    35  	counter := s.Counters()[index]
    36  	notFound := fmt.Sprintf("not found: key=%s, actual=%v", index, counterKeys(s.Counters()))
    37  	if !assert.NotNil(t, counter, notFound) {
    38  		return false
    39  	}
    40  	mismatch := fmt.Sprintf("current values: %v", CounterMap(s.Counters()))
    41  	return assert.Equal(t, expected, counter.Value(), mismatch)
    42  }
    43  
    44  // AssertGaugeValue asserts that the given gauge has the expected value.
    45  func AssertGaugeValue(t *testing.T, expected float64, s tally.Snapshot, name string, tags map[string]string) bool {
    46  	index := flattenMetricIndex(name, tags)
    47  	gauge := s.Gauges()[index]
    48  	notFound := fmt.Sprintf("not found: key=%s, actual=%v", index, gaugeKeys(s.Gauges()))
    49  	if !assert.NotNil(t, gauge, notFound) {
    50  		return false
    51  	}
    52  	mismatch := fmt.Sprintf("current values: %v", GaugeMap(s.Gauges()))
    53  	return assert.InDelta(t, expected, gauge.Value(), 0.0001, mismatch)
    54  }
    55  
    56  // AssertGaugeNil asserts that the given gauge does not exist.
    57  func AssertGaugeNil(t *testing.T, s tally.Snapshot, name string, tags map[string]string) bool {
    58  	index := flattenMetricIndex(name, tags)
    59  	gauge := s.Gauges()[index]
    60  	found := fmt.Sprintf("found: key=%s, actual=%v", index, gaugeKeys(s.Gauges()))
    61  	return assert.Nil(t, gauge, found)
    62  }
    63  
    64  func flattenMetricIndex(name string, tags map[string]string) string {
    65  	keys := make([]string, 0, len(tags))
    66  	for k := range tags {
    67  		keys = append(keys, k)
    68  	}
    69  	sort.Strings(keys)
    70  
    71  	index := name + "+"
    72  	for i, k := range keys {
    73  		sep := ""
    74  		if i != 0 {
    75  			sep = ","
    76  		}
    77  
    78  		index += fmt.Sprintf("%s%s=%s", sep, k, tags[k])
    79  	}
    80  
    81  	return index
    82  }
    83  
    84  // CounterMap returns the counter map for a snapshot.
    85  func CounterMap(m map[string]tally.CounterSnapshot) map[string]int64 {
    86  	result := make(map[string]int64, len(m))
    87  	for k, v := range m {
    88  		result[k] = v.Value()
    89  	}
    90  	return result
    91  }
    92  
    93  // GaugeMap returns the gauge map for a snapshot.
    94  func GaugeMap(m map[string]tally.GaugeSnapshot) map[string]float64 {
    95  	result := make(map[string]float64, len(m))
    96  	for k, v := range m {
    97  		result[k] = v.Value()
    98  	}
    99  	return result
   100  }
   101  
   102  func counterKeys(m map[string]tally.CounterSnapshot) []string {
   103  	r := make([]string, 0, len(m))
   104  	for k := range m {
   105  		r = append(r, k)
   106  	}
   107  	return r
   108  }
   109  
   110  func gaugeKeys(m map[string]tally.GaugeSnapshot) []string {
   111  	r := make([]string, 0, len(m))
   112  	for k := range m {
   113  		r = append(r, k)
   114  	}
   115  	return r
   116  }