forked from schollz/progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressbar_test.go
377 lines (326 loc) · 8.31 KB
/
progressbar_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package progressbar
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func BenchmarkRender(b *testing.B) {
bar := NewOptions64(100000000,
OptionSetWriter(os.Stderr),
OptionShowIts(),
)
for i := 0; i < b.N; i++ {
bar.Add(1)
}
}
func ExampleProgressBar() {
bar := New(100)
bar.Add(10)
// Output:
// 10% |████ | [0s:0s]
}
func ExampleProgressBar_Set() {
bar := New(100)
bar.Set(10)
// Output:
// 10% |████ | [0s:0s]
}
func ExampleProgressBar_Set64() {
bar := New(100)
bar.Set64(10)
// Output:
// 10% |████ | [0s:0s]
}
func ExampleProgressBar_basic() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█ | [1s:9s]
}
func ExampleOptionThrottle() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionThrottle(100*time.Millisecond))
bar.Reset()
bar.Add(5)
time.Sleep(150 * time.Millisecond)
bar.Add(5)
bar.Add(10)
// Output:
// 10% |█ | [0s:1s]
}
func ExampleOptionClearOnFinish() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionClearOnFinish())
bar.Reset()
bar.Finish()
fmt.Println("Finished")
// Output:
// Finished
}
func ExampleProgressBar_Finish() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Finish()
// Output:
// 100% |██████████| [0s:0s]
}
func Example_xOutOfY() {
bar := NewOptions(100, OptionSetPredictTime(true))
for i := 0; i < 100; i++ {
bar.Add(1)
time.Sleep(1 * time.Millisecond)
}
}
func ExampleOptionShowIts_count() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionShowCount())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█ | (10/100, 10 it/s) [1s:9s]
}
func ExampleOptionShowIts() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█ | (10 it/s) [1s:9s]
}
func ExampleOptionShowItsSlow() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts())
bar.Reset()
time.Sleep(4 * time.Second)
bar.Add(1)
// Output:
// 1% | | (15 it/min) [4s:6m36s]
}
func ExampleOptionSetPredictTime() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
_ = bar.Add(10)
// Output:
// 10% |█ | [10:100]
}
func ExampleIgnoreLength_WithIteration() {
/*
IgnoreLength test with iteration count and iteration rate
*/
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowIts(),
OptionShowCount(),
)
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)
// Output:
// 50% | █ | (5/-, 5 it/s)
}
func ExampleIgnoreLength_WithSpeed() {
/*
IgnoreLength test with iterations and count
*/
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowBytes(true),
)
bar.Reset()
time.Sleep(1 * time.Second)
// since 10 is the width and we don't know the max bytes
// it will do a infinite scrolling.
bar.Add(11)
// Output:
// 10% |█ | (0.011 kB/s)
}
func TestBar(t *testing.T) {
bar := New(0)
if err := bar.Add(1); err == nil {
t.Error("should have an error for 0 bar")
}
bar = New(10)
if err := bar.Add(11); err == nil {
t.Error("should have an error for adding > bar")
}
}
func TestState(t *testing.T) {
bar := NewOptions(100, OptionSetWidth(10))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
s := bar.State()
if s.CurrentPercent != 0.1 {
t.Error(s)
}
}
func ExampleOptionSetRenderBlankState() {
NewOptions(10, OptionSetWidth(10), OptionSetRenderBlankState(true))
// Output:
// 0% | | [0s:0s]
}
func TestBasicSets(t *testing.T) {
b := NewOptions(
999,
OptionSetWidth(888),
OptionSetRenderBlankState(true),
OptionSetWriter(ioutil.Discard), // suppressing output for this test
)
tc := b.config
if tc.max != 999 {
t.Errorf("Expected %s to be %d, instead I got %d\n%+v", "max", 999, tc.max, b)
}
if tc.width != 888 {
t.Errorf("Expected %s to be %d, instead I got %d\n%+v", "width", 999, tc.max, b)
}
if !tc.renderWithBlankState {
t.Errorf("Expected %s to be %t, instead I got %t\n%+v", "renderWithBlankState", true, tc.renderWithBlankState, b)
}
}
func TestOptionSetTheme(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetTheme(Theme{Saucer: "#", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
OptionSetWidth(10),
OptionSetWriter(&buf),
)
bar.Add(5)
result := strings.TrimSpace(buf.String())
expect := "50% >#####-----< [0s:0s]"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}
// TestOptionSetPredictTime ensures that when predict time is turned off, the progress
// bar is showing the total steps completed of the given max, otherwise the predicted
// time in seconds is specified.
func TestOptionSetPredictTime(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetPredictTime(false),
OptionSetWidth(10),
OptionSetWriter(&buf),
)
_ = bar.Add(2)
result := strings.TrimSpace(buf.String())
expect := "20% |██ | [2:10]"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
bar.Reset()
bar.config.predictTime = true
buf.Reset()
_ = bar.Add(7)
result = strings.TrimSpace(buf.String())
expect = "70% |███████ | [0s:0s]"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}
func TestIgnoreLength(t *testing.T) {
bar := NewOptions(
-1,
OptionSetWidth(100),
)
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
state := bar.State()
if state.CurrentBytes != 10.0 {
t.Errorf("Number of bytes mismatched gotBytes %f wantBytes %f", state.CurrentBytes, 10.0)
}
if state.CurrentPercent != 0.1 {
t.Errorf("Percent of bar mismatched got %f want %f", state.CurrentPercent, 0.1)
}
kbPerSec := fmt.Sprintf("%2.2f", state.KBsPerSecond)
if kbPerSec != "0.01" {
t.Errorf("Speed mismatched got %s want %s", kbPerSec, "0.01")
}
}
func TestReaderToBuffer(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
urlToGet := "https://dl.google.com/go/go1.14.1.src.tar.gz"
req, err := http.NewRequest("GET", urlToGet, nil)
assert.Nil(t, err)
resp, err := http.DefaultClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close()
buf := new(bytes.Buffer)
bar := NewOptions(int(resp.ContentLength), OptionShowBytes(true))
out := io.MultiWriter(buf, bar)
_, err = io.Copy(out, resp.Body)
assert.Nil(t, err)
md5, err := md5sum(buf)
assert.Nil(t, err)
assert.Equal(t, "d441819a800f8c90825355dfbede7266", md5)
}
func TestReaderToFile(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
urlToGet := "https://dl.google.com/go/go1.14.1.src.tar.gz"
req, err := http.NewRequest("GET", urlToGet, nil)
assert.Nil(t, err)
resp, err := http.DefaultClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close()
f, err := ioutil.TempFile("", "progressbar_testfile")
if err != nil {
t.Fatal()
}
defer os.Remove(f.Name())
defer f.Close()
bar := NewOptions(int(resp.ContentLength), OptionShowBytes(true))
out := io.MultiWriter(f, bar)
_, err = io.Copy(out, resp.Body)
assert.Nil(t, err)
f.Sync()
f.Seek(0, 0)
md5, err := md5sum(f)
assert.Nil(t, err)
assert.Equal(t, "d441819a800f8c90825355dfbede7266", md5)
}
func TestConcurrency(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
1000,
OptionSetWriter(&buf),
)
var wg sync.WaitGroup
for i := 0; i < 900; i++ {
wg.Add(1)
go func(b *ProgressBar, wg *sync.WaitGroup) {
bar.Add(1)
wg.Done()
}(bar, &wg)
}
wg.Wait()
result := bar.state.currentNum
expect := int64(900)
assert.Equal(t, expect, result)
}
func md5sum(r io.Reader) (string, error) {
hash := md5.New()
_, err := io.Copy(hash, r)
return hex.EncodeToString(hash.Sum(nil)), err
}
func ExampleProgressBar_Describe() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Describe("performing axial adjustements")
bar.Add(10)
// Output:
// performing axial adjustements 10% |█ | [1s:9s]
}