Codebase list golang-github-vbauerster-mpb / b19f2d4
Tests refactoring Vladimir Bauer 8 years ago
3 changed file(s) with 28 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
4545 }
4646 }
4747
48 func TestBarInProgress(t *testing.T) {
49 var buf bytes.Buffer
50 cancel := make(chan struct{})
48 func TestBarCompleted(t *testing.T) {
5149 p := mpb.New(
52 mpb.Output(&buf),
53 mpb.WithCancel(cancel),
50 mpb.Output(ioutil.Discard),
5451 )
55 bar := p.AddBar(100, mpb.BarTrim())
52 total := 80
53 bar := p.AddBar(int64(total))
5654
57 stopped := make(chan struct{})
55 var count int
56 for !bar.Completed() {
57 time.Sleep(10 * time.Millisecond)
58 bar.Increment()
59 count++
60 }
5861
59 go func() {
60 defer close(stopped)
61 for bar.InProgress() {
62 time.Sleep(10 * time.Millisecond)
63 bar.Increment()
64 }
65 }()
66
67 time.Sleep(250 * time.Millisecond)
68 close(cancel)
6962 p.Stop()
70
71 select {
72 case <-stopped:
73 case <-time.After(300 * time.Millisecond):
74 t.Error("bar.InProgress returns true after cancel")
63 if count != total {
64 t.Errorf("got count: %d, expected %d\n", count, total)
7565 }
7666 }
7767
78 func TestBarGetID(t *testing.T) {
79 var wg sync.WaitGroup
80 p := mpb.New(
81 mpb.Output(ioutil.Discard),
82 mpb.WithWaitGroup(&wg),
83 )
68 func TestBarID(t *testing.T) {
69 p := mpb.New(mpb.Output(ioutil.Discard))
8470
8571 numBars := 3
86 wg.Add(numBars)
87
8872 bars := make([]*mpb.Bar, numBars)
8973 for i := 0; i < numBars; i++ {
90 bars[i] = p.AddBar(100, mpb.BarID(i))
91
74 bars[i] = p.AddBar(80, mpb.BarID(i))
9275 go func(bar *mpb.Bar) {
93 defer wg.Done()
94 for i := 0; i < 100; i++ {
76 for i := 0; i < 80; i++ {
9577 time.Sleep(10 * time.Millisecond)
9678 bar.Increment()
9779 }
9880 }(bars[i])
9981 }
10082
83 p.Stop()
10184 for wantID, bar := range bars {
10285 gotID := bar.ID()
10386 if gotID != wantID {
10487 t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
10588 }
10689 }
107
108 p.Stop()
10990 }
11091
11192 func TestBarIncrWithReFill(t *testing.T) {
127108
128109 for i := 0; i < total; i++ {
129110 bar.Increment()
111 time.Sleep(10 * time.Millisecond)
130112 }
131113
132114 p.Stop()
4545 p.Stop()
4646 }
4747
48 func ExampleBar_InProgress() {
48 func ExampleBar_Completed() {
4949 p := mpb.New()
5050 bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0)))
5151
52 for bar.InProgress() {
52 for !bar.Completed() {
5353 time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
5454 bar.Increment()
5555 }
1010 "time"
1111
1212 "github.com/vbauerster/mpb"
13 "github.com/vbauerster/mpb/decor"
1413 )
1514
1615 func init() {
9089 current int64
9190 }
9291
93 resStream := make(chan *sample, numBars)
92 bars := make([]*mpb.Bar, 0, numBars)
9493 for i := 0; i < numBars; i++ {
95 bar := p.AddBar(int64(200),
96 mpb.BarID(i),
97 mpb.PrependDecorators(
98 func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
99 if s.Completed {
100 resStream <- &sample{
101 id: s.ID,
102 total: s.Total,
103 current: s.Current,
104 }
105 }
106 return ""
107 },
108 ))
109
94 bar := p.AddBar(int64(1000), mpb.BarID(i))
95 bars = append(bars, bar)
11096 go func() {
111 for bar.InProgress() {
97 for !bar.Completed() {
98 time.Sleep(randomDuration(40 * time.Millisecond))
11299 bar.Increment()
113 time.Sleep(randomDuration(80 * time.Millisecond))
114100 }
115101 }()
116102 }
120106 })
121107
122108 p.Stop()
123 close(resStream)
124 for res := range resStream {
125 if res.current >= res.total {
126 t.Errorf("bar %d: total = %d, current = %d\n", res.id, res.total, res.current)
109 for _, bar := range bars {
110 if bar.Current() >= bar.Total() {
111 t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current())
127112 }
128113 }
129114 }