Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions flowrate/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package flowrate

import (
"bytes"
"reflect"
"testing"
"time"
)

const statusTimeTolerance = 25 * time.Millisecond

const (
_50ms = 50 * time.Millisecond
_100ms = 100 * time.Millisecond
Expand All @@ -31,6 +32,37 @@ func nextStatus(m *Monitor) Status {
return m.Status()
}

func statusEqual(got, want Status) bool {
if got.Start != want.Start || got.Active != want.Active ||
got.Bytes != want.Bytes || got.Samples != want.Samples ||
got.BytesRem != want.BytesRem || got.Progress != want.Progress {
return false
}
if absDuration(got.Duration-want.Duration) > statusTimeTolerance ||
absDuration(got.Idle-want.Idle) > statusTimeTolerance ||
absDuration(got.TimeRem-want.TimeRem) > statusTimeTolerance {
return false
}
return absInt(got.InstRate-want.InstRate) <= 10 &&
absInt(got.CurRate-want.CurRate) <= 15 &&
absInt(got.AvgRate-want.AvgRate) <= 15 &&
absInt(got.PeakRate-want.PeakRate) <= 10
}

func absDuration(d time.Duration) time.Duration {
if d < 0 {
return -d
}
return d
}

func absInt(v int64) int64 {
if v < 0 {
return -v
}
return v
}

func TestReader(t *testing.T) {
in := make([]byte, 100)
for i := range in {
Expand Down Expand Up @@ -90,7 +122,7 @@ func TestReader(t *testing.T) {
Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
}
for i, s := range status {
if !reflect.DeepEqual(&s, &want[i]) {
if !statusEqual(s, want[i]) {
t.Errorf("r.Status(%v) expected %v; got %v", i, want[i], s)
}
}
Expand Down Expand Up @@ -136,7 +168,7 @@ func TestWriter(t *testing.T) {
Status{true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},
}
for i, s := range status {
if !reflect.DeepEqual(&s, &want[i]) {
if !statusEqual(s, want[i]) {
t.Errorf("w.Status(%v) expected %v; got %v", i, want[i], s)
}
}
Expand Down