test p.WithContext and p.WithCancel
Vladimir Bauer
9 years ago
| 0 | package mpb | |
| 0 | package mpb_test | |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | 3 | "bytes" |
| 4 | "context" | |
| 5 | "fmt" | |
| 6 | "math/rand" | |
| 7 | "strings" | |
| 8 | "sync" | |
| 4 | 9 | "testing" |
| 10 | "time" | |
| 11 | "unicode/utf8" | |
| 12 | ||
| 13 | "github.com/vbauerster/mpb" | |
| 5 | 14 | ) |
| 6 | 15 | |
| 16 | func TestDefaultWidth(t *testing.T) { | |
| 17 | var buf bytes.Buffer | |
| 18 | p := mpb.New().SetOut(&buf) | |
| 19 | bar := p.AddBar(100) | |
| 20 | for i := 0; i < 100; i++ { | |
| 21 | bar.Incr(1) | |
| 22 | } | |
| 23 | p.Stop() | |
| 24 | runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String())) | |
| 25 | defWidth := 80 | |
| 26 | if runeCount != defWidth { | |
| 27 | t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount) | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | func TestCustomWidth(t *testing.T) { | |
| 32 | customWidth := 60 | |
| 33 | var buf bytes.Buffer | |
| 34 | p := mpb.New().SetWidth(customWidth).SetOut(&buf) | |
| 35 | bar := p.AddBar(100) | |
| 36 | for i := 0; i < 100; i++ { | |
| 37 | bar.Incr(1) | |
| 38 | } | |
| 39 | p.Stop() | |
| 40 | runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String())) | |
| 41 | if runeCount != customWidth { | |
| 42 | t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount) | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 7 | 46 | func TestAddBar(t *testing.T) { |
| 47 | var wg sync.WaitGroup | |
| 8 | 48 | var buf bytes.Buffer |
| 9 | p := New().SetWidth(60).SetOut(&buf) | |
| 49 | p := mpb.New().SetOut(&buf) | |
| 50 | ||
| 10 | 51 | count := p.BarCount() |
| 11 | 52 | if count != 0 { |
| 12 | t.Errorf("Count want: %q, got: %q\n", 0, count) | |
| 53 | t.Errorf("BarCount want: %q, got: %q\n", 0, count) | |
| 13 | 54 | } |
| 14 | bar := p.AddBar(10) | |
| 55 | ||
| 56 | numBars := 3 | |
| 57 | wg.Add(numBars) | |
| 58 | ||
| 59 | for i := 0; i < numBars; i++ { | |
| 60 | name := fmt.Sprintf("Bar#%d:", i) | |
| 61 | bar := p.AddBar(100).PrependName(name, len(name), 0) | |
| 62 | ||
| 63 | go func() { | |
| 64 | defer wg.Done() | |
| 65 | for i := 0; i < 100; i++ { | |
| 66 | bar.Incr(1) | |
| 67 | } | |
| 68 | }() | |
| 69 | } | |
| 70 | ||
| 15 | 71 | count = p.BarCount() |
| 16 | if count != 1 { | |
| 17 | t.Errorf("Count want: %q, got: %q\n", 0, count) | |
| 72 | if count != numBars { | |
| 73 | t.Errorf("BarCount want: %q, got: %q\n", numBars, count) | |
| 18 | 74 | } |
| 19 | for i := 0; i < 10; i++ { | |
| 20 | bar.Incr(1) | |
| 21 | } | |
| 75 | wg.Wait() | |
| 22 | 76 | p.Stop() |
| 23 | 77 | } |
| 24 | 78 | |
| 25 | 79 | func TestRemoveBar(t *testing.T) { |
| 26 | p := New() | |
| 80 | p := mpb.New() | |
| 81 | ||
| 27 | 82 | b := p.AddBar(10) |
| 28 | 83 | |
| 29 | 84 | if !p.RemoveBar(b) { |
| 32 | 87 | |
| 33 | 88 | count := p.BarCount() |
| 34 | 89 | if count != 0 { |
| 35 | t.Errorf("Count want: %q, got: %q\n", 0, count) | |
| 90 | t.Errorf("BarCount want: %q, got: %q\n", 0, count) | |
| 36 | 91 | } |
| 37 | 92 | p.Stop() |
| 38 | 93 | } |
| 94 | ||
| 95 | func TestWithContext(t *testing.T) { | |
| 96 | ctx, cancel := context.WithCancel(context.Background()) | |
| 97 | shutdown := make(chan struct{}) | |
| 98 | p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown) | |
| 99 | numBars := 3 | |
| 100 | ||
| 101 | for i := 0; i < numBars; i++ { | |
| 102 | name := fmt.Sprintf("Bar#%d:", i) | |
| 103 | bar := p.AddBar(100).PrependName(name, len(name), 0) | |
| 104 | ||
| 105 | go func() { | |
| 106 | for i := 0; i < 10000; i++ { | |
| 107 | bar.Incr(1) | |
| 108 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 109 | } | |
| 110 | }() | |
| 111 | } | |
| 112 | ||
| 113 | cancel() | |
| 114 | ||
| 115 | select { | |
| 116 | case <-shutdown: | |
| 117 | case <-time.After(500 * time.Millisecond): | |
| 118 | t.Error("ProgressBar didn't stop") | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | func TestWithCancel(t *testing.T) { | |
| 123 | cancel := make(chan struct{}) | |
| 124 | shutdown := make(chan struct{}) | |
| 125 | p := mpb.New().WithCancel(cancel).ShutdownNotify(shutdown) | |
| 126 | numBars := 3 | |
| 127 | ||
| 128 | for i := 0; i < numBars; i++ { | |
| 129 | name := fmt.Sprintf("Bar#%d:", i) | |
| 130 | bar := p.AddBar(100).PrependName(name, len(name), 0) | |
| 131 | ||
| 132 | go func() { | |
| 133 | for i := 0; i < 10000; i++ { | |
| 134 | bar.Incr(1) | |
| 135 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) | |
| 136 | } | |
| 137 | }() | |
| 138 | } | |
| 139 | ||
| 140 | close(cancel) | |
| 141 | ||
| 142 | select { | |
| 143 | case <-shutdown: | |
| 144 | case <-time.After(500 * time.Millisecond): | |
| 145 | t.Error("ProgressBar didn't stop") | |
| 146 | } | |
| 147 | } | |