| 45 | 45 |
}
|
| 46 | 46 |
}
|
| 47 | 47 |
|
| 48 | |
func TestBarInProgress(t *testing.T) {
|
| 49 | |
var buf bytes.Buffer
|
| 50 | |
cancel := make(chan struct{})
|
|
48 |
func TestBarCompleted(t *testing.T) {
|
| 51 | 49 |
p := mpb.New(
|
| 52 | |
mpb.Output(&buf),
|
| 53 | |
mpb.WithCancel(cancel),
|
|
50 |
mpb.Output(ioutil.Discard),
|
| 54 | 51 |
)
|
| 55 | |
bar := p.AddBar(100, mpb.BarTrim())
|
|
52 |
total := 80
|
|
53 |
bar := p.AddBar(int64(total))
|
| 56 | 54 |
|
| 57 | |
stopped := make(chan struct{})
|
|
55 |
var count int
|
|
56 |
for !bar.Completed() {
|
|
57 |
time.Sleep(10 * time.Millisecond)
|
|
58 |
bar.Increment()
|
|
59 |
count++
|
|
60 |
}
|
| 58 | 61 |
|
| 59 | |
go func() {
|
| 60 | |
defer close(stopped)
|
| 61 | |
for bar.InProgress() {
|
| 62 | |
time.Sleep(10 * time.Millisecond)
|
| 63 | |
bar.Increment()
|
| 64 | |
}
|
| 65 | |
}()
|
| 66 | |
|
| 67 | |
time.Sleep(250 * time.Millisecond)
|
| 68 | |
close(cancel)
|
| 69 | 62 |
p.Stop()
|
| 70 | |
|
| 71 | |
select {
|
| 72 | |
case <-stopped:
|
| 73 | |
case <-time.After(300 * time.Millisecond):
|
| 74 | |
t.Error("bar.InProgress returns true after cancel")
|
|
63 |
if count != total {
|
|
64 |
t.Errorf("got count: %d, expected %d\n", count, total)
|
| 75 | 65 |
}
|
| 76 | 66 |
}
|
| 77 | 67 |
|
| 78 | |
func TestBarGetID(t *testing.T) {
|
| 79 | |
var wg sync.WaitGroup
|
| 80 | |
p := mpb.New(
|
| 81 | |
mpb.Output(ioutil.Discard),
|
| 82 | |
mpb.WithWaitGroup(&wg),
|
| 83 | |
)
|
|
68 |
func TestBarID(t *testing.T) {
|
|
69 |
p := mpb.New(mpb.Output(ioutil.Discard))
|
| 84 | 70 |
|
| 85 | 71 |
numBars := 3
|
| 86 | |
wg.Add(numBars)
|
| 87 | |
|
| 88 | 72 |
bars := make([]*mpb.Bar, numBars)
|
| 89 | 73 |
for i := 0; i < numBars; i++ {
|
| 90 | |
bars[i] = p.AddBar(100, mpb.BarID(i))
|
| 91 | |
|
|
74 |
bars[i] = p.AddBar(80, mpb.BarID(i))
|
| 92 | 75 |
go func(bar *mpb.Bar) {
|
| 93 | |
defer wg.Done()
|
| 94 | |
for i := 0; i < 100; i++ {
|
|
76 |
for i := 0; i < 80; i++ {
|
| 95 | 77 |
time.Sleep(10 * time.Millisecond)
|
| 96 | 78 |
bar.Increment()
|
| 97 | 79 |
}
|
| 98 | 80 |
}(bars[i])
|
| 99 | 81 |
}
|
| 100 | 82 |
|
|
83 |
p.Stop()
|
| 101 | 84 |
for wantID, bar := range bars {
|
| 102 | 85 |
gotID := bar.ID()
|
| 103 | 86 |
if gotID != wantID {
|
| 104 | 87 |
t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
|
| 105 | 88 |
}
|
| 106 | 89 |
}
|
| 107 | |
|
| 108 | |
p.Stop()
|
| 109 | 90 |
}
|
| 110 | 91 |
|
| 111 | 92 |
func TestBarIncrWithReFill(t *testing.T) {
|
|
| 127 | 108 |
|
| 128 | 109 |
for i := 0; i < total; i++ {
|
| 129 | 110 |
bar.Increment()
|
|
111 |
time.Sleep(10 * time.Millisecond)
|
| 130 | 112 |
}
|
| 131 | 113 |
|
| 132 | 114 |
p.Stop()
|