Codebase list golang-github-vbauerster-mpb / c24264d
Shutdown does one operation, refactoring and more tests Vladimir Bauer 8 years ago
6 changed file(s) with 60 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
261261 }
262262 }
263263
264 // Complete signals to the bar, that process has been completed.
265 // You should call this method when total is unknown and you've reached the point
266 // of process completion. If you don't call this method, it will be called
267 // implicitly, upon p.Stop() call.
264 // Complete stops bar's progress tracking, but not removes the bar.
265 // If you need to remove, call Progress.RemoveBar(*Bar) instead.
268266 func (b *Bar) Complete() {
269267 b.once.Do(b.shutdown)
268 <-b.quit
270269 }
271270
272271 func (b *Bar) shutdown() {
273272 b.quit <- struct{}{}
274 <-b.quit
275273 }
276274
277275 func (b *Bar) serve(s *bState, wg *sync.WaitGroup, cancel <-chan struct{}) {
278 defer wg.Done()
279
280276 for {
281277 select {
282278 case op := <-b.operateState:
288284 case <-b.quit:
289285 b.cacheState = s
290286 close(b.quit)
287 wg.Done()
291288 return
292289 }
293290 }
4141 }
4242 }
4343
44 // WithRefreshRate overrides default 100ms refresh rate
44 // WithRefreshRate overrides default 120ms refresh rate
4545 func WithRefreshRate(d time.Duration) ProgressOption {
4646 return func(s *pState) {
47 if d < 10*time.Millisecond {
48 return
49 }
4750 s.ticker.Stop()
4851 s.ticker = time.NewTicker(d)
4952 s.rr = d
156156 if p.ewg != nil {
157157 p.ewg.Wait()
158158 }
159 // wait for all bars to quit
159 // first wait for all bars to quit
160160 p.wg.Wait()
161161 p.once.Do(p.shutdown)
162 <-p.quit
162163 }
163164
164165 func (p *Progress) shutdown() {
165166 p.quit <- struct{}{}
166 <-p.quit
167167 }
168168
169169 func newWidthSync(timeout <-chan struct{}, numBars, numColumn int) *widthSync {
205205
206206 func (s *pState) writeAndFlush(tw, numP, numA int) (err error) {
207207 wSyncTimeout := make(chan struct{})
208 time.AfterFunc(s.rr, func() {
208 time.AfterFunc(s.rr-s.rr/12, func() {
209209 close(wSyncTimeout)
210210 })
211211
6060 s.cancel = nil
6161 // don't return here, p.Stop() must be called eventually
6262 case <-p.quit:
63 close(p.quit)
6364 if s.cancel != nil {
6465 s.ticker.Stop()
6566 }
6768 close(s.shutdownNotifier)
6869 }
6970 signal.Stop(winch)
70 close(p.quit)
7171 return
7272 }
7373 }
5050 p.Stop()
5151 }
5252
53 func TestRemoveBars(t *testing.T) {
54 p := mpb.New(mpb.Output(ioutil.Discard))
55
56 var wg sync.WaitGroup
57 bars := make([]*mpb.Bar, 3)
58 for i := 0; i < 3; i++ {
59 wg.Add(1)
60 b := p.AddBar(80)
61 bars[i] = b
62 go func() {
63 for i := 0; i < 80; i++ {
64 if i == 33 {
65 wg.Done()
66 }
67 b.Increment()
68 time.Sleep(randomDuration(80 * time.Millisecond))
69 }
70 }()
71 }
72
73 wg.Wait()
74 for i := 0; i < 3; i++ {
75 i := i
76 go func() {
77 if ok := p.RemoveBar(bars[i]); !ok {
78 t.Errorf("bar %d: remove failed\n", i)
79 }
80 }()
81 }
82 p.Stop()
83 }
84
5385 func TestWithCancel(t *testing.T) {
5486 var wg sync.WaitGroup
5587 cancel := make(chan struct{})
78110 return
79111 default:
80112 }
81 time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100)
113 time.Sleep(randomDuration(80 * time.Millisecond))
82114 bar.Increment()
83115 }
84116 }()
106138 mpb.WithCancel(cancel),
107139 mpb.WithFormat(customFormat),
108140 )
109 bar := p.AddBar(100, mpb.BarTrim())
141 bar := p.AddBar(80, mpb.BarTrim())
110142
143 var wg sync.WaitGroup
144 wg.Add(1)
111145 go func() {
112 for i := 0; i < 100; i++ {
113 time.Sleep(10 * time.Millisecond)
146 for i := 0; i < 80; i++ {
147 if i == 33 {
148 wg.Done()
149 }
150 time.Sleep(randomDuration(80 * time.Millisecond))
114151 bar.Increment()
115152 }
116153 }()
117154
118 time.Sleep(300 * time.Millisecond)
155 wg.Wait()
119156 close(cancel)
120157 p.Stop()
121158
138175 bar := p.AddBar(100, mpb.BarTrim())
139176
140177 for i := 0; i < 100; i++ {
141 time.Sleep(10 * time.Millisecond)
178 time.Sleep(randomDuration(40 * time.Millisecond))
142179 bar.Increment()
143180 }
144181
150187 t.Errorf("Expected format: %s, got %s\n", want, got)
151188 }
152189 }
190
191 func randomDuration(max time.Duration) time.Duration {
192 return time.Duration(rand.Intn(10)+1) * max / 10
193 }
3737 s.cancel = nil
3838 // don't return here, p.Stop() must be called eventually
3939 case <-p.quit:
40 close(p.quit)
4041 if s.cancel != nil {
4142 s.ticker.Stop()
4243 }
4344 if s.shutdownNotifier != nil {
4445 close(s.shutdownNotifier)
4546 }
46 close(p.quit)
4747 return
4848 }
4949 }