Skip to content

Commit c0b3906

Browse files
authored
Merge pull request #15 from yunginnanet/closed-rate-fix
Fix: Rate method returns zero after sp is closed + add test
2 parents a8483bb + 88bb2b9 commit c0b3906

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Diff for: speedometer/speedometer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (s *Speedometer) Close() error {
209209
// Rate returns the bytes per second rate at which data is being written to the underlying writer.
210210
func (s *Speedometer) Rate() float64 {
211211
if s.internal.closed.Load() {
212-
return float64(s.Total()) / s.internal.duration.Load().Seconds()
212+
return 0
213213
}
214214
return float64(s.Total()) / time.Since(*s.internal.birth.Load()).Seconds()
215215
}

Diff for: speedometer/speedometer_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ func Test_Speedometer(t *testing.T) {
152152
if closeErr := sp.Close(); closeErr != nil {
153153
t.Errorf("wantErr: want %v, have %v", nil, closeErr)
154154
}
155+
t.Run("ZeroRateAfterClose", func(t *testing.T) {
156+
if sp.Rate() != 0 {
157+
t.Errorf("rate: want %f after 'Close' method, have %f", 0.0, sp.Rate())
158+
}
159+
})
155160
err = <-errChan
156161
if !errors.Is(err, io.ErrClosedPipe) {
157162
t.Errorf("wantErr: want %v, have %v", io.ErrClosedPipe, err)

0 commit comments

Comments
 (0)