aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/prometheus/counter_test.go
blob: 04f0c894f876ff9156f1a6d492096233e0906258 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package prometheus

import (
	"testing"

	"github.com/prometheus/client_golang/prometheus"
	dto "github.com/prometheus/client_model/go"
	"github.com/stretchr/testify/assert"
)

func TestCounter_Add(t *testing.T) {
	c := &Counter{cnt: prometheus.NewCounter(prometheus.CounterOpts{
		Name: "test_counter_add",
	})}

	var expectValue int64 = 42
	c.Add(expectValue)

	var res dto.Metric
	err := c.cnt.Write(&res)

	assert.NoError(t, err)
	assert.Equal(t, expectValue, int64(res.GetCounter().GetValue()))
}

func TestCounter_Inc(t *testing.T) {
	c := &Counter{cnt: prometheus.NewCounter(prometheus.CounterOpts{
		Name: "test_counter_inc",
	})}

	var res dto.Metric
	for i := 1; i <= 10; i++ {
		c.Inc()
		err := c.cnt.Write(&res)
		assert.NoError(t, err)
		assert.Equal(t, int64(i), int64(res.GetCounter().GetValue()))
	}
}