Codebase list golang-github-vbauerster-mpb / 35465c8
drop WithCancel Vladimir Bauer 7 years ago
7 changed file(s) with 41 addition(s) and 96 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "bytes"
4 "context"
45 "fmt"
56 "io"
67 "io/ioutil"
7576 )
7677
7778 func newBar(
79 ctx context.Context,
7880 wg *sync.WaitGroup,
7981 filler Filler,
8082 id, width int,
8183 total int64,
82 cancel <-chan struct{},
8384 options ...BarOption,
8485 ) *Bar {
8586 if total <= 0 {
123124 b.priority = b.runningBar.priority
124125 }
125126
126 go b.serve(wg, s, cancel)
127 go b.serve(ctx, wg, s)
127128 return b
128129 }
129130
248249 }
249250 }
250251
251 func (b *Bar) serve(wg *sync.WaitGroup, s *bState, cancel <-chan struct{}) {
252 func (b *Bar) serve(ctx context.Context, wg *sync.WaitGroup, s *bState) {
252253 defer wg.Done()
254 cancel := ctx.Done()
253255 for {
254256 select {
255257 case op := <-b.operateState:
0 //+build go1.7
1
20 package main
31
42 import (
00 package mpb
11
22 import (
3 "context"
34 "io"
45 "sync"
56 "time"
89 )
910
1011 // ProgressOption is a function option which changes the default behavior of
11 // progress pool, if passed to mpb.New(...ProgressOption)
12 // progress pool, if passed to mpb.New(...ProgressOption).
1213 type ProgressOption func(*pState)
1314
1415 // WithWaitGroup provides means to have a single joint point.
2122 }
2223 }
2324
24 // WithWidth overrides default width 80
25 // WithWidth overrides default width 80.
2526 func WithWidth(w int) ProgressOption {
2627 return func(s *pState) {
2728 if w >= 0 {
3031 }
3132 }
3233
33 // WithRefreshRate overrides default 120ms refresh rate
34 // WithRefreshRate overrides default 120ms refresh rate.
3435 func WithRefreshRate(d time.Duration) ProgressOption {
3536 return func(s *pState) {
3637 if d < 10*time.Millisecond {
4849 }
4950 }
5051
51 // WithCancel provide your cancel channel,
52 // which you plan to close at some point.
53 func WithCancel(ch <-chan struct{}) ProgressOption {
52 // WithContext provided context will be used for cancellation purposes.
53 func WithContext(ctx context.Context) ProgressOption {
5454 return func(s *pState) {
55 s.cancel = ch
55 if ctx == nil {
56 return
57 }
58 s.ctx = ctx
5659 }
5760 }
5861
6366 }
6467 }
6568
66 // WithOutput overrides default output os.Stdout
69 // WithOutput overrides default output os.Stdout.
6770 func WithOutput(w io.Writer) ProgressOption {
6871 return func(s *pState) {
6972 if w == nil {
+0
-15
options_go1.7.go less more
0 //+build go1.7
1
2 package mpb
3
4 import "context"
5
6 // WithContext provided context will be used for cancellation purposes
7 func WithContext(ctx context.Context) ProgressOption {
8 return func(s *pState) {
9 if ctx == nil {
10 panic("ctx must not be nil")
11 }
12 s.cancel = ctx.Done()
13 }
14 }
11
22 import (
33 "container/heap"
4 "context"
45 "fmt"
56 "io"
67 "io/ioutil"
3940 pMatrix map[int][]chan int
4041 aMatrix map[int][]chan int
4142
42 // following are provided by user
43 // following are provided/overrided by user
44 ctx context.Context
4345 uwg *sync.WaitGroup
4446 manualRefreshCh <-chan time.Time
45 cancel <-chan struct{}
4647 shutdownNotifier chan struct{}
4748 waitBars map[*Bar]*Bar
4849 debugOut io.Writer
5455 pq := make(priorityQueue, 0)
5556 heap.Init(&pq)
5657 s := &pState{
58 ctx: context.Background(),
5759 bHeap: &pq,
5860 width: pwidth,
5961 cw: cwriter.New(os.Stdout),
100102 result := make(chan *Bar)
101103 select {
102104 case p.operateState <- func(s *pState) {
103 b := newBar(p.wg, filler, s.idCounter, s.width, total, s.cancel, options...)
105 b := newBar(s.ctx, p.wg, filler, s.idCounter, s.width, total, options...)
104106 if b.runningBar != nil {
105107 s.waitBars[b.runningBar] = b
106108 } else {
+0
-50
progress_go1.7_test.go less more
0 //+build go1.7
1
2 package mpb_test
3
4 import (
5 "context"
6 "io/ioutil"
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(
17 mpb.WithOutput(ioutil.Discard),
18 mpb.WithContext(ctx),
19 mpb.WithShutdownNotifier(shutdown),
20 )
21
22 total := 1000
23 numBars := 3
24 bars := make([]*mpb.Bar, 0, numBars)
25 for i := 0; i < numBars; i++ {
26 bar := p.AddBar(int64(total))
27 bars = append(bars, bar)
28 go func() {
29 for !bar.Completed() {
30 time.Sleep(randomDuration(40 * time.Millisecond))
31 bar.Increment()
32 }
33 }()
34 }
35
36 time.AfterFunc(100*time.Millisecond, cancel)
37
38 p.Wait()
39 for _, bar := range bars {
40 if bar.Current() >= int64(total) {
41 t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), total, bar.Current())
42 }
43 }
44 select {
45 case <-shutdown:
46 case <-time.After(100 * time.Millisecond):
47 t.Error("Progress didn't stop")
48 }
49 }
11
22 import (
33 "bytes"
4 "context"
45 "fmt"
56 "io/ioutil"
67 "math/rand"
89 "testing"
910 "time"
1011
12 "github.com/vbauerster/mpb"
1113 . "github.com/vbauerster/mpb"
1214 "github.com/vbauerster/mpb/cwriter"
1315 )
7981 p.Wait()
8082 }
8183
82 func TestWithCancel(t *testing.T) {
83 cancel := make(chan struct{})
84 func TestWithContext(t *testing.T) {
85 ctx, cancel := context.WithCancel(context.Background())
8486 shutdown := make(chan struct{})
85 p := New(
86 WithOutput(ioutil.Discard),
87 WithCancel(cancel),
88 WithShutdownNotifier(shutdown),
87 p := mpb.New(
88 mpb.WithOutput(ioutil.Discard),
89 mpb.WithContext(ctx),
90 mpb.WithRefreshRate(50*time.Millisecond),
91 mpb.WithShutdownNotifier(shutdown),
8992 )
9093
91 for i := 0; i < 2; i++ {
92 bar := p.AddBar(int64(1000), BarID(i))
94 total := 10000
95 numBars := 3
96 bars := make([]*mpb.Bar, 0, numBars)
97 for i := 0; i < numBars; i++ {
98 bar := p.AddBar(int64(total))
99 bars = append(bars, bar)
93100 go func() {
94101 for !bar.Completed() {
102 bar.Increment()
95103 time.Sleep(randomDuration(100 * time.Millisecond))
96 bar.Increment()
97104 }
98105 }()
99106 }
100107
101 time.AfterFunc(100*time.Millisecond, func() {
102 close(cancel)
103 })
108 time.Sleep(50 * time.Millisecond)
109 cancel()
104110
105111 p.Wait()
106
107112 select {
108113 case <-shutdown:
109 case <-time.After(200 * time.Millisecond):
110 t.FailNow()
114 case <-time.After(100 * time.Millisecond):
115 t.Error("Progress didn't stop")
111116 }
112117 }
113118