refactoring: progress test
Vladimir Bauer
4 years ago
| 4 | 4 | "context" |
| 5 | 5 | "io/ioutil" |
| 6 | 6 | "math/rand" |
| 7 | "sync" | |
| 8 | 7 | "testing" |
| 9 | 8 | "time" |
| 10 | 9 | |
| 19 | 18 | func TestBarCount(t *testing.T) { |
| 20 | 19 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) |
| 21 | 20 | |
| 22 | var wg sync.WaitGroup | |
| 23 | wg.Add(1) | |
| 21 | check := make(chan struct{}) | |
| 24 | 22 | b := p.AddBar(100) |
| 25 | 23 | go func() { |
| 26 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| 27 | 24 | for i := 0; i < 100; i++ { |
| 28 | if i == 33 { | |
| 29 | wg.Done() | |
| 25 | if i == 10 { | |
| 26 | close(check) | |
| 30 | 27 | } |
| 31 | 28 | 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) | |
| 33 | 30 | } |
| 34 | 31 | }() |
| 35 | 32 | |
| 36 | wg.Wait() | |
| 33 | <-check | |
| 37 | 34 | count := p.BarCount() |
| 38 | 35 | if count != 1 { |
| 39 | 36 | t.Errorf("BarCount want: %q, got: %q\n", 1, count) |
| 40 | 37 | } |
| 41 | 38 | |
| 42 | b.Abort(true) | |
| 39 | b.Abort(false) | |
| 43 | 40 | p.Wait() |
| 44 | 41 | } |
| 45 | 42 | |
| 46 | 43 | func TestBarAbort(t *testing.T) { |
| 44 | n := 2 | |
| 47 | 45 | 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++ { | |
| 53 | 48 | 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 { | |
| 58 | 54 | 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) | |
| 60 | 59 | } |
| 61 | b.Increment() | |
| 62 | time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2) | |
| 63 | 60 | } |
| 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 | } | |
| 65 | 76 | bars[i] = b |
| 66 | 77 | } |
| 67 | 78 | |
| 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) | |
| 75 | 80 | p.Wait() |
| 76 | 81 | } |
| 77 | 82 | |
| 139 | 144 | ) |
| 140 | 145 | go func() { |
| 141 | 146 | <-ready |
| 142 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| 143 | 147 | for i := 0; i < total; i++ { |
| 144 | 148 | start := time.Now() |
| 145 | 149 | if id := bar.ID(); id > 1 && i >= 42 { |
| 149 | 153 | bar.Abort(false) |
| 150 | 154 | } |
| 151 | 155 | } |
| 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) | |
| 153 | 157 | bar.IncrInt64(rand.Int63n(5) + 1) |
| 154 | 158 | bar.DecoratorEwmaUpdate(time.Since(start)) |
| 155 | 159 | } |