|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/vbauerster/mpb/v8" |
| 11 | + "github.com/vbauerster/mpb/v8/decor" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | type testWriter struct { |
@@ -41,6 +42,30 @@ func TestProxyWriter(t *testing.T) { |
41 | 42 | p.Wait() |
42 | 43 | } |
43 | 44 |
|
| 45 | +func TestEwmaProxyWriter(t *testing.T) { |
| 46 | + p := mpb.New(mpb.WithOutput(io.Discard)) |
| 47 | + bar := p.New(int64(len(content)), |
| 48 | + mpb.NopStyle(), |
| 49 | + mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)), |
| 50 | + ) |
| 51 | + |
| 52 | + var buf bytes.Buffer |
| 53 | + tw := &testWriter{&buf, false} |
| 54 | + _, err := io.Copy(bar.ProxyWriter(tw), strings.NewReader(content)) |
| 55 | + if err != nil { |
| 56 | + t.Errorf("io.Copy: %s\n", err.Error()) |
| 57 | + } |
| 58 | + |
| 59 | + if !tw.called { |
| 60 | + t.Error("Read not called") |
| 61 | + } |
| 62 | + |
| 63 | + if got := buf.String(); got != content { |
| 64 | + t.Errorf("Expected content: %s, got: %s\n", content, got) |
| 65 | + } |
| 66 | + p.Wait() |
| 67 | +} |
| 68 | + |
44 | 69 | type testWriteCloser struct { |
45 | 70 | io.Writer |
46 | 71 | called bool |
@@ -69,6 +94,27 @@ func TestProxyWriteCloser(t *testing.T) { |
69 | 94 | p.Wait() |
70 | 95 | } |
71 | 96 |
|
| 97 | +func TestEwmaProxyWriteCloser(t *testing.T) { |
| 98 | + p := mpb.New(mpb.WithOutput(io.Discard)) |
| 99 | + bar := p.New(int64(len(content)), |
| 100 | + mpb.NopStyle(), |
| 101 | + mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)), |
| 102 | + ) |
| 103 | + |
| 104 | + var buf bytes.Buffer |
| 105 | + tw := &testWriteCloser{&buf, false} |
| 106 | + wc := bar.ProxyWriter(tw) |
| 107 | + _, err := io.Copy(wc, strings.NewReader(content)) |
| 108 | + if err != nil { |
| 109 | + t.Errorf("io.Copy: %s\n", err.Error()) |
| 110 | + } |
| 111 | + _ = wc.Close() |
| 112 | + if !tw.called { |
| 113 | + t.Error("Close not called") |
| 114 | + } |
| 115 | + p.Wait() |
| 116 | +} |
| 117 | + |
72 | 118 | type testWriterReadFrom struct { |
73 | 119 | io.Writer |
74 | 120 | called bool |
@@ -112,3 +158,28 @@ func TestProxyWriterReadFrom(t *testing.T) { |
112 | 158 | } |
113 | 159 | p.Wait() |
114 | 160 | } |
| 161 | + |
| 162 | +func TestEwmaProxyWriterReadFrom(t *testing.T) { |
| 163 | + p := mpb.New(mpb.WithOutput(io.Discard)) |
| 164 | + bar := p.New(int64(len(content)), |
| 165 | + mpb.NopStyle(), |
| 166 | + mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)), |
| 167 | + ) |
| 168 | + |
| 169 | + var buf bytes.Buffer |
| 170 | + tw := &testWriterReadFrom{&buf, false} |
| 171 | + // To trigger ReadFrom, WriteTo of strings.NewReader needs to be hidden |
| 172 | + _, err := io.Copy(bar.ProxyWriter(tw), wrapReader{strings.NewReader(content)}) |
| 173 | + if err != nil { |
| 174 | + t.Errorf("io.Copy: %s\n", err.Error()) |
| 175 | + } |
| 176 | + |
| 177 | + if !tw.called { |
| 178 | + t.Error("ReadFrom not called") |
| 179 | + } |
| 180 | + |
| 181 | + if got := buf.String(); got != content { |
| 182 | + t.Errorf("Expected content: %s, got: %s\n", content, got) |
| 183 | + } |
| 184 | + p.Wait() |
| 185 | +} |
0 commit comments