diff --git a/bar_test.go b/bar_test.go index 976cb8d..1b8a5d3 100644 --- a/bar_test.go +++ b/bar_test.go @@ -46,67 +46,48 @@ } } -func TestBarInProgress(t *testing.T) { - var buf bytes.Buffer - cancel := make(chan struct{}) +func TestBarCompleted(t *testing.T) { p := mpb.New( - mpb.Output(&buf), - mpb.WithCancel(cancel), + mpb.Output(ioutil.Discard), ) - bar := p.AddBar(100, mpb.BarTrim()) + total := 80 + bar := p.AddBar(int64(total)) - stopped := make(chan struct{}) + var count int + for !bar.Completed() { + time.Sleep(10 * time.Millisecond) + bar.Increment() + count++ + } - go func() { - defer close(stopped) - for bar.InProgress() { - time.Sleep(10 * time.Millisecond) - bar.Increment() - } - }() - - time.Sleep(250 * time.Millisecond) - close(cancel) p.Stop() - - select { - case <-stopped: - case <-time.After(300 * time.Millisecond): - t.Error("bar.InProgress returns true after cancel") + if count != total { + t.Errorf("got count: %d, expected %d\n", count, total) } } -func TestBarGetID(t *testing.T) { - var wg sync.WaitGroup - p := mpb.New( - mpb.Output(ioutil.Discard), - mpb.WithWaitGroup(&wg), - ) +func TestBarID(t *testing.T) { + p := mpb.New(mpb.Output(ioutil.Discard)) numBars := 3 - wg.Add(numBars) - bars := make([]*mpb.Bar, numBars) for i := 0; i < numBars; i++ { - bars[i] = p.AddBar(100, mpb.BarID(i)) - + bars[i] = p.AddBar(80, mpb.BarID(i)) go func(bar *mpb.Bar) { - defer wg.Done() - for i := 0; i < 100; i++ { + for i := 0; i < 80; i++ { time.Sleep(10 * time.Millisecond) bar.Increment() } }(bars[i]) } + p.Stop() for wantID, bar := range bars { gotID := bar.ID() if gotID != wantID { t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) } } - - p.Stop() } func TestBarIncrWithReFill(t *testing.T) { @@ -128,6 +109,7 @@ for i := 0; i < total; i++ { bar.Increment() + time.Sleep(10 * time.Millisecond) } p.Stop() diff --git a/example_test.go b/example_test.go index be3a9ce..97e4346 100644 --- a/example_test.go +++ b/example_test.go @@ -46,11 +46,11 @@ p.Stop() } -func ExampleBar_InProgress() { +func ExampleBar_Completed() { p := mpb.New() bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0))) - for bar.InProgress() { + for !bar.Completed() { time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) bar.Increment() } diff --git a/progress_test.go b/progress_test.go index d1b23ef..a3256e4 100644 --- a/progress_test.go +++ b/progress_test.go @@ -11,7 +11,6 @@ "time" "github.com/vbauerster/mpb" - "github.com/vbauerster/mpb/decor" ) func init() { @@ -91,27 +90,14 @@ current int64 } - resStream := make(chan *sample, numBars) + bars := make([]*mpb.Bar, 0, numBars) for i := 0; i < numBars; i++ { - bar := p.AddBar(int64(200), - mpb.BarID(i), - mpb.PrependDecorators( - func(s *decor.Statistics, _ chan<- int, _ <-chan int) string { - if s.Completed { - resStream <- &sample{ - id: s.ID, - total: s.Total, - current: s.Current, - } - } - return "" - }, - )) - + bar := p.AddBar(int64(1000), mpb.BarID(i)) + bars = append(bars, bar) go func() { - for bar.InProgress() { + for !bar.Completed() { + time.Sleep(randomDuration(40 * time.Millisecond)) bar.Increment() - time.Sleep(randomDuration(80 * time.Millisecond)) } }() } @@ -121,10 +107,9 @@ }) p.Stop() - close(resStream) - for res := range resStream { - if res.current >= res.total { - t.Errorf("bar %d: total = %d, current = %d\n", res.id, res.total, res.current) + for _, bar := range bars { + if bar.Current() >= bar.Total() { + t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current()) } } }