Codebase list golang-github-vbauerster-mpb / b2888b5
test p.WithContext and p.WithCancel Vladimir Bauer 9 years ago
1 changed file(s) with 120 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
0 package mpb
0 package mpb_test
11
22 import (
33 "bytes"
4 "context"
5 "fmt"
6 "math/rand"
7 "strings"
8 "sync"
49 "testing"
10 "time"
11 "unicode/utf8"
12
13 "github.com/vbauerster/mpb"
514 )
615
16 func TestDefaultWidth(t *testing.T) {
17 var buf bytes.Buffer
18 p := mpb.New().SetOut(&buf)
19 bar := p.AddBar(100)
20 for i := 0; i < 100; i++ {
21 bar.Incr(1)
22 }
23 p.Stop()
24 runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String()))
25 defWidth := 80
26 if runeCount != defWidth {
27 t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount)
28 }
29 }
30
31 func TestCustomWidth(t *testing.T) {
32 customWidth := 60
33 var buf bytes.Buffer
34 p := mpb.New().SetWidth(customWidth).SetOut(&buf)
35 bar := p.AddBar(100)
36 for i := 0; i < 100; i++ {
37 bar.Incr(1)
38 }
39 p.Stop()
40 runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String()))
41 if runeCount != customWidth {
42 t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount)
43 }
44 }
45
746 func TestAddBar(t *testing.T) {
47 var wg sync.WaitGroup
848 var buf bytes.Buffer
9 p := New().SetWidth(60).SetOut(&buf)
49 p := mpb.New().SetOut(&buf)
50
1051 count := p.BarCount()
1152 if count != 0 {
12 t.Errorf("Count want: %q, got: %q\n", 0, count)
53 t.Errorf("BarCount want: %q, got: %q\n", 0, count)
1354 }
14 bar := p.AddBar(10)
55
56 numBars := 3
57 wg.Add(numBars)
58
59 for i := 0; i < numBars; i++ {
60 name := fmt.Sprintf("Bar#%d:", i)
61 bar := p.AddBar(100).PrependName(name, len(name), 0)
62
63 go func() {
64 defer wg.Done()
65 for i := 0; i < 100; i++ {
66 bar.Incr(1)
67 }
68 }()
69 }
70
1571 count = p.BarCount()
16 if count != 1 {
17 t.Errorf("Count want: %q, got: %q\n", 0, count)
72 if count != numBars {
73 t.Errorf("BarCount want: %q, got: %q\n", numBars, count)
1874 }
19 for i := 0; i < 10; i++ {
20 bar.Incr(1)
21 }
75 wg.Wait()
2276 p.Stop()
2377 }
2478
2579 func TestRemoveBar(t *testing.T) {
26 p := New()
80 p := mpb.New()
81
2782 b := p.AddBar(10)
2883
2984 if !p.RemoveBar(b) {
3287
3388 count := p.BarCount()
3489 if count != 0 {
35 t.Errorf("Count want: %q, got: %q\n", 0, count)
90 t.Errorf("BarCount want: %q, got: %q\n", 0, count)
3691 }
3792 p.Stop()
3893 }
94
95 func TestWithContext(t *testing.T) {
96 ctx, cancel := context.WithCancel(context.Background())
97 shutdown := make(chan struct{})
98 p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown)
99 numBars := 3
100
101 for i := 0; i < numBars; i++ {
102 name := fmt.Sprintf("Bar#%d:", i)
103 bar := p.AddBar(100).PrependName(name, len(name), 0)
104
105 go func() {
106 for i := 0; i < 10000; i++ {
107 bar.Incr(1)
108 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
109 }
110 }()
111 }
112
113 cancel()
114
115 select {
116 case <-shutdown:
117 case <-time.After(500 * time.Millisecond):
118 t.Error("ProgressBar didn't stop")
119 }
120 }
121
122 func TestWithCancel(t *testing.T) {
123 cancel := make(chan struct{})
124 shutdown := make(chan struct{})
125 p := mpb.New().WithCancel(cancel).ShutdownNotify(shutdown)
126 numBars := 3
127
128 for i := 0; i < numBars; i++ {
129 name := fmt.Sprintf("Bar#%d:", i)
130 bar := p.AddBar(100).PrependName(name, len(name), 0)
131
132 go func() {
133 for i := 0; i < 10000; i++ {
134 bar.Incr(1)
135 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
136 }
137 }()
138 }
139
140 close(cancel)
141
142 select {
143 case <-shutdown:
144 case <-time.After(500 * time.Millisecond):
145 t.Error("ProgressBar didn't stop")
146 }
147 }