Codebase list golang-github-vbauerster-mpb / 325eec6
Some test fixes Vladimir Bauer 8 years ago
2 changed file(s) with 20 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
77 "sync"
88 "testing"
99 "time"
10 "unicode/utf8"
1110
1211 "github.com/vbauerster/mpb"
1312 "github.com/vbauerster/mpb/decor"
1413 )
1514
16 func TestWithWidth(t *testing.T) {
17 cases := map[string]struct{ w, expected int }{
18 "WithWidth-1": {-1, 81},
19 "WithWidth0": {0, 3},
20 "WithWidth1": {1, 3},
21 "WithWidth2": {2, 3},
22 "WithWidth3": {3, 4},
23 "WithWidth60": {60, 61},
24 }
25
26 var buf bytes.Buffer
27 for k, tc := range cases {
28 buf.Reset()
29 p := mpb.New(
30 mpb.Output(&buf),
31 mpb.WithWidth(tc.w),
32 )
33 bar := p.AddBar(10, mpb.BarTrim())
34
35 for i := 0; i < 10; i++ {
36 bar.Increment()
37 }
38
39 p.Wait()
40
41 gotWidth := utf8.RuneCount(buf.Bytes())
42 if gotWidth != tc.expected {
43 t.Errorf("%s: Expected width: %d, got: %d\n", k, tc.expected, gotWidth)
44 }
45 }
46 }
47
4815 func TestBarCompleted(t *testing.T) {
49 p := mpb.New(
50 mpb.Output(ioutil.Discard),
51 )
16 p := mpb.New(mpb.Output(ioutil.Discard))
5217 total := 80
5318 bar := p.AddBar(int64(total))
5419
1616 rand.Seed(time.Now().UnixNano())
1717 }
1818
19 func TestAddBar(t *testing.T) {
19 func TestBarCount(t *testing.T) {
2020 p := mpb.New(mpb.Output(ioutil.Discard))
2121
2222 var wg sync.WaitGroup
2323 wg.Add(1)
24 b := p.AddBar(80)
24 b := p.AddBar(100)
2525 go func() {
26 for i := 0; i < 80; i++ {
26 for i := 0; i < 100; i++ {
2727 if i == 33 {
2828 wg.Done()
2929 }
3030 b.Increment()
31 time.Sleep(randomDuration(80 * time.Millisecond))
31 time.Sleep(randomDuration(100 * time.Millisecond))
3232 }
3333 }()
3434
3838 t.Errorf("BarCount want: %q, got: %q\n", 1, count)
3939 }
4040
41 b.Complete()
41 p.Abort(b)
4242 p.Wait()
4343 }
4444
45 func TestRemoveBars(t *testing.T) {
45 func TestBarAbort(t *testing.T) {
4646 p := mpb.New(mpb.Output(ioutil.Discard))
4747
4848 var wg sync.WaitGroup
49 wg.Add(1)
4950 bars := make([]*mpb.Bar, 3)
5051 for i := 0; i < 3; i++ {
51 wg.Add(1)
52 b := p.AddBar(80)
52 b := p.AddBar(100)
5353 bars[i] = b
54 go func() {
55 for i := 0; i < 80; i++ {
56 if i == 33 {
54 go func(n int) {
55 for i := 0; i < 100; i++ {
56 if n == 0 && i == 33 {
57 p.Abort(b)
5758 wg.Done()
5859 }
5960 b.Increment()
60 time.Sleep(randomDuration(80 * time.Millisecond))
61 time.Sleep(randomDuration(100 * time.Millisecond))
6162 }
62 }()
63 }(i)
6364 }
6465
6566 wg.Wait()
66 for i := 0; i < 3; i++ {
67 i := i
68 go func() {
69 if ok := p.RemoveBar(bars[i]); !ok {
70 t.Errorf("bar %d: remove failed\n", i)
71 }
72 }()
67 count := p.BarCount()
68 if count != 2 {
69 t.Errorf("BarCount want: %q, got: %q\n", 2, count)
7370 }
71 p.Abort(bars[1])
72 p.Abort(bars[2])
7473 p.Wait()
7574 }
7675