forked from vbauerster/mpb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress_test.go
191 lines (163 loc) · 3.86 KB
/
progress_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package mpb_test
import (
"context"
"io/ioutil"
"math/rand"
"testing"
"time"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
)
const (
timeout = 200 * time.Millisecond
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestBarCount(t *testing.T) {
shutdown := make(chan struct{})
p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
b := p.AddBar(0, mpb.BarRemoveOnComplete())
if count := p.BarCount(); count != 1 {
t.Errorf("BarCount want: %d, got: %d\n", 1, count)
}
b.SetTotal(100, true)
b.Wait()
if count := p.BarCount(); count != 0 {
t.Errorf("BarCount want: %d, got: %d\n", 0, count)
}
go p.Wait()
select {
case <-shutdown:
case <-time.After(timeout):
t.Errorf("Progress didn't shutdown after %v", timeout)
}
}
func TestBarAbort(t *testing.T) {
shutdown := make(chan struct{})
p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
n := 2
bars := make([]*mpb.Bar, n)
for i := 0; i < n; i++ {
b := p.AddBar(100)
switch i {
case n - 1:
var abortCalledTimes int
for j := 0; !b.Aborted(); j++ {
if j >= 10 {
b.Abort(true)
abortCalledTimes++
} else {
b.Increment()
}
}
if abortCalledTimes != 1 {
t.Errorf("Expected abortCalledTimes: %d, got: %d\n", 1, abortCalledTimes)
}
count := p.BarCount()
if count != 1 {
t.Errorf("BarCount want: %d, got: %d\n", 1, count)
}
default:
go func() {
for !b.Completed() {
b.Increment()
time.Sleep(randomDuration(100 * time.Millisecond))
}
}()
}
bars[i] = b
}
bars[0].Abort(false)
go p.Wait()
select {
case <-shutdown:
case <-time.After(timeout):
t.Errorf("Progress didn't shutdown after %v", timeout)
}
}
func TestWithContext(t *testing.T) {
shutdown := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
p := mpb.NewWithContext(ctx, mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard))
done := make(chan struct{})
bar := p.AddBar(0) // never complete bar
go func() {
for !bar.Aborted() {
time.Sleep(randomDuration(100 * time.Millisecond))
cancel()
}
close(done)
}()
go func() {
<-done
p.Wait()
}()
select {
case <-shutdown:
case <-time.After(timeout):
t.Errorf("Progress didn't shutdown after %v", timeout)
}
}
// MaxWidthDistributor shouldn't stuck in the middle while removing or aborting a bar
func TestMaxWidthDistributor(t *testing.T) {
makeWrapper := func(f func([]chan int), start, end chan struct{}) func([]chan int) {
return func(column []chan int) {
start <- struct{}{}
f(column)
<-end
}
}
ready := make(chan struct{})
start := make(chan struct{})
end := make(chan struct{})
mpb.MaxWidthDistributor = makeWrapper(mpb.MaxWidthDistributor, start, end)
total := 100
numBars := 6
p := mpb.New(mpb.WithOutput(ioutil.Discard))
for i := 0; i < numBars; i++ {
bar := p.AddBar(int64(total),
mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0),
mpb.PrependDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace)),
)
go func() {
<-ready
for i := 0; i < total; i++ {
start := time.Now()
if id := bar.ID(); id > 1 && i >= 32 {
if id&1 == 1 {
bar.Abort(true)
} else {
bar.Abort(false)
}
}
time.Sleep(randomDuration(100 * time.Millisecond))
bar.IncrInt64(rand.Int63n(5) + 1)
bar.DecoratorEwmaUpdate(time.Since(start))
}
}()
}
go func() {
<-ready
p.Wait()
close(start)
}()
res := t.Run("maxWidthDistributor", func(t *testing.T) {
close(ready)
for v := range start {
timer := time.NewTimer(100 * time.Millisecond)
select {
case end <- v:
timer.Stop()
case <-timer.C:
t.FailNow()
}
}
})
if !res {
t.Error("maxWidthDistributor stuck in the middle")
}
}
func randomDuration(max time.Duration) time.Duration {
return time.Duration(rand.Intn(10)+1) * max / 10
}