bps/bandwidth_test.go

79 lines
1.6 KiB
Go
Raw Normal View History

package bps
2014-03-02 22:44:09 -08:00
import (
"bytes"
"io"
2014-03-02 22:44:09 -08:00
"log"
"testing"
"time"
)
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
2015-09-30 22:57:14 -07:00
func TestSimple(t *testing.T) {
bw, err := New(1*time.Second, 100)
2014-03-02 22:44:09 -08:00
if err != nil {
2015-09-30 22:57:14 -07:00
t.Fatalf("failure to make reasonable BPS: %v", err)
2014-03-02 22:44:09 -08:00
}
bw.Close()
2014-03-02 22:44:09 -08:00
}
2015-09-30 22:57:14 -07:00
func TestBadResolution(t *testing.T) {
_, err := New(1*time.Hour, 1)
2014-03-02 22:44:09 -08:00
if err == nil {
2015-09-30 22:57:14 -07:00
t.Fatal("expected an error, got nil")
2014-03-02 22:44:09 -08:00
}
}
2015-09-30 22:57:14 -07:00
func TestWriter(t *testing.T) {
t.Parallel()
bw, err := New(1*time.Second, 100)
if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err)
}
2015-09-30 22:57:14 -07:00
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())
}
}
2015-09-30 22:57:14 -07:00
time.Sleep(2 * time.Second)
for i := 0; i < 100; i++ {
if bw.Rate() > 0 {
t.Errorf("got high rate: got, want 0.0000", bw.Rate())
}
time.Sleep(9 * time.Millisecond)
}
bw.Close()
}
2015-09-30 22:57:14 -07:00
func TestAdd(t *testing.T) {
t.Parallel()
bw, err := New(1*time.Second, 100)
if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err)
}
2015-09-30 22:57:14 -07:00
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())
}
}
2015-09-30 22:57:14 -07:00
time.Sleep(2 * time.Second)
for i := 0; i < 100; i++ {
if bw.Rate() > 0 {
t.Errorf("got high rate: got, want 0.0000", bw.Rate())
}
time.Sleep(9 * time.Millisecond)
}
bw.Close()
}