diff --git a/progress_go1.7_test.go b/progress_go1.7_test.go index 7ba7933..e709675 100644 --- a/progress_go1.7_test.go +++ b/progress_go1.7_test.go @@ -6,6 +6,7 @@ "context" "fmt" "math/rand" + "sync" "testing" "time" @@ -16,21 +17,37 @@ ctx, cancel := context.WithCancel(context.Background()) shutdown := make(chan struct{}) p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown) + + var wg sync.WaitGroup + total := 100 numBars := 3 + wg.Add(numBars) for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(100).PrependName(name, len(name), 0) + bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0) go func() { - for i := 0; i < 10000; i++ { + defer func() { + // fmt.Printf("%s done\n", name) + wg.Done() + }() + for i := 0; i < total; i++ { + select { + case <-ctx.Done(): + return + default: + } + time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) bar.Incr(1) - time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) } }() } - cancel() + time.AfterFunc(300*time.Millisecond, cancel) + + wg.Wait() + p.Stop() select { case <-shutdown: diff --git a/progress_test.go b/progress_test.go index 5e7a729..6dacba6 100644 --- a/progress_test.go +++ b/progress_test.go @@ -102,21 +102,39 @@ cancel := make(chan struct{}) shutdown := make(chan struct{}) p := mpb.New().WithCancel(cancel).ShutdownNotify(shutdown) + + var wg sync.WaitGroup + total := 100 numBars := 3 + wg.Add(numBars) for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(100).PrependName(name, len(name), 0) + bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0) go func() { - for i := 0; i < 10000; i++ { + defer func() { + // fmt.Printf("%s done\n", name) + 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(100)) * time.Millisecond) } }() } - close(cancel) + time.AfterFunc(300*time.Millisecond, func() { + close(cancel) + }) + + wg.Wait() + p.Stop() select { case <-shutdown: @@ -124,3 +142,57 @@ t.Error("ProgressBar didn't stop") } } + +func TestWithNilCancel(t *testing.T) { + defer func() { + if p := recover(); p != nil { + if msg, ok := p.(string); ok && msg == "nil cancel channel" { + return + } + t.Errorf("Expected nil channel panic, got: %+v", p) + } + }() + _ = mpb.New().WithCancel(nil) +} + +func TestFormat(t *testing.T) { + var buf bytes.Buffer + cancel := make(chan struct{}) + shutdown := make(chan struct{}) + customFormat := "╢▌▌░╟" + p := mpb.New().Format(customFormat) + p.WithCancel(cancel) + p.ShutdownNotify(shutdown) + p.SetOut(&buf) + bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() + + go func() { + for i := 0; i < 100; i++ { + bar.Incr(1) + time.Sleep(10 * time.Millisecond) + if i == 42 { + close(cancel) + } + } + }() + + // p.Stop() + // time.Sleep(300 * time.Millisecond) + + // gotBar := strings.TrimSpace(buf.String()) + gotBar := buf.String() + seen := make(map[rune]bool) + for _, r := range gotBar { + if !seen[r] { + seen[r] = true + } + } + fmt.Println(gotBar) + for r, _ := range seen { + fmt.Printf("%#U\n", r) + } + // expectBar := "╢▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌╟" + // if gotBar != expectBar { + // t.Errorf("Expected for") + // } +}