Codebase list golang-github-vbauerster-mpb / 31c430e
Some test updates Vladimir Bauer 8 years ago
2 changed file(s) with 23 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
44 import (
55 "context"
66 "fmt"
7 "io/ioutil"
78 "math/rand"
89 "sync"
910 "testing"
1617 func TestWithContext(t *testing.T) {
1718 ctx, cancel := context.WithCancel(context.Background())
1819 shutdown := make(chan struct{})
19 p := mpb.New(mpb.WithContext(ctx), mpb.WithShutdownNotifier(shutdown))
20 p := mpb.New(
21 mpb.Output(ioutil.Discard),
22 mpb.WithContext(ctx),
23 mpb.WithShutdownNotifier(shutdown),
24 )
2025
2126 var wg sync.WaitGroup
2227 total := 100
2631 for i := 0; i < numBars; i++ {
2732 name := fmt.Sprintf("Bar#%d:", i)
2833 bar := p.AddBar(int64(total), mpb.BarID(i),
29 mpb.PrependDecorators(decor.Name(name, len(name), 0)))
34 mpb.PrependDecorators(decor.StaticName(name, len(name), 0)))
3035
3136 go func() {
3237 defer wg.Done()
3641 return
3742 default:
3843 }
39 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
40 bar.Incr(1)
44 time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
45 bar.Increment()
4146 }
4247 }()
4348 }
22 import (
33 "bytes"
44 "fmt"
5 "io/ioutil"
56 "math/rand"
67 "sync"
78 "testing"
6667 func TestAddBar(t *testing.T) {
6768 var wg sync.WaitGroup
6869 var buf bytes.Buffer
69 p := mpb.New(mpb.Output(&buf))
70 p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg))
7071
7172 count := p.BarCount()
7273 if count != 0 {
7879
7980 for i := 0; i < numBars; i++ {
8081 name := fmt.Sprintf("Bar#%d:", i)
81 bar := p.AddBar(100, mpb.PrependDecorators(decor.Name(name, len(name), 0)))
82 bar := p.AddBar(100, mpb.PrependDecorators(decor.StaticName(name, len(name), 0)))
8283
8384 go func() {
8485 defer wg.Done()
9293 if count != numBars {
9394 t.Errorf("BarCount want: %q, got: %q\n", numBars, count)
9495 }
95 wg.Wait()
9696 p.Stop()
9797 }
9898
113113 }
114114
115115 func TestWithCancel(t *testing.T) {
116 var wg sync.WaitGroup
116117 cancel := make(chan struct{})
117118 shutdown := make(chan struct{})
118 p := mpb.New(mpb.WithCancel(cancel), mpb.WithShutdownNotifier(shutdown))
119 p := mpb.New(
120 mpb.Output(ioutil.Discard),
121 mpb.WithCancel(cancel),
122 mpb.WithShutdownNotifier(shutdown),
123 mpb.WithWaitGroup(&wg),
124 )
119125
120 var wg sync.WaitGroup
121126 total := 100
122127 numBars := 3
123128 wg.Add(numBars)
125130 for i := 0; i < numBars; i++ {
126131 name := fmt.Sprintf("Bar#%d:", i)
127132 bar := p.AddBar(int64(total), mpb.BarID(i),
128 mpb.PrependDecorators(decor.Name(name, len(name), 0)))
133 mpb.PrependDecorators(decor.StaticName(name, len(name), 0)))
129134
130135 go func() {
131 defer func() {
132 // fmt.Printf("%s done\n", name)
133 wg.Done()
134 }()
136 defer wg.Done()
135137 for i := 0; i < total; i++ {
136138 select {
137139 case <-cancel:
138140 return
139141 default:
140142 }
141 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
142 bar.Incr(1)
143 time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
144 bar.Increment()
143145 }
144146 }()
145147 }
148150 close(cancel)
149151 })
150152
151 wg.Wait()
152153 p.Stop()
153154
154155 select {