2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-02-12 05:36:06 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2012-12-19 14:48:12 +04:00
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
2012-12-19 14:48:12 +04:00
|
|
|
|
|
|
|
import (
|
2013-01-19 17:48:30 +04:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
2012-12-19 14:48:12 +04:00
|
|
|
)
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
func testCounter(t tester) {
|
2013-01-19 17:48:30 +04:00
|
|
|
type input struct {
|
|
|
|
steps []func(g Counter)
|
|
|
|
}
|
|
|
|
type output struct {
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
var scenarios = []struct {
|
|
|
|
in input
|
|
|
|
out output
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(nil, 1)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{},"value":1}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{}, 2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{},"value":2}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{}, 3)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{}, 5)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{},"value":5}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 13)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/bar"}, 17)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.ResetAll()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 19)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":19}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 23)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.Increment(map[string]string{"handler": "/foo"})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":24}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Increment(map[string]string{"handler": "/foo"})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":1}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Decrement(map[string]string{"handler": "/foo"})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":-1}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 29)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.Decrement(map[string]string{"handler": "/foo"})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":28}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 31)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.IncrementBy(map[string]string{"handler": "/foo"}, 5)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":36}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
steps: []func(g Counter){
|
|
|
|
func(g Counter) {
|
|
|
|
g.Set(map[string]string{"handler": "/foo"}, 37)
|
|
|
|
},
|
|
|
|
func(g Counter) {
|
|
|
|
g.DecrementBy(map[string]string{"handler": "/foo"}, 10)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: output{
|
2013-04-19 15:29:24 +04:00
|
|
|
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":27}]}`,
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
counter := NewCounter()
|
|
|
|
|
|
|
|
for _, step := range scenario.in.steps {
|
|
|
|
step(counter)
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:11:01 +04:00
|
|
|
bytes, err := json.Marshal(counter)
|
2013-01-19 17:48:30 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d. could not marshal into JSON %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
asString := string(bytes)
|
|
|
|
|
|
|
|
if scenario.out.value != asString {
|
|
|
|
t.Errorf("%d. expected %q, got %q", i, scenario.out.value, asString)
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 14:48:12 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
func TestCounter(t *testing.T) {
|
|
|
|
testCounter(t)
|
2012-12-19 14:48:12 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
func BenchmarkCounter(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testCounter(b)
|
|
|
|
}
|
2012-12-19 14:48:12 +04:00
|
|
|
}
|