Codebase list golang-github-vbauerster-mpb / 638547f
refactoring progress test Vladimir Bauer 4 years ago
1 changed file(s) with 27 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
1616 }
1717
1818 func TestBarCount(t *testing.T) {
19 p := mpb.New(mpb.WithOutput(ioutil.Discard))
19 shutdown := make(chan struct{})
20 p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
2021
2122 check := make(chan struct{})
2223 b := p.AddBar(100)
2627 close(check)
2728 }
2829 b.Increment()
29 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
30 time.Sleep(randomDuration(100 * time.Millisecond))
3031 }
3132 }()
3233
3334 <-check
34 count := p.BarCount()
35 if count != 1 {
35 if count := p.BarCount(); count != 1 {
3636 t.Errorf("BarCount want: %q, got: %q\n", 1, count)
3737 }
3838
3939 b.Abort(false)
40 p.Wait()
40 go p.Wait()
41 select {
42 case <-shutdown:
43 case <-time.After(150 * time.Millisecond):
44 t.Error("Progress didn't shutdown")
45 }
4146 }
4247
4348 func TestBarAbort(t *testing.T) {
49 shutdown := make(chan struct{})
50 p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
4451 n := 2
45 p := mpb.New(mpb.WithOutput(ioutil.Discard))
4652 bars := make([]*mpb.Bar, n)
4753 for i := 0; i < n; i++ {
4854 b := p.AddBar(100)
4955 switch i {
5056 case n - 1:
5157 var abortCalledTimes int
52 for j := 0; !b.Completed(); j++ {
53 if j >= 33 {
58 for j := 0; !b.Aborted(); j++ {
59 if j >= 10 {
5460 b.Abort(true)
5561 abortCalledTimes++
5662 } else {
5763 b.Increment()
58 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
5964 }
6065 }
6166 if abortCalledTimes != 1 {
6974 go func() {
7075 for !b.Completed() {
7176 b.Increment()
72 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
77 time.Sleep(randomDuration(100 * time.Millisecond))
7378 }
7479 }()
7580 }
7782 }
7883
7984 bars[0].Abort(false)
80 p.Wait()
85 go p.Wait()
86 select {
87 case <-shutdown:
88 case <-time.After(150 * time.Millisecond):
89 t.Error("Progress didn't shutdown")
90 }
8191 }
8292
8393 func TestWithContext(t *testing.T) {
8595 ctx, cancel := context.WithCancel(context.Background())
8696 p := mpb.NewWithContext(ctx, mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
8797
88 start := make(chan struct{})
8998 done := make(chan struct{})
9099 fail := make(chan struct{})
91100 bar := p.AddBar(0) // never complete bar
92101 go func() {
93 close(start)
94 for !bar.Completed() {
95 bar.Increment()
102 for !bar.Aborted() {
96103 time.Sleep(randomDuration(100 * time.Millisecond))
104 cancel()
97105 }
98106 close(done)
99107 }()
107115 }
108116 }()
109117
110 <-start
111 cancel()
112118 select {
113119 case <-shutdown:
114120 case <-fail:
132138 end := make(chan struct{})
133139 mpb.MaxWidthDistributor = makeWrapper(mpb.MaxWidthDistributor, start, end)
134140
135 total := 80
141 total := 100
136142 numBars := 6
137143 p := mpb.New(mpb.WithOutput(ioutil.Discard))
138144 for i := 0; i < numBars; i++ {
139145 bar := p.AddBar(int64(total),
140146 mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0),
141 mpb.PrependDecorators(
142 decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace),
143 ),
147 mpb.PrependDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace)),
144148 )
145149 go func() {
146150 <-ready
147151 for i := 0; i < total; i++ {
148152 start := time.Now()
149 if id := bar.ID(); id > 1 && i >= 42 {
153 if id := bar.ID(); id > 1 && i >= 32 {
150154 if id&1 == 1 {
151155 bar.Abort(true)
152156 } else {
153157 bar.Abort(false)
154158 }
155159 }
156 time.Sleep((time.Duration(rand.Intn(10)+1) * (50 * time.Millisecond)) / 2)
160 time.Sleep(randomDuration(100 * time.Millisecond))
157161 bar.IncrInt64(rand.Int63n(5) + 1)
158162 bar.DecoratorEwmaUpdate(time.Since(start))
159163 }