diff --git a/speedometer/speedometer.go b/speedometer/speedometer.go index 837bf70..eff4216 100644 --- a/speedometer/speedometer.go +++ b/speedometer/speedometer.go @@ -209,7 +209,7 @@ func (s *Speedometer) Close() error { // Rate returns the bytes per second rate at which data is being written to the underlying writer. func (s *Speedometer) Rate() float64 { if s.internal.closed.Load() { - return float64(s.Total()) / s.internal.duration.Load().Seconds() + return 0 } return float64(s.Total()) / time.Since(*s.internal.birth.Load()).Seconds() } diff --git a/speedometer/speedometer_test.go b/speedometer/speedometer_test.go index 5dfb022..ba02f1e 100644 --- a/speedometer/speedometer_test.go +++ b/speedometer/speedometer_test.go @@ -152,6 +152,11 @@ func Test_Speedometer(t *testing.T) { if closeErr := sp.Close(); closeErr != nil { t.Errorf("wantErr: want %v, have %v", nil, closeErr) } + t.Run("ZeroRateAfterClose", func(t *testing.T) { + if sp.Rate() != 0 { + t.Errorf("rate: want %f after 'Close' method, have %f", 0.0, sp.Rate()) + } + }) err = <-errChan if !errors.Is(err, io.ErrClosedPipe) { t.Errorf("wantErr: want %v, have %v", io.ErrClosedPipe, err)