Codebase list golang-github-vbauerster-mpb / 971e032
newWidthSync accepts quit channel Vladimir Bauer 9 years ago
2 changed file(s) with 14 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "reflect"
44 "testing"
5 "time"
65 )
76
87 func TestFillBar(t *testing.T) {
2423 barWidth: 100,
2524 total: 100,
2625 current: 20,
27 want: []byte("[>]"),
26 want: []byte("[]"),
2827 },
2928 {
3029 termWidth: 20,
8584 },
8685 }
8786
88 prependWs := newWidthSync(rr*time.Millisecond, 1, 0)
89 appendWs := newWidthSync(rr*time.Millisecond, 1, 0)
87 prependWs := newWidthSync(nil, 1, 0)
88 appendWs := newWidthSync(nil, 1, 0)
9089 for _, test := range tests {
9190 s := newTestState()
9291 s.width = test.barWidth
237237 }
238238 wg.Done()
239239 }
240
240241 var beforeRender BeforeRender
241242 cw := cwriter.New(os.Stdout)
242243 bars := make([]*Bar, 0, 3)
280281 beforeRender(bars)
281282 }
282283
283 prependWs := newWidthSync(userRR, numBars, bars[0].NumOfPrependers())
284 appendWs := newWidthSync(userRR, numBars, bars[0].NumOfAppenders())
284 quitWidthSyncCh := make(chan struct{})
285 time.AfterFunc(userRR, func() {
286 close(quitWidthSyncCh)
287 })
288
289 b0 := bars[0]
290 prependWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfPrependers())
291 appendWs := newWidthSync(quitWidthSyncCh, numBars, b0.NumOfAppenders())
285292
286293 width, _, _ := cwriter.GetTermSize()
287294 ibars := iBarsGen(bars, width)
326333 }
327334 }
328335
329 func newWidthSync(userRR time.Duration, numBars, numColumn int) *widthSync {
336 func newWidthSync(quit <-chan struct{}, numBars, numColumn int) *widthSync {
330337 ws := &widthSync{
331338 listen: make([]chan int, numColumn),
332339 result: make([]chan int, numColumn),
339346 go func(listenCh <-chan int, resultCh chan<- int) {
340347 defer close(resultCh)
341348 widths := make([]int, 0, numBars)
342 abandon := time.After(userRR)
343349 loop:
344350 for {
345351 select {
348354 if len(widths) == numBars {
349355 break loop
350356 }
351 case <-abandon:
357 case <-quit:
352358 return
353359 }
354360 }