Codebase list golang-github-vbauerster-mpb / 71941f7
add EnableTriggerComplete Vladimir Bauer 4 years ago
2 changed file(s) with 72 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
154154 }
155155 }
156156
157 // EnableTriggerComplete enables triggering complete event by increment
158 // methods or triggers it now if bar already has been incremented up
159 // to the total. Triggering complete event is auto disabled if bar
160 // constructed with `total <= 0`.
161 func (b *Bar) EnableTriggerComplete() {
162 select {
163 case b.operateState <- func(s *bState) {
164 if s.triggerComplete {
165 return
166 }
167 if s.current >= s.total {
168 s.current = s.total
169 s.completed = true
170 go b.forceRefresh()
171 } else {
172 s.triggerComplete = true
173 }
174 }:
175 case <-b.done:
176 }
177 }
178
157179 // SetTotal sets total to an arbitrary value. Setting it to negative
158180 // value is equivalent to (*Bar).SetTotal((*Bar).Current(), bool).
159181 func (b *Bar) SetTotal(total int64, triggerCompleteNow bool) {
4545
4646 if !bar.Aborted() {
4747 t.Error("bar isn't aborted after abort call")
48 }
49
50 p.Wait()
51 }
52
53 func TestBarEnableTriggerCompleteAndIncrementBefore(t *testing.T) {
54 p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(ioutil.Discard))
55 bar := p.AddBar(0) // never complete bar
56
57 for _, f := range []func(){
58 func() { bar.SetTotal(40, false) },
59 func() { bar.IncrBy(60) },
60 func() { bar.SetTotal(80, false) },
61 func() { bar.IncrBy(20) },
62 } {
63 f()
64 if bar.Completed() {
65 t.Fail()
66 }
67 }
68
69 bar.EnableTriggerComplete()
70
71 if !bar.Completed() {
72 t.Fail()
73 }
74
75 p.Wait()
76 }
77
78 func TestBarEnableTriggerCompleteAndIncrementAfter(t *testing.T) {
79 p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(ioutil.Discard))
80 bar := p.AddBar(0) // never complete bar
81
82 for _, f := range []func(){
83 func() { bar.SetTotal(40, false) },
84 func() { bar.IncrBy(60) },
85 func() { bar.SetTotal(80, false) },
86 func() { bar.EnableTriggerComplete() },
87 } {
88 f()
89 if bar.Completed() {
90 t.Fail()
91 }
92 }
93
94 bar.IncrBy(20)
95
96 if !bar.Completed() {
97 t.Fail()
4898 }
4999
50100 p.Wait()