Skip to content

Commit

Permalink
Fix humanizeDurationSeconds func (#75)
Browse files Browse the repository at this point in the history
If duration is more than 1000s, the function will panic.

Closes: #74
  • Loading branch information
adidenko authored Aug 7, 2024
1 parent d4a8641 commit eaece28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ func humanizeDurationSeconds(seconds float64) string {
for n := duration / unit; n >= unit; n /= unit {
div *= unit
exp++
if exp >= len(units)-1 {
break
}
}

return fmt.Sprintf("%.2f%s",
Expand Down
17 changes: 17 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func TestHeadersParsing(t *testing.T) {
}
}
}

func TestHumanizeDurationSeconds(t *testing.T) {
requestTime := humanizeDurationSeconds(6000)
if requestTime != "6000.00s" {
t.Errorf("Wrong requestTime: %q", requestTime)
}

requestTime = humanizeDurationSeconds(0.05)
if requestTime != "50.00ms" {
t.Errorf("Wrong requestTime: %q", requestTime)
}

requestTime = humanizeDurationSeconds(99999999)
if requestTime != "99999999.00s" {
t.Errorf("Wrong requestTime: %q", requestTime)
}
}

0 comments on commit eaece28

Please sign in to comment.