Codebase list golang-github-vbauerster-mpb / 164717d
refactoring: remove wt fields Vladimir Bauer 3 years ago
2 changed file(s) with 15 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1717
1818 type proxyWriterTo struct {
1919 proxyReader
20 wt io.WriterTo
2120 }
2221
2322 func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
24 n, err := x.wt.WriteTo(w)
23 n, err := x.ReadCloser.(io.WriterTo).WriteTo(w)
2524 x.bar.IncrInt64(n)
2625 return n, err
2726 }
4140
4241 type ewmaProxyWriterTo struct {
4342 ewmaProxyReader
44 wt proxyWriterTo
4543 }
4644
4745 func (x ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) {
4846 start := time.Now()
49 n, err := x.wt.WriteTo(w)
47 n, err := x.ReadCloser.(io.WriterTo).WriteTo(w)
5048 if n > 0 {
5149 x.bar.DecoratorEwmaUpdate(time.Since(start))
5250 }
5755 pr := proxyReader{toReadCloser(r), b}
5856 if hasEwma {
5957 epr := ewmaProxyReader{pr}
60 if wt, ok := r.(io.WriterTo); ok {
61 pwt := proxyWriterTo{pr, wt}
62 return ewmaProxyWriterTo{epr, pwt}
58 if _, ok := r.(io.WriterTo); ok {
59 return ewmaProxyWriterTo{epr}
6360 }
6461 return epr
6562 }
66 if wt, ok := r.(io.WriterTo); ok {
67 return proxyWriterTo{pr, wt}
63 if _, ok := r.(io.WriterTo); ok {
64 return proxyWriterTo{pr}
6865 }
6966 return pr
7067 }
2828
2929 type testWriterTo struct {
3030 *testReader
31 wt io.WriterTo
31 called bool
3232 }
3333
34 func (wt testWriterTo) WriteTo(w io.Writer) (n int64, err error) {
34 func (wt *testWriterTo) WriteTo(w io.Writer) (n int64, err error) {
3535 wt.called = true
36 return wt.wt.WriteTo(w)
36 return wt.Reader.(io.WriterTo).WriteTo(w)
3737 }
3838
3939 func TestProxyReader(t *testing.T) {
4040 p := mpb.New(mpb.WithOutput(io.Discard))
4141
42 tReader := &testReader{strings.NewReader(content), false}
42 reader := &testReader{strings.NewReader(content), false}
4343
4444 bar := p.AddBar(int64(len(content)))
4545
4646 var buf bytes.Buffer
47 _, err := io.Copy(&buf, bar.ProxyReader(tReader))
47 _, err := io.Copy(&buf, bar.ProxyReader(reader))
4848 if err != nil {
4949 t.Errorf("Error copying from reader: %+v\n", err)
5050 }
5151
5252 p.Wait()
5353
54 if !tReader.called {
54 if !reader.called {
5555 t.Error("Read not called")
5656 }
5757
6464 p := mpb.New(mpb.WithOutput(io.Discard))
6565
6666 var reader io.Reader = strings.NewReader(content)
67 tWriterTo := testWriterTo{&testReader{reader, false}, reader.(io.WriterTo)}
67 writerTo := &testWriterTo{&testReader{reader, false}, false}
6868
6969 bar := p.AddBar(int64(len(content)))
7070
7171 var buf bytes.Buffer
72 _, err := io.Copy(&buf, bar.ProxyReader(tWriterTo))
72 _, err := io.Copy(&buf, bar.ProxyReader(writerTo))
7373 if err != nil {
7474 t.Errorf("Error copying from reader: %+v\n", err)
7575 }
7676
7777 p.Wait()
7878
79 if !tWriterTo.called {
79 if !writerTo.called {
8080 t.Error("WriteTo not called")
8181 }
8282