2018-04-28 02:04:56 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Vitess Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-04-28 19:17:45 +03:00
|
|
|
func TestCounterDuration(t *testing.T) {
|
2018-04-28 02:04:56 +03:00
|
|
|
var gotname string
|
2018-04-28 19:17:45 +03:00
|
|
|
var gotv *CounterDuration
|
2018-04-28 02:04:56 +03:00
|
|
|
clear()
|
|
|
|
Register(func(name string, v expvar.Var) {
|
|
|
|
gotname = name
|
2018-04-28 19:17:45 +03:00
|
|
|
gotv = v.(*CounterDuration)
|
2018-04-28 02:04:56 +03:00
|
|
|
})
|
2018-04-28 19:17:45 +03:00
|
|
|
v := NewCounterDuration("CounterDuration", "help")
|
|
|
|
if gotname != "CounterDuration" {
|
|
|
|
t.Errorf("want CounterDuration, got %s", gotname)
|
|
|
|
}
|
|
|
|
if gotv != v {
|
|
|
|
t.Errorf("want %#v, got %#v", v, gotv)
|
|
|
|
}
|
|
|
|
if v.Get() != 0 {
|
|
|
|
t.Errorf("want 0, got %v", v.Get())
|
|
|
|
}
|
|
|
|
v.Add(time.Duration(1))
|
|
|
|
if v.Get() != 1 {
|
|
|
|
t.Errorf("want 1, got %v", v.Get())
|
|
|
|
}
|
|
|
|
if v.String() != "1" {
|
|
|
|
t.Errorf("want 1, got %v", v.Get())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCounterDurationFunc(t *testing.T) {
|
|
|
|
var gotname string
|
|
|
|
var gotv *CounterDurationFunc
|
|
|
|
clear()
|
|
|
|
Register(func(name string, v expvar.Var) {
|
|
|
|
gotname = name
|
|
|
|
gotv = v.(*CounterDurationFunc)
|
|
|
|
})
|
|
|
|
|
|
|
|
v := NewCounterDurationFunc("CounterDurationFunc", "help", func() time.Duration {
|
|
|
|
return time.Duration(1)
|
|
|
|
})
|
|
|
|
if gotname != "CounterDurationFunc" {
|
|
|
|
t.Errorf("want CounterDurationFunc, got %s", gotname)
|
|
|
|
}
|
|
|
|
if gotv != v {
|
|
|
|
t.Errorf("want %#v, got %#v", v, gotv)
|
|
|
|
}
|
|
|
|
if v.String() != "1" {
|
|
|
|
t.Errorf("want 1, got %v", v.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeDuration(t *testing.T) {
|
|
|
|
var gotname string
|
|
|
|
var gotv *GaugeDuration
|
|
|
|
clear()
|
|
|
|
Register(func(name string, v expvar.Var) {
|
|
|
|
gotname = name
|
|
|
|
gotv = v.(*GaugeDuration)
|
|
|
|
})
|
|
|
|
v := NewGaugeDuration("GaugeDuration", "help")
|
|
|
|
if gotname != "GaugeDuration" {
|
|
|
|
t.Errorf("want GaugeDuration, got %s", gotname)
|
2018-04-28 02:04:56 +03:00
|
|
|
}
|
|
|
|
if gotv != v {
|
|
|
|
t.Errorf("want %#v, got %#v", v, gotv)
|
|
|
|
}
|
|
|
|
v.Set(time.Duration(5))
|
|
|
|
if v.Get() != 5 {
|
|
|
|
t.Errorf("want 5, got %v", v.Get())
|
|
|
|
}
|
|
|
|
v.Add(time.Duration(1))
|
|
|
|
if v.Get() != 6 {
|
|
|
|
t.Errorf("want 6, got %v", v.Get())
|
|
|
|
}
|
|
|
|
if v.String() != "6" {
|
|
|
|
t.Errorf("want 6, got %v", v.Get())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:17:45 +03:00
|
|
|
func TestGaugeDurationFunc(t *testing.T) {
|
2018-04-28 02:04:56 +03:00
|
|
|
var gotname string
|
2018-04-28 19:17:45 +03:00
|
|
|
var gotv *GaugeDurationFunc
|
2018-04-28 02:04:56 +03:00
|
|
|
clear()
|
|
|
|
Register(func(name string, v expvar.Var) {
|
|
|
|
gotname = name
|
2018-04-28 19:17:45 +03:00
|
|
|
gotv = v.(*GaugeDurationFunc)
|
2018-04-28 02:04:56 +03:00
|
|
|
})
|
|
|
|
|
2018-04-28 19:17:45 +03:00
|
|
|
v := NewGaugeDurationFunc("GaugeDurationFunc", "help", func() time.Duration {
|
2018-04-28 02:04:56 +03:00
|
|
|
return time.Duration(1)
|
2018-04-28 03:37:33 +03:00
|
|
|
})
|
2018-04-28 02:04:56 +03:00
|
|
|
|
2018-04-28 19:17:45 +03:00
|
|
|
if gotname != "GaugeDurationFunc" {
|
|
|
|
t.Errorf("want GaugeDurationFunc, got %s", gotname)
|
|
|
|
}
|
2018-04-28 02:04:56 +03:00
|
|
|
if gotv != v {
|
|
|
|
t.Errorf("want %#v, got %#v", v, gotv)
|
|
|
|
}
|
|
|
|
if v.String() != "1" {
|
|
|
|
t.Errorf("want 1, got %v", v.String())
|
|
|
|
}
|
|
|
|
}
|