199 lines
3.7 KiB
Go
199 lines
3.7 KiB
Go
package bps
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
}
|
|
|
|
func TestSimple(t *testing.T) {
|
|
bw, err := New(1 * time.Second)
|
|
if err != nil {
|
|
t.Fatalf("failure to make reasonable BPS: %v", err)
|
|
}
|
|
bw.Close()
|
|
}
|
|
|
|
func TestBadResolution(t *testing.T) {
|
|
var i uint
|
|
for i = 0; i < minResolution; i++ {
|
|
_, err := NewPrecise(1*time.Second, i)
|
|
if err == nil {
|
|
t.Fatalf("expected an error, got nil for %d", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWriter(t *testing.T) {
|
|
t.Parallel()
|
|
bw, err := New(1 * time.Second)
|
|
if err != nil {
|
|
t.Fatalf("failure to make reasonable BPS: %v", err)
|
|
}
|
|
b := &bytes.Buffer{}
|
|
b.Write([]byte("helloooooooooooooooooooooooooooooooooooooooooooooo"))
|
|
for i := 0; i < 90; i++ {
|
|
io.Copy(bw, b)
|
|
last := bw.Rate()
|
|
time.Sleep(10 * time.Millisecond)
|
|
if last > bw.Rate() {
|
|
t.Errorf("rate should be increasing, it isn't: last: %f > current: %f", last, bw.Rate())
|
|
}
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
for i := 0; i < 100; i++ {
|
|
if bw.Rate() > 0 {
|
|
t.Errorf("got high rate: got %f, want 0.0000", bw.Rate())
|
|
}
|
|
time.Sleep(9 * time.Millisecond)
|
|
}
|
|
bw.Close()
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
t.Parallel()
|
|
bw, err := New(1 * time.Second)
|
|
if err != nil {
|
|
t.Fatalf("failure to make reasonable BPS: %v", err)
|
|
}
|
|
for i := 0; i < 90; i++ {
|
|
bw.Add(1024)
|
|
last := bw.Rate()
|
|
time.Sleep(10 * time.Millisecond)
|
|
if last > bw.Rate() {
|
|
t.Errorf("rate should be increasing, it isn't: last: %f > current: %f", last, bw.Rate())
|
|
}
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
for i := 0; i < 100; i++ {
|
|
if bw.Rate() > 0 {
|
|
t.Errorf("got high rate: got %v, want 0.0000", bw.Rate())
|
|
}
|
|
time.Sleep(9 * time.Millisecond)
|
|
}
|
|
bw.Close()
|
|
}
|
|
|
|
func TestHumanBytes(t *testing.T) {
|
|
t.Parallel()
|
|
bw, err := NewPrecise(100*time.Second, 10)
|
|
if err != nil {
|
|
t.Fatalf("failure to make reasonable BPS: %v", err)
|
|
}
|
|
bw.Add(1000)
|
|
got := bw.HumanRate()
|
|
want := "10B/s"
|
|
if got != want {
|
|
t.Fatalf("did not get right human rate; got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestData(t *testing.T) {
|
|
tests := []struct {
|
|
i int
|
|
buckets []int64
|
|
expected []int
|
|
}{
|
|
{
|
|
i: 0,
|
|
buckets: []int64{},
|
|
expected: []int{},
|
|
},
|
|
{
|
|
i: 0,
|
|
buckets: []int64{0},
|
|
expected: []int{0},
|
|
},
|
|
{
|
|
i: 0,
|
|
buckets: []int64{0, 1},
|
|
expected: []int{1, 0},
|
|
},
|
|
{
|
|
i: 0,
|
|
buckets: []int64{0, 1, 2},
|
|
expected: []int{2, 1, 0},
|
|
},
|
|
{
|
|
i: 0,
|
|
buckets: []int64{0, 1, 2, 3},
|
|
expected: []int{3, 2, 1, 0},
|
|
},
|
|
|
|
{
|
|
i: 1,
|
|
buckets: []int64{},
|
|
expected: []int{},
|
|
},
|
|
{
|
|
i: 1,
|
|
buckets: []int64{0},
|
|
expected: []int{0},
|
|
},
|
|
{
|
|
i: 1,
|
|
buckets: []int64{0, 1},
|
|
expected: []int{0, 1},
|
|
},
|
|
{
|
|
i: 1,
|
|
buckets: []int64{0, 1, 2},
|
|
expected: []int{0, 2, 1},
|
|
},
|
|
{
|
|
i: 1,
|
|
buckets: []int64{0, 1, 2, 3},
|
|
expected: []int{0, 3, 2, 1},
|
|
},
|
|
|
|
{
|
|
i: 2,
|
|
buckets: []int64{0, 1, 2},
|
|
expected: []int{1, 0, 2},
|
|
},
|
|
{
|
|
i: 2,
|
|
buckets: []int64{0, 1, 2, 3},
|
|
expected: []int{1, 0, 3, 2},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
b := BPS{
|
|
timeI: test.i,
|
|
buckets: test.buckets,
|
|
}
|
|
got := b.Data()
|
|
if !equals(got, test.expected) {
|
|
t.Errorf("got wrong data:\n\tgot: %v\n\twant: %v", got, test.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func equals(a, b []int) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestOddBytes(t *testing.T) {
|
|
if got, want := human(1), "1B"; got != want {
|
|
t.Errorf("got: %s, want %s", got, want)
|
|
}
|
|
if got, want := human(1290), "1.3kB"; got != want {
|
|
t.Errorf("got: %s, want %s", got, want)
|
|
}
|
|
}
|