client_golang/prometheus/gauge_test.go

155 lines
2.8 KiB
Go
Raw Normal View History

// Copyright (c) 2013, Prometheus Team
// All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2012-05-20 01:59:25 +04:00
package prometheus
2012-05-20 01:59:25 +04:00
import (
"encoding/json"
"testing"
"github.com/prometheus/client_golang/test"
2012-05-20 01:59:25 +04:00
)
func testGauge(t test.Tester) {
type input struct {
steps []func(g Gauge)
}
type output struct {
value string
}
2012-05-20 01:59:25 +04:00
var scenarios = []struct {
in input
out output
}{
{
in: input{
steps: []func(g Gauge){},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(nil, 1)
},
},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[{"labels":{},"value":1}]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{}, 2)
},
},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[{"labels":{},"value":2}]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{}, 3)
},
func(g Gauge) {
g.Set(map[string]string{}, 5)
},
},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[{"labels":{},"value":5}]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{"handler": "/foo"}, 13)
},
func(g Gauge) {
g.Set(map[string]string{"handler": "/bar"}, 17)
},
func(g Gauge) {
g.Reset(map[string]string{"handler": "/bar"})
},
},
},
out: output{
value: `{"type":"gauge","value":[{"labels":{"handler":"/foo"},"value":13}]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{"handler": "/foo"}, 13)
},
func(g Gauge) {
g.Set(map[string]string{"handler": "/bar"}, 17)
},
func(g Gauge) {
g.ResetAll()
},
},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{"handler": "/foo"}, 19)
},
},
},
out: output{
2013-04-19 15:29:24 +04:00
value: `{"type":"gauge","value":[{"labels":{"handler":"/foo"},"value":19}]}`,
},
},
}
2012-05-20 01:59:25 +04:00
for i, scenario := range scenarios {
gauge := NewGauge()
2012-05-20 01:59:25 +04:00
for _, step := range scenario.in.steps {
step(gauge)
}
2012-05-20 01:59:25 +04:00
bytes, err := json.Marshal(gauge)
if err != nil {
t.Errorf("%d. could not marshal into JSON %s", i, err)
continue
}
2012-05-20 01:59:25 +04:00
asString := string(bytes)
2012-05-20 01:59:25 +04:00
if scenario.out.value != asString {
t.Errorf("%d. expected %q, got %q", i, scenario.out.value, asString)
}
}
2012-05-20 01:59:25 +04:00
}
func TestGauge(t *testing.T) {
testGauge(t)
}
2012-05-20 01:59:25 +04:00
func BenchmarkGauge(b *testing.B) {
for i := 0; i < b.N; i++ {
testGauge(b)
}
2012-05-20 01:59:25 +04:00
}