Some test fixes
Vladimir Bauer
8 years ago
| 7 | 7 |
"sync"
|
| 8 | 8 |
"testing"
|
| 9 | 9 |
"time"
|
| 10 | |
"unicode/utf8"
|
| 11 | 10 |
|
| 12 | 11 |
"github.com/vbauerster/mpb"
|
| 13 | 12 |
"github.com/vbauerster/mpb/decor"
|
| 14 | 13 |
)
|
| 15 | 14 |
|
| 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 | |
|
| 48 | 15 |
func TestBarCompleted(t *testing.T) {
|
| 49 | |
p := mpb.New(
|
| 50 | |
mpb.Output(ioutil.Discard),
|
| 51 | |
)
|
|
16 |
p := mpb.New(mpb.Output(ioutil.Discard))
|
| 52 | 17 |
total := 80
|
| 53 | 18 |
bar := p.AddBar(int64(total))
|
| 54 | 19 |
|
| 16 | 16 |
rand.Seed(time.Now().UnixNano())
|
| 17 | 17 |
}
|
| 18 | 18 |
|
| 19 | |
func TestAddBar(t *testing.T) {
|
|
19 |
func TestBarCount(t *testing.T) {
|
| 20 | 20 |
p := mpb.New(mpb.Output(ioutil.Discard))
|
| 21 | 21 |
|
| 22 | 22 |
var wg sync.WaitGroup
|
| 23 | 23 |
wg.Add(1)
|
| 24 | |
b := p.AddBar(80)
|
|
24 |
b := p.AddBar(100)
|
| 25 | 25 |
go func() {
|
| 26 | |
for i := 0; i < 80; i++ {
|
|
26 |
for i := 0; i < 100; i++ {
|
| 27 | 27 |
if i == 33 {
|
| 28 | 28 |
wg.Done()
|
| 29 | 29 |
}
|
| 30 | 30 |
b.Increment()
|
| 31 | |
time.Sleep(randomDuration(80 * time.Millisecond))
|
|
31 |
time.Sleep(randomDuration(100 * time.Millisecond))
|
| 32 | 32 |
}
|
| 33 | 33 |
}()
|
| 34 | 34 |
|
|
| 38 | 38 |
t.Errorf("BarCount want: %q, got: %q\n", 1, count)
|
| 39 | 39 |
}
|
| 40 | 40 |
|
| 41 | |
b.Complete()
|
|
41 |
p.Abort(b)
|
| 42 | 42 |
p.Wait()
|
| 43 | 43 |
}
|
| 44 | 44 |
|
| 45 | |
func TestRemoveBars(t *testing.T) {
|
|
45 |
func TestBarAbort(t *testing.T) {
|
| 46 | 46 |
p := mpb.New(mpb.Output(ioutil.Discard))
|
| 47 | 47 |
|
| 48 | 48 |
var wg sync.WaitGroup
|
|
49 |
wg.Add(1)
|
| 49 | 50 |
bars := make([]*mpb.Bar, 3)
|
| 50 | 51 |
for i := 0; i < 3; i++ {
|
| 51 | |
wg.Add(1)
|
| 52 | |
b := p.AddBar(80)
|
|
52 |
b := p.AddBar(100)
|
| 53 | 53 |
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)
|
| 57 | 58 |
wg.Done()
|
| 58 | 59 |
}
|
| 59 | 60 |
b.Increment()
|
| 60 | |
time.Sleep(randomDuration(80 * time.Millisecond))
|
|
61 |
time.Sleep(randomDuration(100 * time.Millisecond))
|
| 61 | 62 |
}
|
| 62 | |
}()
|
|
63 |
}(i)
|
| 63 | 64 |
}
|
| 64 | 65 |
|
| 65 | 66 |
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)
|
| 73 | 70 |
}
|
|
71 |
p.Abort(bars[1])
|
|
72 |
p.Abort(bars[2])
|
| 74 | 73 |
p.Wait()
|
| 75 | 74 |
}
|
| 76 | 75 |
|