You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/pkg/stat/metric/window_test.go

68 lines
1.5 KiB

package metric
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWindowResetWindow(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
for i := 0; i < opts.Size; i++ {
window.Append(i, 1.0)
}
window.ResetWindow()
for i := 0; i < opts.Size; i++ {
assert.Equal(t, len(window.Bucket(i).Points), 0)
}
}
func TestWindowResetBucket(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
for i := 0; i < opts.Size; i++ {
window.Append(i, 1.0)
}
window.ResetBucket(1)
assert.Equal(t, len(window.Bucket(1).Points), 0)
assert.Equal(t, window.Bucket(0).Points[0], float64(1.0))
assert.Equal(t, window.Bucket(2).Points[0], float64(1.0))
}
func TestWindowResetBuckets(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
for i := 0; i < opts.Size; i++ {
window.Append(i, 1.0)
}
window.ResetBuckets([]int{0, 1, 2})
for i := 0; i < opts.Size; i++ {
assert.Equal(t, len(window.Bucket(i).Points), 0)
}
}
func TestWindowAppend(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
for i := 0; i < opts.Size; i++ {
window.Append(i, 1.0)
}
for i := 0; i < opts.Size; i++ {
assert.Equal(t, window.Bucket(i).Points[0], float64(1.0))
}
}
func TestWindowAdd(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
window.Append(0, 1.0)
window.Add(0, 1.0)
assert.Equal(t, window.Bucket(0).Points[0], float64(2.0))
}
func TestWindowSize(t *testing.T) {
opts := WindowOpts{Size: 3}
window := NewWindow(opts)
assert.Equal(t, window.Size(), 3)
}