Skip to content

Commit

Permalink
fix divide-by-zero issue in progress tracker's ETA function (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Sep 13, 2018
1 parent a06b0e8 commit 996191e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions progress/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (t *Tracker) ETA() time.Duration {
return t.ExpectedDuration - timeTaken
}

pDone := t.PercentDone()
pDone := int64(t.PercentDone())
if pDone == 0 {
return time.Duration(0)
}
return time.Duration((int64(timeTaken) / int64(pDone)) * int64(100-pDone))
return time.Duration((int64(timeTaken) / pDone) * (100 - pDone))
}

// Increment updates the current value of the task being tracked.
Expand Down Expand Up @@ -66,6 +66,9 @@ func (t *Tracker) MarkAsDone() {

// PercentDone returns the currently completed percentage value.
func (t *Tracker) PercentDone() float64 {
if t.Total == 0 {
return 0
}
return float64(t.value) * 100.0 / float64(t.Total)
}

Expand Down
10 changes: 8 additions & 2 deletions progress/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ func TestTracker_ETA(t *testing.T) {
timeDelayUnit := time.Millisecond
timeDelay := timeDelayUnit * 25

tracker := Tracker{Total: 100}
tracker := Tracker{}
assert.Equal(t, time.Duration(0), tracker.ETA())

tracker.Total = 100
tracker.start()
assert.Equal(t, time.Duration(0), tracker.ETA())
time.Sleep(timeDelay)
Expand Down Expand Up @@ -65,7 +68,10 @@ func TestTracker_MarkAsDone(t *testing.T) {
}

func TestTracker_PercentDone(t *testing.T) {
tracker := Tracker{Total: 100}
tracker := Tracker{}
assert.Equal(t, 0.00, tracker.PercentDone())

tracker.Total = 100
assert.Equal(t, 0.00, tracker.PercentDone())

for idx := 1; idx <= 100; idx++ {
Expand Down

0 comments on commit 996191e

Please sign in to comment.