Skip to content

Commit

Permalink
fix: resume progress bar after timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Apr 19, 2021
1 parent 5ff2946 commit dd31f23
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions pkg/output/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions pkg/output/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit dd31f23

Please sign in to comment.