/usr/share/gocode/src/github.com/paulbellamy/ratecounter/avgratecounter_test.go is in golang-github-paulbellamy-ratecounter-dev 0.2.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | package ratecounter
import (
"testing"
"time"
)
func TestAvgRateCounter(t *testing.T) {
interval := 500 * time.Millisecond
r := NewAvgRateCounter(interval)
check := func(expected float64) {
val := r.Rate()
if val != expected {
t.Error("Expected ", val, " to equal ", expected)
}
}
check(0)
r.Incr(1) // counter = 1, hits = 1
check(1.0)
r.Incr(3) // counter = 4, hits = 2
check(2.0)
time.Sleep(2 * interval)
check(0)
}
func TestAvgRateCounterAdvanced(t *testing.T) {
interval := 500 * time.Millisecond
almost := 450 * time.Millisecond
r := NewAvgRateCounter(interval)
check := func(expected float64) {
val := r.Rate()
if val != expected {
t.Error("Expected ", val, " to equal ", expected)
}
}
check(0)
r.Incr(1) // counter = 1, hits = 1
check(1.0)
time.Sleep(interval - almost)
r.Incr(3) // counter = 4, hits = 2
check(2.0)
time.Sleep(almost)
check(3.0) // counter = 3, hits = 1
time.Sleep(2 * interval)
check(0)
}
func TestAvgRateCounterNoResolution(t *testing.T) {
interval := 500 * time.Millisecond
almost := 450 * time.Millisecond
r := NewAvgRateCounter(interval).WithResolution(1)
check := func(expected float64) {
val := r.Rate()
if val != expected {
t.Error("Expected ", val, " to equal ", expected)
}
}
check(0)
r.Incr(1) // counter = 1, hits = 1
check(1.0)
time.Sleep(interval - almost)
r.Incr(3) // counter = 4, hits = 2
check(2.0)
time.Sleep(almost)
check(0) // counter = 0, hits = 0
time.Sleep(2 * interval)
check(0)
}
func TestAvgRateCounter_Incr_ReturnsImmediately(t *testing.T) {
interval := 1 * time.Second
r := NewRateCounter(interval)
start := time.Now()
r.Incr(-1)
duration := time.Since(start)
if duration >= 1*time.Second {
t.Error("incr took", duration, "to return")
}
}
func BenchmarkAvgRateCounter(b *testing.B) {
interval := 0 * time.Millisecond
r := NewAvgRateCounter(interval)
for i := 0; i < b.N; i++ {
r.Incr(1)
r.Rate()
}
}
|