Codebase list golang-github-vbauerster-mpb / b48a312
don't ignore user's rr Vladimir Bauer 9 years ago
2 changed file(s) with 18 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "reflect"
44 "testing"
5 "time"
56 )
67
78 func TestFillBar(t *testing.T) {
7071 },
7172 }
7273
73 stopWidthListen := make(chan struct{})
74 prependWs := newWidthSync(stopWidthListen, 1, 0)
75 appendWs := newWidthSync(stopWidthListen, 1, 0)
74 prependWs := newWidthSync(rr*time.Millisecond, 1, 0)
75 appendWs := newWidthSync(rr*time.Millisecond, 1, 0)
7676 for _, test := range tests {
7777 s := newTestState()
7878 s.width = test.barWidth
9494 done: make(chan struct{}),
9595 wg: new(sync.WaitGroup),
9696 }
97 go p.server(cwriter.New(os.Stdout), time.NewTicker(rr*time.Millisecond))
97 go p.server(cwriter.New(os.Stdout))
9898 return p
9999 }
100100
218218 }
219219
220220 // server monitors underlying channels and renders any progress bars
221 func (p *Progress) server(cw *cwriter.Writer, t *time.Ticker) {
221 func (p *Progress) server(cw *cwriter.Writer) {
222 userRR := rr * time.Millisecond
223 t := time.NewTicker(userRR)
224
222225 defer func() {
223226 t.Stop()
224227 close(p.done)
225228 }()
226 bars := make([]*Bar, 0, 3)
227 var beforeRender BeforeRender
229
228230 var wg sync.WaitGroup
229231 recoverIfPanic := func() {
230232 if p := recover(); p != nil {
235237 }
236238 wg.Done()
237239 }
240 var beforeRender BeforeRender
241 bars := make([]*Bar, 0, 3)
242
238243 for {
239244 select {
240245 case w := <-p.outChangeReqCh:
274279 beforeRender(bars)
275280 }
276281
277 prependWs := newWidthSync(numBars, bars[0].NumOfPrependers())
278 appendWs := newWidthSync(numBars, bars[0].NumOfAppenders())
282 prependWs := newWidthSync(userRR, numBars, bars[0].NumOfPrependers())
283 appendWs := newWidthSync(userRR, numBars, bars[0].NumOfAppenders())
279284
280285 width, _, _ := cwriter.GetTermSize()
281286 ibars := iBarsGen(bars, width)
311316 for _, b := range bars {
312317 b.flushed()
313318 }
314 case d := <-p.rrChangeReqCh:
319 case userRR = <-p.rrChangeReqCh:
315320 t.Stop()
316 t = time.NewTicker(d)
321 t = time.NewTicker(userRR)
317322 case <-p.cancel:
318323 return
319324 }
320325 }
321326 }
322327
323 func newWidthSync(numBars, numColumn int) *widthSync {
328 func newWidthSync(userRR time.Duration, numBars, numColumn int) *widthSync {
324329 ws := &widthSync{
325330 listen: make([]chan int, numColumn),
326331 result: make([]chan int, numColumn),
341346 if len(widths) == numBars {
342347 break loop
343348 }
344 case <-time.After(rr * time.Millisecond):
349 case <-time.After(userRR):
345350 return
346351 }
347352 }