refactoring progress test
Vladimir Bauer
4 years ago
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | func TestBarCount(t *testing.T) { |
| 19 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) | |
| 19 | shutdown := make(chan struct{}) | |
| 20 | p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard)) | |
| 20 | 21 | |
| 21 | 22 | check := make(chan struct{}) |
| 22 | 23 | b := p.AddBar(100) |
| 26 | 27 | close(check) |
| 27 | 28 | } |
| 28 | 29 | b.Increment() |
| 29 | time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2) | |
| 30 | time.Sleep(randomDuration(100 * time.Millisecond)) | |
| 30 | 31 | } |
| 31 | 32 | }() |
| 32 | 33 | |
| 33 | 34 | <-check |
| 34 | count := p.BarCount() | |
| 35 | if count != 1 { | |
| 35 | if count := p.BarCount(); count != 1 { | |
| 36 | 36 | t.Errorf("BarCount want: %q, got: %q\n", 1, count) |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | b.Abort(false) |
| 40 | p.Wait() | |
| 40 | go p.Wait() | |
| 41 | select { | |
| 42 | case <-shutdown: | |
| 43 | case <-time.After(150 * time.Millisecond): | |
| 44 | t.Error("Progress didn't shutdown") | |
| 45 | } | |
| 41 | 46 | } |
| 42 | 47 | |
| 43 | 48 | func TestBarAbort(t *testing.T) { |
| 49 | shutdown := make(chan struct{}) | |
| 50 | p := mpb.New(mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard)) | |
| 44 | 51 | n := 2 |
| 45 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) | |
| 46 | 52 | bars := make([]*mpb.Bar, n) |
| 47 | 53 | for i := 0; i < n; i++ { |
| 48 | 54 | b := p.AddBar(100) |
| 49 | 55 | switch i { |
| 50 | 56 | case n - 1: |
| 51 | 57 | var abortCalledTimes int |
| 52 | for j := 0; !b.Completed(); j++ { | |
| 53 | if j >= 33 { | |
| 58 | for j := 0; !b.Aborted(); j++ { | |
| 59 | if j >= 10 { | |
| 54 | 60 | b.Abort(true) |
| 55 | 61 | abortCalledTimes++ |
| 56 | 62 | } else { |
| 57 | 63 | b.Increment() |
| 58 | time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2) | |
| 59 | 64 | } |
| 60 | 65 | } |
| 61 | 66 | if abortCalledTimes != 1 { |
| 69 | 74 | go func() { |
| 70 | 75 | for !b.Completed() { |
| 71 | 76 | b.Increment() |
| 72 | time.Sleep((time.Duration(rand.Intn(10)+1) * (10 * time.Millisecond)) / 2) | |
| 77 | time.Sleep(randomDuration(100 * time.Millisecond)) | |
| 73 | 78 | } |
| 74 | 79 | }() |
| 75 | 80 | } |
| 77 | 82 | } |
| 78 | 83 | |
| 79 | 84 | bars[0].Abort(false) |
| 80 | p.Wait() | |
| 85 | go p.Wait() | |
| 86 | select { | |
| 87 | case <-shutdown: | |
| 88 | case <-time.After(150 * time.Millisecond): | |
| 89 | t.Error("Progress didn't shutdown") | |
| 90 | } | |
| 81 | 91 | } |
| 82 | 92 | |
| 83 | 93 | func TestWithContext(t *testing.T) { |
| 85 | 95 | ctx, cancel := context.WithCancel(context.Background()) |
| 86 | 96 | p := mpb.NewWithContext(ctx, mpb.WithShutdownNotifier(shutdown), mpb.WithOutput(ioutil.Discard)) |
| 87 | 97 | |
| 88 | start := make(chan struct{}) | |
| 89 | 98 | done := make(chan struct{}) |
| 90 | 99 | fail := make(chan struct{}) |
| 91 | 100 | bar := p.AddBar(0) // never complete bar |
| 92 | 101 | go func() { |
| 93 | close(start) | |
| 94 | for !bar.Completed() { | |
| 95 | bar.Increment() | |
| 102 | for !bar.Aborted() { | |
| 96 | 103 | time.Sleep(randomDuration(100 * time.Millisecond)) |
| 104 | cancel() | |
| 97 | 105 | } |
| 98 | 106 | close(done) |
| 99 | 107 | }() |
| 107 | 115 | } |
| 108 | 116 | }() |
| 109 | 117 | |
| 110 | <-start | |
| 111 | cancel() | |
| 112 | 118 | select { |
| 113 | 119 | case <-shutdown: |
| 114 | 120 | case <-fail: |
| 132 | 138 | end := make(chan struct{}) |
| 133 | 139 | mpb.MaxWidthDistributor = makeWrapper(mpb.MaxWidthDistributor, start, end) |
| 134 | 140 | |
| 135 | total := 80 | |
| 141 | total := 100 | |
| 136 | 142 | numBars := 6 |
| 137 | 143 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) |
| 138 | 144 | for i := 0; i < numBars; i++ { |
| 139 | 145 | bar := p.AddBar(int64(total), |
| 140 | 146 | mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0), |
| 141 | mpb.PrependDecorators( | |
| 142 | decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), | |
| 143 | ), | |
| 147 | mpb.PrependDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace)), | |
| 144 | 148 | ) |
| 145 | 149 | go func() { |
| 146 | 150 | <-ready |
| 147 | 151 | for i := 0; i < total; i++ { |
| 148 | 152 | start := time.Now() |
| 149 | if id := bar.ID(); id > 1 && i >= 42 { | |
| 153 | if id := bar.ID(); id > 1 && i >= 32 { | |
| 150 | 154 | if id&1 == 1 { |
| 151 | 155 | bar.Abort(true) |
| 152 | 156 | } else { |
| 153 | 157 | bar.Abort(false) |
| 154 | 158 | } |
| 155 | 159 | } |
| 156 | time.Sleep((time.Duration(rand.Intn(10)+1) * (50 * time.Millisecond)) / 2) | |
| 160 | time.Sleep(randomDuration(100 * time.Millisecond)) | |
| 157 | 161 | bar.IncrInt64(rand.Int63n(5) + 1) |
| 158 | 162 | bar.DecoratorEwmaUpdate(time.Since(start)) |
| 159 | 163 | } |