github.com/xmidt-org/webpa-common@v1.11.9/convey/conveymetric/metric_test.go (about)

     1  package conveymetric
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/xmidt-org/webpa-common/convey"
     7  	"github.com/xmidt-org/webpa-common/xmetrics"
     8  	"github.com/xmidt-org/webpa-common/xmetrics/xmetricstest"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestConveyMetric(t *testing.T) {
    14  	assert := assert.New(t)
    15  
    16  	gauge := xmetricstest.NewGauge("hardware")
    17  
    18  	conveyMetric := NewConveyMetric(gauge, []TagLabelPair{{"hw-model", "model"}, {"fw-name", "firmware"}}...)
    19  
    20  	dec, err := conveyMetric.Update(convey.C{"data": "neat", "hw-model": "hardware123abc", "fw-name": "firmware-xyz"})
    21  	assert.NoError(err)
    22  	assert.Equal(float64(1), gauge.With("model", "hardware123abc", "firmware", "firmware-xyz").(xmetrics.Valuer).Value())
    23  	// remove the update
    24  	dec()
    25  
    26  	assert.Equal(float64(0), gauge.With("model", "hardware123abc", "firmware", "firmware-xyz").(xmetrics.Valuer).Value())
    27  
    28  	// try with no `hw_model`
    29  	dec, err = conveyMetric.Update(convey.C{"data": "neat", "fw-name": "firmware-abc"})
    30  	t.Logf("%v+", gauge)
    31  	assert.Equal(float64(1), gauge.With("model", UnknownLabelValue, "firmware", "firmware-abc").(xmetrics.Valuer).Value())
    32  
    33  	// remove the update
    34  	dec()
    35  	assert.Equal(float64(0), gauge.With("model", UnknownLabelValue, "firmware", "firmware-abc").(xmetrics.Valuer).Value())
    36  }