| 9 | 9 |
"time"
|
| 10 | 10 |
|
| 11 | 11 |
"github.com/vbauerster/mpb/v5"
|
|
12 |
"github.com/vbauerster/mpb/v5/decor"
|
| 12 | 13 |
)
|
| 13 | 14 |
|
| 14 | 15 |
func init() {
|
|
| 22 | 23 |
wg.Add(1)
|
| 23 | 24 |
b := p.AddBar(100)
|
| 24 | 25 |
go func() {
|
|
26 |
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
| 25 | 27 |
for i := 0; i < 100; i++ {
|
| 26 | 28 |
if i == 33 {
|
| 27 | 29 |
wg.Done()
|
| 28 | 30 |
}
|
| 29 | 31 |
b.Increment()
|
| 30 | |
time.Sleep(randomDuration(100 * time.Millisecond))
|
|
32 |
time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
|
| 31 | 33 |
}
|
| 32 | 34 |
}()
|
| 33 | 35 |
|
|
| 49 | 51 |
bars := make([]*mpb.Bar, 3)
|
| 50 | 52 |
for i := 0; i < 3; i++ {
|
| 51 | 53 |
b := p.AddBar(100)
|
| 52 | |
bars[i] = b
|
|
54 |
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
| 53 | 55 |
go func(n int) {
|
| 54 | 56 |
for i := 0; !b.Completed(); i++ {
|
| 55 | 57 |
if n == 0 && i >= 33 {
|
|
| 57 | 59 |
wg.Done()
|
| 58 | 60 |
}
|
| 59 | 61 |
b.Increment()
|
| 60 | |
time.Sleep(randomDuration(100 * time.Millisecond))
|
|
62 |
time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
|
| 61 | 63 |
}
|
| 62 | 64 |
}(i)
|
|
65 |
bars[i] = b
|
| 63 | 66 |
}
|
| 64 | 67 |
|
| 65 | 68 |
wg.Wait()
|
|
| 106 | 109 |
}
|
| 107 | 110 |
}
|
| 108 | 111 |
|
|
112 |
// MaxWidthDistributor shouldn't stuck in the middle while removing or aborting a bar
|
|
113 |
func TestMaxWidthDistributor(t *testing.T) {
|
|
114 |
|
|
115 |
makeWrapper := func(f func([]chan int), start, end chan struct{}) func([]chan int) {
|
|
116 |
return func(column []chan int) {
|
|
117 |
start <- struct{}{}
|
|
118 |
f(column)
|
|
119 |
<-end
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
ready := make(chan struct{})
|
|
124 |
start := make(chan struct{})
|
|
125 |
end := make(chan struct{})
|
|
126 |
*mpb.MaxWidthDistributor = makeWrapper(*mpb.MaxWidthDistributor, start, end)
|
|
127 |
|
|
128 |
total := 80
|
|
129 |
numBars := 3
|
|
130 |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
|
131 |
for i := 0; i < numBars; i++ {
|
|
132 |
bar := p.AddBar(int64(total),
|
|
133 |
mpb.BarOptOn(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }),
|
|
134 |
mpb.PrependDecorators(
|
|
135 |
decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace),
|
|
136 |
),
|
|
137 |
)
|
|
138 |
go func() {
|
|
139 |
<-ready
|
|
140 |
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
141 |
for i := 0; i < total; i++ {
|
|
142 |
start := time.Now()
|
|
143 |
if bar.ID() >= numBars-1 && i >= 42 {
|
|
144 |
bar.Abort(true)
|
|
145 |
}
|
|
146 |
time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
|
|
147 |
bar.Increment()
|
|
148 |
bar.DecoratorEwmaUpdate(time.Since(start))
|
|
149 |
}
|
|
150 |
}()
|
|
151 |
}
|
|
152 |
|
|
153 |
go func() {
|
|
154 |
<-ready
|
|
155 |
p.Wait()
|
|
156 |
close(start)
|
|
157 |
}()
|
|
158 |
|
|
159 |
res := t.Run("maxWidthDistributor", func(t *testing.T) {
|
|
160 |
close(ready)
|
|
161 |
for v := range start {
|
|
162 |
timer := time.NewTimer(100 * time.Millisecond)
|
|
163 |
select {
|
|
164 |
case end <- v:
|
|
165 |
timer.Stop()
|
|
166 |
case <-timer.C:
|
|
167 |
t.FailNow()
|
|
168 |
}
|
|
169 |
}
|
|
170 |
})
|
|
171 |
|
|
172 |
if !res {
|
|
173 |
t.Error("maxWidthDistributor stuck in the middle")
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
| 109 | 177 |
func getLastLine(bb []byte) []byte {
|
| 110 | 178 |
split := bytes.Split(bb, []byte("\n"))
|
| 111 | 179 |
return split[len(split)-2]
|