fix: resume progress bar after timeout

main
sundowndev 2021-04-19 18:10:22 +02:00
parent 3a470bbb25
commit b38063da8d
2 changed files with 14 additions and 7 deletions

View File

@ -53,9 +53,12 @@ func (p *progress) Stop() {
}
func (p *progress) Inc() {
if p.started.Load() {
p.count.Inc()
if lastVal := p.count.Load(); !p.started.Load() {
logrus.Debug("Progress received a tic after stopping. Restarting...")
p.Start()
p.count.Store(lastVal)
}
p.count.Inc()
}
func (p *progress) Val() uint64 {

View File

@ -10,9 +10,14 @@ import (
func TestProgressTimeoutDoesNotInc(t *testing.T) {
progress := NewProgress()
progress.Start()
progress.Inc()
progress.Stop() // should not hang
progress.Inc() // should not hang or inc
assert.Equal(t, uint64(0), progress.Val())
progress.Inc() // should restart progress and inc
assert.Equal(t, uint64(2), progress.Val())
assert.Equal(t, true, progress.started.Load())
progress.Stop()
assert.Equal(t, false, progress.started.Load())
}
func TestProgressTimeoutDoesNotHang(t *testing.T) {
@ -21,10 +26,9 @@ func TestProgressTimeoutDoesNotHang(t *testing.T) {
time.Sleep(progressTimeout)
for progress.started.Load() == true {
}
progress.Inc() // should not hang or inc
progress.Inc() // should not hang but inc
progress.Stop() // should not hang
assert.Equal(t, uint64(0), progress.Val())
assert.Equal(t, uint64(1), progress.Val())
}
func TestProgress(t *testing.T) {