Codebase list golang-github-vbauerster-mpb / 368e8db
refactor some tests Vladimir Bauer 8 years ago
1 changed file(s) with 31 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
44 "fmt"
55 "io/ioutil"
66 "strings"
7 "sync"
87 "testing"
98 "time"
109
3231
3332 func TestBarID(t *testing.T) {
3433 p := New(WithOutput(ioutil.Discard))
34 total := 80
35 wantID := 11
36 bar := p.AddBar(int64(total), BarID(wantID))
3537
36 numBars := 3
37 bars := make([]*Bar, numBars)
38 for i := 0; i < numBars; i++ {
39 bars[i] = p.AddBar(80, BarID(i))
40 go func(bar *Bar) {
41 for i := 0; i < 80; i++ {
42 time.Sleep(10 * time.Millisecond)
43 bar.Increment()
44 }
45 }(bars[i])
38 go func() {
39 for i := 0; i < total; i++ {
40 time.Sleep(50 * time.Millisecond)
41 bar.Increment()
42 }
43 }()
44
45 gotID := bar.ID()
46 if gotID != wantID {
47 t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
4648 }
4749
50 p.Abort(bar)
4851 p.Wait()
49 for wantID, bar := range bars {
50 gotID := bar.ID()
51 if gotID != wantID {
52 t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
53 }
54 }
5552 }
5653
5754 func TestBarIncrWithReFill(t *testing.T) {
6461 till := 30
6562 refillChar := '+'
6663
67 bar := p.AddBar(100, BarTrim())
64 bar := p.AddBar(int64(total), BarTrim())
6865
6966 bar.ResumeFill(refillChar, int64(till))
7067
8582 }
8683
8784 func TestBarPanics(t *testing.T) {
88 var wg sync.WaitGroup
8985 var buf bytes.Buffer
90 p := New(WithDebugOutput(&buf), WithOutput(nil), WithWaitGroup(&wg))
86 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
9187
9288 wantPanic := "Upps!!!"
93 numBars := 1
94 wg.Add(numBars)
89 total := 100
9590
96 for i := 0; i < numBars; i++ {
97 name := fmt.Sprintf("b#%02d:", i)
98 bar := p.AddBar(100, PrependDecorators(
99 func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
100 if s.Current >= 42 {
101 panic(wantPanic)
102 }
103 return name
104 },
105 ))
91 bar := p.AddBar(int64(total), PrependDecorators(
92 func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
93 if s.Current >= 42 {
94 panic(wantPanic)
95 }
96 return "test"
97 },
98 ))
10699
107 go func() {
108 defer wg.Done()
109 for i := 0; i < 100; i++ {
110 time.Sleep(10 * time.Millisecond)
111 bar.Increment()
112 }
113 }()
114 }
100 go func() {
101 for i := 0; i < 100; i++ {
102 time.Sleep(10 * time.Millisecond)
103 bar.Increment()
104 }
105 }()
115106
116107 p.Wait()
117108
118109 wantPanic = fmt.Sprintf("panic: %s", wantPanic)
119
120110 debugStr := buf.String()
121111 if !strings.Contains(debugStr, wantPanic) {
122112 t.Errorf("%q doesn't contain %q\n", debugStr, wantPanic)