Codebase list golang-github-vbauerster-mpb / ae27546
TestMaxWidthDistributor Vladimir Bauer 5 years ago
3 changed file(s) with 87 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
11
22 // make syncWidth func public in test
33 var SyncWidth = syncWidth
4 var MaxWidthDistributor = &maxWidthDistributor
371371
372372 func syncWidth(matrix map[int][]chan int) {
373373 for _, column := range matrix {
374 column := column
375 go func() {
376 var maxWidth int
377 for _, ch := range column {
378 if w := <-ch; w > maxWidth {
379 maxWidth = w
380 }
381 }
382 for _, ch := range column {
383 ch <- maxWidth
384 }
385 }()
386 }
387 }
374 go maxWidthDistributor(column)
375 }
376 }
377
378 var maxWidthDistributor = func(column []chan int) {
379 var maxWidth int
380 for _, ch := range column {
381 if w := <-ch; w > maxWidth {
382 maxWidth = w
383 }
384 }
385 for _, ch := range column {
386 ch <- maxWidth
387 }
388 }
99 "time"
1010
1111 "github.com/vbauerster/mpb/v5"
12 "github.com/vbauerster/mpb/v5/decor"
1213 )
1314
1415 func init() {
2223 wg.Add(1)
2324 b := p.AddBar(100)
2425 go func() {
26 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
2527 for i := 0; i < 100; i++ {
2628 if i == 33 {
2729 wg.Done()
2830 }
2931 b.Increment()
30 time.Sleep(randomDuration(100 * time.Millisecond))
32 time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
3133 }
3234 }()
3335
4951 bars := make([]*mpb.Bar, 3)
5052 for i := 0; i < 3; i++ {
5153 b := p.AddBar(100)
52 bars[i] = b
54 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
5355 go func(n int) {
5456 for i := 0; !b.Completed(); i++ {
5557 if n == 0 && i >= 33 {
5759 wg.Done()
5860 }
5961 b.Increment()
60 time.Sleep(randomDuration(100 * time.Millisecond))
62 time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
6163 }
6264 }(i)
65 bars[i] = b
6366 }
6467
6568 wg.Wait()
106109 }
107110 }
108111
112 // MaxWidthDistributor shouldn't stuck in the middle while removing or aborting a bar
113 func TestMaxWidthDistributor(t *testing.T) {
114
115 makeWrapper := func(f func([]chan int), start, end chan struct{}) func([]chan int) {
116 return func(column []chan int) {
117 start <- struct{}{}
118 f(column)
119 <-end
120 }
121 }
122
123 ready := make(chan struct{})
124 start := make(chan struct{})
125 end := make(chan struct{})
126 *mpb.MaxWidthDistributor = makeWrapper(*mpb.MaxWidthDistributor, start, end)
127
128 total := 80
129 numBars := 3
130 p := mpb.New(mpb.WithOutput(ioutil.Discard))
131 for i := 0; i < numBars; i++ {
132 bar := p.AddBar(int64(total),
133 mpb.BarOptOn(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }),
134 mpb.PrependDecorators(
135 decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace),
136 ),
137 )
138 go func() {
139 <-ready
140 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
141 for i := 0; i < total; i++ {
142 start := time.Now()
143 if bar.ID() >= numBars-1 && i >= 42 {
144 bar.Abort(true)
145 }
146 time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
147 bar.Increment()
148 bar.DecoratorEwmaUpdate(time.Since(start))
149 }
150 }()
151 }
152
153 go func() {
154 <-ready
155 p.Wait()
156 close(start)
157 }()
158
159 res := t.Run("maxWidthDistributor", func(t *testing.T) {
160 close(ready)
161 for v := range start {
162 timer := time.NewTimer(100 * time.Millisecond)
163 select {
164 case end <- v:
165 timer.Stop()
166 case <-timer.C:
167 t.FailNow()
168 }
169 }
170 })
171
172 if !res {
173 t.Error("maxWidthDistributor stuck in the middle")
174 }
175 }
176
109177 func getLastLine(bb []byte) []byte {
110178 split := bytes.Split(bb, []byte("\n"))
111179 return split[len(split)-2]