Skip to content

Commit a03d9e1

Browse files
committed
refactor: proxywriter_test with ewma
1 parent 4c4ab56 commit a03d9e1

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

proxywriter_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/vbauerster/mpb/v8"
11+
"github.com/vbauerster/mpb/v8/decor"
1112
)
1213

1314
type testWriter struct {
@@ -41,6 +42,30 @@ func TestProxyWriter(t *testing.T) {
4142
p.Wait()
4243
}
4344

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+
4469
type testWriteCloser struct {
4570
io.Writer
4671
called bool
@@ -69,6 +94,27 @@ func TestProxyWriteCloser(t *testing.T) {
6994
p.Wait()
7095
}
7196

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+
72118
type testWriterReadFrom struct {
73119
io.Writer
74120
called bool
@@ -112,3 +158,28 @@ func TestProxyWriterReadFrom(t *testing.T) {
112158
}
113159
p.Wait()
114160
}
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

Comments
 (0)