Codebase list golang-github-vbauerster-mpb / 674667e
refactoring TestDecorStatisticsAvailableWidth Vladimir Bauer 3 years ago
1 changed file(s) with 19 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
198198 }
199199
200200 func TestDecorStatisticsAvailableWidth(t *testing.T) {
201 var called [2]bool
201 ch := make(chan int, 2)
202202 td1 := func(s decor.Statistics) string {
203 if s.AvailableWidth != 80 {
204 t.Errorf("expected AvailableWidth %d got %d", 80, s.AvailableWidth)
205 }
206 called[0] = true
203 ch <- s.AvailableWidth
207204 return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("0", 20))
208205 }
209206 td2 := func(s decor.Statistics) string {
210 if s.AvailableWidth != 40 {
211 t.Errorf("expected AvailableWidth %d got %d", 40, s.AvailableWidth)
212 }
213 called[1] = true
207 ch <- s.AvailableWidth
214208 return ""
215209 }
216210 ctx, cancel := context.WithCancel(context.Background())
217211 refresh := make(chan interface{})
218 p := mpb.NewWithContext(ctx, mpb.WithWidth(100),
212 p := mpb.NewWithContext(ctx,
213 mpb.WithWidth(100),
219214 mpb.WithManualRefresh(refresh),
220215 mpb.WithOutput(io.Discard),
221216 )
231226 ),
232227 )
233228 refresh <- time.Now()
234 cancel()
235 p.Wait()
236 for i, ok := range called {
237 if !ok {
238 t.Errorf("Decorator %d isn't called", i+1)
239 }
240 }
241 }
229 go func() {
230 time.Sleep(10 * time.Millisecond)
231 cancel()
232 }()
233 p.Wait()
234
235 if availableWidth := <-ch; availableWidth != 80 {
236 t.Errorf("expected AvailableWidth %d got %d", 80, availableWidth)
237 }
238
239 if availableWidth := <-ch; availableWidth != 40 {
240 t.Errorf("expected AvailableWidth %d got %d", 40, availableWidth)
241 }
242 }