-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from henvic/fix-race-condition-on-stop
Using time.Ticker for running stoppable listener. Fix edge case where flush happens after the listener is stopped.
- Loading branch information
Showing
2 changed files
with
80 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package uiprogress | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestStoppingPrintout(t *testing.T) { | ||
progress := New() | ||
progress.RefreshInterval = time.Millisecond * 10 | ||
var buffer = &bytes.Buffer{} | ||
progress.Out = buffer | ||
bar := progress.AddBar(100) | ||
progress.Start() | ||
|
||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
|
||
go func() { | ||
for i := 0; i <= 80; i = i + 10 { | ||
bar.Set(i) | ||
time.Sleep(time.Millisecond * 5) | ||
} | ||
|
||
wg.Done() | ||
}() | ||
|
||
wg.Wait() | ||
|
||
progress.Stop() | ||
fmt.Fprintf(buffer, "foo") | ||
|
||
var wantSuffix = "[======================================================>-------------]\nfoo" | ||
|
||
if !strings.HasSuffix(buffer.String(), wantSuffix) { | ||
t.Errorf("Content that should be printed after stop not appearing on buffer.") | ||
} | ||
} |