Codebase list golang-github-vbauerster-mpb / be85215
Passing WithContext and WithCancel tests Vladimir Bauer 9 years ago
2 changed file(s) with 97 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
55 "context"
66 "fmt"
77 "math/rand"
8 "sync"
89 "testing"
910 "time"
1011
1516 ctx, cancel := context.WithCancel(context.Background())
1617 shutdown := make(chan struct{})
1718 p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown)
19
20 var wg sync.WaitGroup
21 total := 100
1822 numBars := 3
23 wg.Add(numBars)
1924
2025 for i := 0; i < numBars; i++ {
2126 name := fmt.Sprintf("Bar#%d:", i)
22 bar := p.AddBar(100).PrependName(name, len(name), 0)
27 bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0)
2328
2429 go func() {
25 for i := 0; i < 10000; i++ {
30 defer func() {
31 // fmt.Printf("%s done\n", name)
32 wg.Done()
33 }()
34 for i := 0; i < total; i++ {
35 select {
36 case <-ctx.Done():
37 return
38 default:
39 }
40 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2641 bar.Incr(1)
27 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2842 }
2943 }()
3044 }
3145
32 cancel()
46 time.AfterFunc(300*time.Millisecond, cancel)
47
48 wg.Wait()
49 p.Stop()
3350
3451 select {
3552 case <-shutdown:
101101 cancel := make(chan struct{})
102102 shutdown := make(chan struct{})
103103 p := mpb.New().WithCancel(cancel).ShutdownNotify(shutdown)
104
105 var wg sync.WaitGroup
106 total := 100
104107 numBars := 3
108 wg.Add(numBars)
105109
106110 for i := 0; i < numBars; i++ {
107111 name := fmt.Sprintf("Bar#%d:", i)
108 bar := p.AddBar(100).PrependName(name, len(name), 0)
112 bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0)
109113
110114 go func() {
111 for i := 0; i < 10000; i++ {
115 defer func() {
116 // fmt.Printf("%s done\n", name)
117 wg.Done()
118 }()
119 for i := 0; i < total; i++ {
120 select {
121 case <-cancel:
122 return
123 default:
124 }
125 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
112126 bar.Incr(1)
113 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
114127 }
115128 }()
116129 }
117130
118 close(cancel)
131 time.AfterFunc(300*time.Millisecond, func() {
132 close(cancel)
133 })
134
135 wg.Wait()
136 p.Stop()
119137
120138 select {
121139 case <-shutdown:
123141 t.Error("ProgressBar didn't stop")
124142 }
125143 }
144
145 func TestWithNilCancel(t *testing.T) {
146 defer func() {
147 if p := recover(); p != nil {
148 if msg, ok := p.(string); ok && msg == "nil cancel channel" {
149 return
150 }
151 t.Errorf("Expected nil channel panic, got: %+v", p)
152 }
153 }()
154 _ = mpb.New().WithCancel(nil)
155 }
156
157 func TestFormat(t *testing.T) {
158 var buf bytes.Buffer
159 cancel := make(chan struct{})
160 shutdown := make(chan struct{})
161 customFormat := "╢▌▌░╟"
162 p := mpb.New().Format(customFormat)
163 p.WithCancel(cancel)
164 p.ShutdownNotify(shutdown)
165 p.SetOut(&buf)
166 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
167
168 go func() {
169 for i := 0; i < 100; i++ {
170 bar.Incr(1)
171 time.Sleep(10 * time.Millisecond)
172 if i == 42 {
173 close(cancel)
174 }
175 }
176 }()
177
178 // p.Stop()
179 // time.Sleep(300 * time.Millisecond)
180
181 // gotBar := strings.TrimSpace(buf.String())
182 gotBar := buf.String()
183 seen := make(map[rune]bool)
184 for _, r := range gotBar {
185 if !seen[r] {
186 seen[r] = true
187 }
188 }
189 fmt.Println(gotBar)
190 for r, _ := range seen {
191 fmt.Printf("%#U\n", r)
192 }
193 // expectBar := "╢▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌╟"
194 // if gotBar != expectBar {
195 // t.Errorf("Expected for")
196 // }
197 }