2018-05-08 09:38:12 +00:00
|
|
|
package metrics
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-05-07 06:50:03 +00:00
|
|
|
"time"
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
)
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
func makeMetric(value string) *api.Metric {
|
|
|
|
metr := &api.Metric{
|
|
|
|
Name: "test",
|
|
|
|
Peer: "peer1",
|
|
|
|
Value: value,
|
|
|
|
Valid: true,
|
|
|
|
}
|
|
|
|
metr.SetTTL(5 * time.Second)
|
|
|
|
return metr
|
|
|
|
}
|
|
|
|
|
2018-05-01 13:39:41 +00:00
|
|
|
func TestMetricsWindow(t *testing.T) {
|
2018-05-09 07:04:49 +00:00
|
|
|
mw := NewWindow(4)
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
_, err := mw.Latest()
|
|
|
|
if err != ErrNoMetrics {
|
|
|
|
t.Error("expected ErrNoMetrics")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mw.All()) != 0 {
|
|
|
|
t.Error("expected 0 metrics")
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
mw.Add(makeMetric("1"))
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
metr2, err := mw.Latest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if metr2.Value != "1" {
|
|
|
|
t.Error("expected different value")
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
mw.Add(makeMetric("2"))
|
|
|
|
mw.Add(makeMetric("3"))
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
all := mw.All()
|
|
|
|
if len(all) != 3 {
|
|
|
|
t.Fatal("should only be storing 3 metrics")
|
|
|
|
}
|
|
|
|
|
|
|
|
if all[0].Value != "3" {
|
|
|
|
t.Error("newest metric should be first")
|
|
|
|
}
|
|
|
|
|
|
|
|
if all[1].Value != "2" {
|
|
|
|
t.Error("older metric should be second")
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
mw.Add(makeMetric("4"))
|
|
|
|
mw.Add(makeMetric("5"))
|
2018-05-01 13:39:41 +00:00
|
|
|
|
|
|
|
all = mw.All()
|
|
|
|
if len(all) != 4 {
|
|
|
|
t.Fatal("should only be storing 4 metrics")
|
|
|
|
}
|
|
|
|
|
|
|
|
if all[len(all)-1].Value != "2" {
|
|
|
|
t.Error("oldest metric should be 2")
|
|
|
|
}
|
|
|
|
}
|