2015-09-29 23:29:49 -07:00
|
|
|
package bps
|
2014-03-02 22:44:09 -08:00
|
|
|
|
|
|
|
import (
|
2015-09-29 23:29:49 -07:00
|
|
|
"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
|
|
|
}
|
2015-09-29 23:29:49 -07: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
|
|
|
}
|
|
|
|
}
|
2014-03-08 00:45:55 -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)
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
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())
|
|
|
|
}
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
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)
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
2015-09-29 23:29:49 -07:00
|
|
|
bw.Close()
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
|
|
|
|
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)
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
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())
|
|
|
|
}
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
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)
|
2014-03-08 18:07:43 -08:00
|
|
|
}
|
2015-09-29 23:29:49 -07:00
|
|
|
bw.Close()
|
2014-03-08 00:45:55 -08:00
|
|
|
}
|
2015-09-30 23:07:41 -07:00
|
|
|
|
|
|
|
func TestHumanBytes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
bw, err := New(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)
|
|
|
|
}
|
|
|
|
}
|