package stats import ( "testing" "time" ) func TestCalculateRate(t *testing.T) { ts1 := time.Now().Unix() time.Sleep(1 * time.Second) ts2 := time.Now().Unix() v1 := metric{value: 1, ts: ts1} v2 := metric{value: 0, ts: ts1} result := calculateRate(v1, v2) t.Log("v1.Value > v2.Value && v1.Timestamp == v2.Timestamp, result:", result) if result != 0 { t.Fatal("unexpected result") } v1 = metric{value: 1, ts: ts1} v2 = metric{value: 1, ts: ts1} result = calculateRate(v1, v2) t.Log("v1.Value == v2.Value && v1.Timestamp == v2.Timestamp, result:", result) if result != 0 { t.Fatal("unexpected result") } v1 = metric{value: 1, ts: ts1} v2 = metric{value: 2, ts: ts1} result = calculateRate(v1, v2) t.Log("v1.Value < v2.Value && v1.Timestamp == v2.Timestamp, result:", result) if result != 0 { t.Fatal("unexpected result") } v1 = metric{value: 1, ts: ts2} v2 = metric{value: 2, ts: ts1} result = calculateRate(v1, v2) t.Log("v1.Value < v2.Value && v1.Timestamp > v2.Timestamp, result:", result) if result != 0 { t.Fatal("unexpected result") } v1 = metric{value: 1, ts: ts1} v2 = metric{value: 2, ts: ts2} result = calculateRate(v1, v2) t.Log("v1.Value < v2.Value && v1.Timestamp < v2.Timestamp, result:", result) if result == 0 { t.Fatal("unexpected result") } v1 = metric{value: 1024, ts: ts1} v2 = metric{value: 2048, ts: ts2} result = calculateRate(v1, v2) t.Log("rate is", result) if result != 1024 { t.Fatal("unexpected result") } }