diff --git a/progress_go1.7_test.go b/progress_go1.7_test.go index ba44563..e903b1d 100644 --- a/progress_go1.7_test.go +++ b/progress_go1.7_test.go @@ -5,6 +5,7 @@ import ( "context" "fmt" + "io/ioutil" "math/rand" "sync" "testing" @@ -17,7 +18,11 @@ func TestWithContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) shutdown := make(chan struct{}) - p := mpb.New(mpb.WithContext(ctx), mpb.WithShutdownNotifier(shutdown)) + p := mpb.New( + mpb.Output(ioutil.Discard), + mpb.WithContext(ctx), + mpb.WithShutdownNotifier(shutdown), + ) var wg sync.WaitGroup total := 100 @@ -27,7 +32,7 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), mpb.BarID(i), - mpb.PrependDecorators(decor.Name(name, len(name), 0))) + mpb.PrependDecorators(decor.StaticName(name, len(name), 0))) go func() { defer wg.Done() @@ -37,8 +42,8 @@ return default: } - time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) - bar.Incr(1) + time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + bar.Increment() } }() } diff --git a/progress_test.go b/progress_test.go index 70eccb4..b81bbd2 100644 --- a/progress_test.go +++ b/progress_test.go @@ -3,6 +3,7 @@ import ( "bytes" "fmt" + "io/ioutil" "math/rand" "sync" "testing" @@ -67,7 +68,7 @@ func TestAddBar(t *testing.T) { var wg sync.WaitGroup var buf bytes.Buffer - p := mpb.New(mpb.Output(&buf)) + p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg)) count := p.BarCount() if count != 0 { @@ -79,7 +80,7 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(100, mpb.PrependDecorators(decor.Name(name, len(name), 0))) + bar := p.AddBar(100, mpb.PrependDecorators(decor.StaticName(name, len(name), 0))) go func() { defer wg.Done() @@ -93,7 +94,6 @@ if count != numBars { t.Errorf("BarCount want: %q, got: %q\n", numBars, count) } - wg.Wait() p.Stop() } @@ -114,11 +114,16 @@ } func TestWithCancel(t *testing.T) { + var wg sync.WaitGroup cancel := make(chan struct{}) shutdown := make(chan struct{}) - p := mpb.New(mpb.WithCancel(cancel), mpb.WithShutdownNotifier(shutdown)) + p := mpb.New( + mpb.Output(ioutil.Discard), + mpb.WithCancel(cancel), + mpb.WithShutdownNotifier(shutdown), + mpb.WithWaitGroup(&wg), + ) - var wg sync.WaitGroup total := 100 numBars := 3 wg.Add(numBars) @@ -126,21 +131,18 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), mpb.BarID(i), - mpb.PrependDecorators(decor.Name(name, len(name), 0))) + mpb.PrependDecorators(decor.StaticName(name, len(name), 0))) go func() { - defer func() { - // fmt.Printf("%s done\n", name) - wg.Done() - }() + defer wg.Done() for i := 0; i < total; i++ { select { case <-cancel: return default: } - time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) - bar.Incr(1) + time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + bar.Increment() } }() } @@ -149,7 +151,6 @@ close(cancel) }) - wg.Wait() p.Stop() select {