refactor some tests
Vladimir Bauer
8 years ago
| 4 | 4 | "fmt" |
| 5 | 5 | "io/ioutil" |
| 6 | 6 | "strings" |
| 7 | "sync" | |
| 8 | 7 | "testing" |
| 9 | 8 | "time" |
| 10 | 9 | |
| 32 | 31 | |
| 33 | 32 | func TestBarID(t *testing.T) { |
| 34 | 33 | p := New(WithOutput(ioutil.Discard)) |
| 34 | total := 80 | |
| 35 | wantID := 11 | |
| 36 | bar := p.AddBar(int64(total), BarID(wantID)) | |
| 35 | 37 | |
| 36 | numBars := 3 | |
| 37 | bars := make([]*Bar, numBars) | |
| 38 | for i := 0; i < numBars; i++ { | |
| 39 | bars[i] = p.AddBar(80, BarID(i)) | |
| 40 | go func(bar *Bar) { | |
| 41 | for i := 0; i < 80; i++ { | |
| 42 | time.Sleep(10 * time.Millisecond) | |
| 43 | bar.Increment() | |
| 44 | } | |
| 45 | }(bars[i]) | |
| 38 | go func() { | |
| 39 | for i := 0; i < total; i++ { | |
| 40 | time.Sleep(50 * time.Millisecond) | |
| 41 | bar.Increment() | |
| 42 | } | |
| 43 | }() | |
| 44 | ||
| 45 | gotID := bar.ID() | |
| 46 | if gotID != wantID { | |
| 47 | t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) | |
| 46 | 48 | } |
| 47 | 49 | |
| 50 | p.Abort(bar) | |
| 48 | 51 | p.Wait() |
| 49 | for wantID, bar := range bars { | |
| 50 | gotID := bar.ID() | |
| 51 | if gotID != wantID { | |
| 52 | t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) | |
| 53 | } | |
| 54 | } | |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | 54 | func TestBarIncrWithReFill(t *testing.T) { |
| 64 | 61 | till := 30 |
| 65 | 62 | refillChar := '+' |
| 66 | 63 | |
| 67 | bar := p.AddBar(100, BarTrim()) | |
| 64 | bar := p.AddBar(int64(total), BarTrim()) | |
| 68 | 65 | |
| 69 | 66 | bar.ResumeFill(refillChar, int64(till)) |
| 70 | 67 | |
| 85 | 82 | } |
| 86 | 83 | |
| 87 | 84 | func TestBarPanics(t *testing.T) { |
| 88 | var wg sync.WaitGroup | |
| 89 | 85 | var buf bytes.Buffer |
| 90 | p := New(WithDebugOutput(&buf), WithOutput(nil), WithWaitGroup(&wg)) | |
| 86 | p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) | |
| 91 | 87 | |
| 92 | 88 | wantPanic := "Upps!!!" |
| 93 | numBars := 1 | |
| 94 | wg.Add(numBars) | |
| 89 | total := 100 | |
| 95 | 90 | |
| 96 | for i := 0; i < numBars; i++ { | |
| 97 | name := fmt.Sprintf("b#%02d:", i) | |
| 98 | bar := p.AddBar(100, PrependDecorators( | |
| 99 | func(s *decor.Statistics, _ chan<- int, _ <-chan int) string { | |
| 100 | if s.Current >= 42 { | |
| 101 | panic(wantPanic) | |
| 102 | } | |
| 103 | return name | |
| 104 | }, | |
| 105 | )) | |
| 91 | bar := p.AddBar(int64(total), PrependDecorators( | |
| 92 | func(s *decor.Statistics, _ chan<- int, _ <-chan int) string { | |
| 93 | if s.Current >= 42 { | |
| 94 | panic(wantPanic) | |
| 95 | } | |
| 96 | return "test" | |
| 97 | }, | |
| 98 | )) | |
| 106 | 99 | |
| 107 | go func() { | |
| 108 | defer wg.Done() | |
| 109 | for i := 0; i < 100; i++ { | |
| 110 | time.Sleep(10 * time.Millisecond) | |
| 111 | bar.Increment() | |
| 112 | } | |
| 113 | }() | |
| 114 | } | |
| 100 | go func() { | |
| 101 | for i := 0; i < 100; i++ { | |
| 102 | time.Sleep(10 * time.Millisecond) | |
| 103 | bar.Increment() | |
| 104 | } | |
| 105 | }() | |
| 115 | 106 | |
| 116 | 107 | p.Wait() |
| 117 | 108 | |
| 118 | 109 | wantPanic = fmt.Sprintf("panic: %s", wantPanic) |
| 119 | ||
| 120 | 110 | debugStr := buf.String() |
| 121 | 111 | if !strings.Contains(debugStr, wantPanic) { |
| 122 | 112 | t.Errorf("%q doesn't contain %q\n", debugStr, wantPanic) |