Skip to content

Commit

Permalink
Merge pull request #19 from pivotal/PR-fix-start-panic
Browse files Browse the repository at this point in the history
Allow Start to be called immediately after Stop
  • Loading branch information
asahasrabuddhe authored Jan 3, 2020
2 parents c14265c + c88f574 commit 11a6ee7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ func (w *Writer) Flush() error {
func (w *Writer) Start() {
if w.ticker == nil {
w.ticker = time.NewTicker(w.RefreshInterval)
w.tdone = make(chan bool, 1)
w.tdone = make(chan bool)
}

go w.Listen()
}

// Stop stops the listener that updates the terminal
func (w *Writer) Stop() {
_ = w.Flush()
close(w.tdone)
w.Flush()
w.tdone <- true
<-w.tdone
}

// Listen listens for updates to the writer's buffer and flushes to the out provided. It blocks the runtime.
Expand All @@ -132,6 +133,7 @@ func (w *Writer) Listen() {
w.ticker.Stop()
w.ticker = nil
w.mtx.Unlock()
close(w.tdone)
return
}
}
Expand Down
11 changes: 11 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ func TestWriter(t *testing.T) {
t.Fatalf("want %q, got %q", want, b.String())
}
}

func TestStartCalledTwice(t *testing.T) {
w := New()
b := &bytes.Buffer{}
w.Out = b

w.Start()
w.Stop()
w.Start()
w.Stop()
}

0 comments on commit 11a6ee7

Please sign in to comment.