-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
92 lines (74 loc) · 1.78 KB
/
utils_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
package progress_test
import (
"math"
"math/rand"
"sync/atomic"
"testing"
"time"
"github.com/libfor/progress"
)
func TestWaitGroup(t *testing.T) {
t.Parallel()
wg := &progress.WaitGroup{}
wg.Add(2)
wg.Add(5)
wg.Done()
wg.Done()
if fin, tot := wg.Count(); fin != 2 || tot != 8 {
t.Fatalf(`expected fin %d = 2 and total %d = 8`, fin, tot)
}
go func() {
wg.Done()
wg.Done()
wg.Done()
wg.Done()
wg.Done()
}()
wg.Wait()
if wg.InProgress() {
t.Fatalf("expected completion because waitgroup has completed")
}
}
// TestPolling tests the polling wrapper.
// It's important to note that this test relies on the debounce mechanism
// to ensure that enough time has passed for the polling to catch new values.
func TestPolling(t *testing.T) {
t.Parallel()
totalWork := atomic.Uint64{}
currentWork := atomic.Uint64{}
totalWork.Store(20)
counter := func() (uint64, uint64) {
return currentWork.Load(), totalWork.Load()
}
tracker := progress.Extend(progress.NewReaderFromCount(counter))
if !tracker.InProgress() {
t.Fatalf("should not be already done")
}
if p := tracker.Percentage(); p != 0 {
t.Fatalf("expected 0 percentage, got %f", p)
}
currentWork.Add(10)
if !tracker.InProgress() {
t.Fatalf("should not be already done")
}
if p := tracker.Percentage(); math.Abs(p-.5) > .1 {
t.Fatalf("expected ~.5 percentage, got %f", p)
}
currentWork.Add(10)
if tracker.InProgress() {
t.Fatalf("should be done")
}
if p := tracker.Percentage(); p != 1 {
t.Fatalf("expected 1 percentage, got %f", p)
}
}
func TestLogger(t *testing.T) {
t.Parallel()
task := progress.NewLongRunningJob()
task.AddWork(50)
go progress.Logger(t.Logf, "building thing: ", task)
for task.InProgress() {
task.FinishWork(uint64(rand.Intn(10)))
time.Sleep(10 * time.Millisecond)
}
}