Skip to content

Commit

Permalink
Merge pull request #15 from yunginnanet/closed-rate-fix
Browse files Browse the repository at this point in the history
Fix: Rate method returns zero after sp is closed + add test
  • Loading branch information
yunginnanet authored Jul 3, 2024
2 parents a8483bb + 88bb2b9 commit c0b3906
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion speedometer/speedometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 5 additions & 0 deletions speedometer/speedometer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c0b3906

Please sign in to comment.