TestWithContext with +build go1.7 flag
Vladimir Bauer
9 years ago
|
0 |
//+build go1.7
|
|
1 |
package mpb_test
|
|
2 |
|
|
3 |
import (
|
|
4 |
"context"
|
|
5 |
"fmt"
|
|
6 |
"math/rand"
|
|
7 |
"testing"
|
|
8 |
"time"
|
|
9 |
|
|
10 |
"github.com/vbauerster/mpb"
|
|
11 |
)
|
|
12 |
|
|
13 |
func TestWithContext(t *testing.T) {
|
|
14 |
ctx, cancel := context.WithCancel(context.Background())
|
|
15 |
shutdown := make(chan struct{})
|
|
16 |
p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown)
|
|
17 |
numBars := 3
|
|
18 |
|
|
19 |
for i := 0; i < numBars; i++ {
|
|
20 |
name := fmt.Sprintf("Bar#%d:", i)
|
|
21 |
bar := p.AddBar(100).PrependName(name, len(name), 0)
|
|
22 |
|
|
23 |
go func() {
|
|
24 |
for i := 0; i < 10000; i++ {
|
|
25 |
bar.Incr(1)
|
|
26 |
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
|
27 |
}
|
|
28 |
}()
|
|
29 |
}
|
|
30 |
|
|
31 |
cancel()
|
|
32 |
|
|
33 |
select {
|
|
34 |
case <-shutdown:
|
|
35 |
case <-time.After(500 * time.Millisecond):
|
|
36 |
t.Error("ProgressBar didn't stop")
|
|
37 |
}
|
|
38 |
}
|
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | 3 |
"bytes"
|
| 4 | |
"context"
|
| 5 | 4 |
"fmt"
|
| 6 | 5 |
"math/rand"
|
| 7 | 6 |
"strings"
|
|
| 98 | 97 |
p.Stop()
|
| 99 | 98 |
}
|
| 100 | 99 |
|
| 101 | |
func TestWithContext(t *testing.T) {
|
| 102 | |
ctx, cancel := context.WithCancel(context.Background())
|
| 103 | |
shutdown := make(chan struct{})
|
| 104 | |
p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown)
|
| 105 | |
numBars := 3
|
| 106 | |
|
| 107 | |
for i := 0; i < numBars; i++ {
|
| 108 | |
name := fmt.Sprintf("Bar#%d:", i)
|
| 109 | |
bar := p.AddBar(100).PrependName(name, len(name), 0)
|
| 110 | |
|
| 111 | |
go func() {
|
| 112 | |
for i := 0; i < 10000; i++ {
|
| 113 | |
bar.Incr(1)
|
| 114 | |
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
| 115 | |
}
|
| 116 | |
}()
|
| 117 | |
}
|
| 118 | |
|
| 119 | |
cancel()
|
| 120 | |
|
| 121 | |
select {
|
| 122 | |
case <-shutdown:
|
| 123 | |
case <-time.After(500 * time.Millisecond):
|
| 124 | |
t.Error("ProgressBar didn't stop")
|
| 125 | |
}
|
| 126 | |
}
|
| 127 | |
|
| 128 | 100 |
func TestWithCancel(t *testing.T) {
|
| 129 | 101 |
cancel := make(chan struct{})
|
| 130 | 102 |
shutdown := make(chan struct{})
|