Codebase list golang-github-vbauerster-mpb / d4b9514
TestWithContext with +build go1.7 flag Vladimir Bauer 9 years ago
2 changed file(s) with 39 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
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 }
11
22 import (
33 "bytes"
4 "context"
54 "fmt"
65 "math/rand"
76 "strings"
9897 p.Stop()
9998 }
10099
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
128100 func TestWithCancel(t *testing.T) {
129101 cancel := make(chan struct{})
130102 shutdown := make(chan struct{})