git/githistory/log: panic() when *PercentageTask is over-counted

This commit is contained in:
Taylor Blau 2017-07-31 10:13:04 -06:00
parent 78195ec383
commit b51003e648
2 changed files with 12 additions and 1 deletions

@ -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 {

@ -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)
}