diff --git a/git/githistory/log/percentage_task.go b/git/githistory/log/percentage_task.go index 68de6e04..39d66479 100644 --- a/git/githistory/log/percentage_task.go +++ b/git/githistory/log/percentage_task.go @@ -38,7 +38,9 @@ func NewPercentageTask(msg string, total uint64) *PercentageTask { // Count returns the new total number of (atomically managed) elements that have // been completed. func (c *PercentageTask) Count(n uint64) (new uint64) { - new = atomic.AddUint64(&c.n, n) + if new = atomic.AddUint64(&c.n, n); new > c.total { + panic("git/githistory/log: counted too many items") + } var percentage float64 if c.total == 0 { diff --git a/git/githistory/log/percentage_task_test.go b/git/githistory/log/percentage_task_test.go index 3410d011..e61c000d 100644 --- a/git/githistory/log/percentage_task_test.go +++ b/git/githistory/log/percentage_task_test.go @@ -60,3 +60,12 @@ func TestPercentageTaskIsThrottled(t *testing.T) { assert.True(t, throttled, "git/githistory/log: expected *PercentageTask to be Throttle()-d") } + +func TestPercentageTaskPanicsWhenOvercounted(t *testing.T) { + task := NewPercentageTask("example", 0) + defer func() { + assert.Equal(t, "git/githistory/log: counted too many items", recover()) + }() + + task.Count(1) +}