Codebase list golang-github-vbauerster-mpb / c24a296
refactoring: progress test Vladimir Bauer 4 years ago
1 changed file(s) with 35 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
44 "context"
55 "io/ioutil"
66 "math/rand"
7 "sync"
87 "testing"
98 "time"
109
1918 func TestBarCount(t *testing.T) {
2019 p := mpb.New(mpb.WithOutput(ioutil.Discard))
2120
22 var wg sync.WaitGroup
23 wg.Add(1)
21 check := make(chan struct{})
2422 b := p.AddBar(100)
2523 go func() {
26 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
2724 for i := 0; i < 100; i++ {
28 if i == 33 {
29 wg.Done()
25 if i == 10 {
26 close(check)
3027 }
3128 b.Increment()
32 time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
29 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
3330 }
3431 }()
3532
36 wg.Wait()
33 <-check
3734 count := p.BarCount()
3835 if count != 1 {
3936 t.Errorf("BarCount want: %q, got: %q\n", 1, count)
4037 }
4138
42 b.Abort(true)
39 b.Abort(false)
4340 p.Wait()
4441 }
4542
4643 func TestBarAbort(t *testing.T) {
44 n := 2
4745 p := mpb.New(mpb.WithOutput(ioutil.Discard))
48
49 var wg sync.WaitGroup
50 wg.Add(1)
51 bars := make([]*mpb.Bar, 3)
52 for i := 0; i < 3; i++ {
46 bars := make([]*mpb.Bar, n)
47 for i := 0; i < n; i++ {
5348 b := p.AddBar(100)
54 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
55 go func(n int) {
56 for i := 0; !b.Completed(); i++ {
57 if n == 0 && i >= 33 {
49 switch i {
50 case n - 1:
51 var abortCalledTimes int
52 for j := 0; !b.Completed(); j++ {
53 if j >= 33 {
5854 b.Abort(true)
59 wg.Done()
55 abortCalledTimes++
56 } else {
57 b.Increment()
58 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
6059 }
61 b.Increment()
62 time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2)
6360 }
64 }(i)
61 if abortCalledTimes != 1 {
62 t.Errorf("Expected abortCalledTimes: %d, got: %d\n", 1, abortCalledTimes)
63 }
64 count := p.BarCount()
65 if count != 1 {
66 t.Errorf("BarCount want: %d, got: %d\n", 1, count)
67 }
68 default:
69 go func() {
70 for !b.Completed() {
71 b.Increment()
72 time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2)
73 }
74 }()
75 }
6576 bars[i] = b
6677 }
6778
68 wg.Wait()
69 count := p.BarCount()
70 if count != 2 {
71 t.Errorf("BarCount want: %d, got: %d\n", 2, count)
72 }
73 bars[1].Abort(true)
74 bars[2].Abort(true)
79 bars[0].Abort(false)
7580 p.Wait()
7681 }
7782
139144 )
140145 go func() {
141146 <-ready
142 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
143147 for i := 0; i < total; i++ {
144148 start := time.Now()
145149 if id := bar.ID(); id > 1 && i >= 42 {
149153 bar.Abort(false)
150154 }
151155 }
152 time.Sleep((time.Duration(rng.Intn(10)+1) * (50 * time.Millisecond)) / 2)
156 time.Sleep((time.Duration(rand.Intn(10)+1) * (50 * time.Millisecond)) / 2)
153157 bar.IncrInt64(rand.Int63n(5) + 1)
154158 bar.DecoratorEwmaUpdate(time.Since(start))
155159 }