Codebase list golang-github-vbauerster-mpb / 854c09a
refactor TestDecorStatisticsAvailableWidth Vladimir Bauer 4 years ago
1 changed file(s) with 17 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "bytes"
4 "context"
45 "fmt"
56 "io/ioutil"
67 "strings"
222223 }
223224
224225 func TestDecorStatisticsAvailableWidth(t *testing.T) {
225 total := 100
226 down := make(chan struct{})
227 checkDone := make(chan struct{})
226 var called [2]bool
228227 td1 := func(s decor.Statistics) string {
229228 if s.AvailableWidth != 80 {
230229 t.Errorf("expected AvailableWidth %d got %d\n", 80, s.AvailableWidth)
231230 }
231 called[0] = true
232232 return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("0", 20))
233233 }
234234 td2 := func(s decor.Statistics) string {
235 defer func() {
236 select {
237 case checkDone <- struct{}{}:
238 default:
239 }
240 }()
241235 if s.AvailableWidth != 40 {
242236 t.Errorf("expected AvailableWidth %d got %d\n", 40, s.AvailableWidth)
243237 }
238 called[1] = true
244239 return ""
245240 }
246 p := mpb.New(
247 mpb.WithWidth(100),
248 mpb.WithShutdownNotifier(down),
241 ctx, cancel := context.WithCancel(context.Background())
242 refresh := make(chan interface{})
243 p := mpb.NewWithContext(ctx, mpb.WithWidth(100),
244 mpb.WithManualRefresh(refresh),
249245 mpb.WithOutput(ioutil.Discard),
250246 )
251 bar := p.AddBar(int64(total),
247 _ = p.AddBar(0,
252248 mpb.BarFillerTrim(),
253249 mpb.PrependDecorators(
254250 decor.Name(strings.Repeat("0", 20)),
259255 decor.Any(td2),
260256 ),
261257 )
262 go func() {
263 for {
264 select {
265 case <-checkDone:
266 bar.Abort(true)
267 case <-down:
268 return
269 }
270 }
271 }()
272 for !bar.Completed() {
273 bar.Increment()
274 }
275 p.Wait()
258 refresh <- time.Now()
259 cancel()
260 p.Wait()
261 for i, ok := range called {
262 if !ok {
263 t.Errorf("Decorator %d isn't called", i+1)
264 }
265 }
276266 }
277267
278268 func panicDecorator(panicMsg string, cond func(decor.Statistics) bool) decor.Decorator {