| 4 | 4 |
"fmt"
|
| 5 | 5 |
"io/ioutil"
|
| 6 | 6 |
"math/rand"
|
| 7 | |
"strings"
|
| 8 | 7 |
"sync"
|
| 9 | 8 |
"testing"
|
| 10 | 9 |
"time"
|
| 11 | 10 |
|
| 12 | |
"github.com/vbauerster/mpb"
|
|
11 |
. "github.com/vbauerster/mpb"
|
|
12 |
"github.com/vbauerster/mpb/cwriter"
|
| 13 | 13 |
)
|
| 14 | 14 |
|
| 15 | 15 |
func init() {
|
|
| 17 | 17 |
}
|
| 18 | 18 |
|
| 19 | 19 |
func TestBarCount(t *testing.T) {
|
| 20 | |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
|
20 |
p := New(WithOutput(ioutil.Discard))
|
| 21 | 21 |
|
| 22 | 22 |
var wg sync.WaitGroup
|
| 23 | 23 |
wg.Add(1)
|
|
| 43 | 43 |
}
|
| 44 | 44 |
|
| 45 | 45 |
func TestBarAbort(t *testing.T) {
|
| 46 | |
p := mpb.New(mpb.WithOutput(ioutil.Discard))
|
|
46 |
p := New(WithOutput(ioutil.Discard))
|
| 47 | 47 |
|
| 48 | 48 |
var wg sync.WaitGroup
|
| 49 | 49 |
wg.Add(1)
|
| 50 | |
bars := make([]*mpb.Bar, 3)
|
|
50 |
bars := make([]*Bar, 3)
|
| 51 | 51 |
for i := 0; i < 3; i++ {
|
| 52 | 52 |
b := p.AddBar(100)
|
| 53 | 53 |
bars[i] = b
|
|
| 76 | 76 |
func TestWithCancel(t *testing.T) {
|
| 77 | 77 |
cancel := make(chan struct{})
|
| 78 | 78 |
shutdown := make(chan struct{})
|
| 79 | |
p := mpb.New(
|
| 80 | |
mpb.WithOutput(ioutil.Discard),
|
| 81 | |
mpb.WithCancel(cancel),
|
| 82 | |
mpb.WithShutdownNotifier(shutdown),
|
|
79 |
p := New(
|
|
80 |
WithOutput(ioutil.Discard),
|
|
81 |
WithCancel(cancel),
|
|
82 |
WithShutdownNotifier(shutdown),
|
| 83 | 83 |
)
|
| 84 | 84 |
|
| 85 | 85 |
numBars := 3
|
| 86 | |
bars := make([]*mpb.Bar, 0, numBars)
|
|
86 |
bars := make([]*Bar, 0, numBars)
|
| 87 | 87 |
for i := 0; i < numBars; i++ {
|
| 88 | |
bar := p.AddBar(int64(1000), mpb.BarID(i))
|
|
88 |
bar := p.AddBar(int64(1000), BarID(i))
|
| 89 | 89 |
bars = append(bars, bar)
|
| 90 | 90 |
go func() {
|
| 91 | 91 |
for !bar.Completed() {
|
| 92 | |
time.Sleep(randomDuration(40 * time.Millisecond))
|
|
92 |
time.Sleep(randomDuration(100 * time.Millisecond))
|
| 93 | 93 |
bar.Increment()
|
| 94 | 94 |
}
|
| 95 | 95 |
}()
|
|
| 100 | 100 |
})
|
| 101 | 101 |
|
| 102 | 102 |
p.Wait()
|
| 103 | |
for _, bar := range bars {
|
| 104 | |
if bar.Current() >= bar.Total() {
|
| 105 | |
t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current())
|
| 106 | |
}
|
| 107 | |
}
|
|
103 |
|
| 108 | 104 |
select {
|
| 109 | 105 |
case <-shutdown:
|
| 110 | |
case <-time.After(100 * time.Millisecond):
|
| 111 | |
t.Error("Progress didn't stop")
|
|
106 |
case <-time.After(200 * time.Millisecond):
|
|
107 |
t.FailNow()
|
| 112 | 108 |
}
|
| 113 | 109 |
}
|
| 114 | 110 |
|
| 115 | |
func TestCustomFormat(t *testing.T) {
|
|
111 |
var (
|
|
112 |
cursorUp = fmt.Sprintf("%c[%dA", cwriter.ESC, 1)
|
|
113 |
clearLine = fmt.Sprintf("%c[2K\r", cwriter.ESC)
|
|
114 |
clearCursorAndLine = cursorUp + clearLine
|
|
115 |
)
|
|
116 |
|
|
117 |
func TestWithFormat(t *testing.T) {
|
| 116 | 118 |
var buf bytes.Buffer
|
| 117 | |
cancel := make(chan struct{})
|
| 118 | 119 |
customFormat := "╢▌▌░╟"
|
| 119 | |
p := mpb.New(
|
| 120 | |
mpb.WithOutput(&buf),
|
| 121 | |
mpb.WithCancel(cancel),
|
| 122 | |
mpb.WithFormat(customFormat),
|
| 123 | |
)
|
| 124 | |
bar := p.AddBar(80, mpb.BarTrim())
|
| 125 | |
|
| 126 | |
var wg sync.WaitGroup
|
| 127 | |
wg.Add(1)
|
| 128 | |
go func() {
|
| 129 | |
for i := 0; i < 80; i++ {
|
| 130 | |
if i == 33 {
|
| 131 | |
wg.Done()
|
| 132 | |
}
|
| 133 | |
time.Sleep(randomDuration(80 * time.Millisecond))
|
| 134 | |
bar.Increment()
|
| 135 | |
}
|
| 136 | |
}()
|
| 137 | |
|
| 138 | |
wg.Wait()
|
| 139 | |
close(cancel)
|
| 140 | |
p.Wait()
|
| 141 | |
|
| 142 | |
for _, r := range customFormat {
|
| 143 | |
if !bytes.ContainsRune(buf.Bytes(), r) {
|
| 144 | |
t.Errorf("Rune %#U not found in bar\n", r)
|
| 145 | |
}
|
| 146 | |
}
|
| 147 | |
}
|
| 148 | |
|
| 149 | |
func TestInvalidFormatWidth(t *testing.T) {
|
| 150 | |
var buf bytes.Buffer
|
| 151 | |
customWidth := 60
|
| 152 | |
customFormat := "(#>=_)"
|
| 153 | |
p := mpb.New(
|
| 154 | |
mpb.WithOutput(&buf),
|
| 155 | |
mpb.WithWidth(customWidth),
|
| 156 | |
mpb.WithFormat(customFormat),
|
| 157 | |
)
|
| 158 | |
bar := p.AddBar(100, mpb.BarTrim())
|
|
120 |
p := New(WithOutput(&buf), WithFormat(customFormat))
|
|
121 |
bar := p.AddBar(100, BarTrim())
|
| 159 | 122 |
|
| 160 | 123 |
for i := 0; i < 100; i++ {
|
| 161 | |
time.Sleep(randomDuration(40 * time.Millisecond))
|
|
124 |
if i == 33 {
|
|
125 |
p.Abort(bar)
|
|
126 |
break
|
|
127 |
}
|
|
128 |
time.Sleep(randomDuration(100 * time.Millisecond))
|
| 162 | 129 |
bar.Increment()
|
| 163 | 130 |
}
|
| 164 | 131 |
|
| 165 | 132 |
p.Wait()
|
| 166 | 133 |
|
| 167 | |
got := buf.String()
|
| 168 | |
want := fmt.Sprintf("[%s]", strings.Repeat("=", customWidth-2))
|
| 169 | |
if !strings.Contains(got, want) {
|
| 170 | |
t.Errorf("Expected format: %s, got %s\n", want, got)
|
|
134 |
bb := bytes.Split(buf.Bytes(), []byte("\n"))
|
|
135 |
lastLine := bb[len(bb)-2]
|
|
136 |
lastLine = lastLine[len(clearCursorAndLine):]
|
|
137 |
|
|
138 |
for _, r := range customFormat {
|
|
139 |
if !bytes.ContainsRune(lastLine, r) {
|
|
140 |
t.Errorf("Rune %#U not found in bar\n", r)
|
|
141 |
}
|
| 171 | 142 |
}
|
| 172 | 143 |
}
|
| 173 | 144 |
|