Codebase list golang-github-vbauerster-mpb / 90674bb
TestProxyReadCloser Vladimir Bauer 3 years ago
1 changed file(s) with 39 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
2626 return r.Reader.Read(p)
2727 }
2828
29 type testWriterTo struct {
30 *testReader
31 called bool
32 }
33
34 func (wt *testWriterTo) WriteTo(w io.Writer) (n int64, err error) {
35 wt.called = true
36 return wt.Reader.(io.WriterTo).WriteTo(w)
37 }
38
3929 func TestProxyReader(t *testing.T) {
4030 p := mpb.New(mpb.WithOutput(io.Discard))
4131
6050 }
6151 }
6252
53 type testReadCloser struct {
54 io.Reader
55 called bool
56 }
57
58 func (r *testReadCloser) Close() error {
59 r.called = true
60 return nil
61 }
62
63 func TestProxyReadCloser(t *testing.T) {
64 p := mpb.New(mpb.WithOutput(io.Discard))
65
66 reader := &testReadCloser{strings.NewReader(content), false}
67
68 bar := p.AddBar(int64(len(content)))
69
70 rc := bar.ProxyReader(reader)
71 _, _ = io.Copy(io.Discard, rc)
72 _ = rc.Close()
73
74 p.Wait()
75
76 if !reader.called {
77 t.Error("Close not called")
78 }
79 }
80
81 type testWriterTo struct {
82 io.Reader
83 called bool
84 }
85
86 func (wt *testWriterTo) WriteTo(w io.Writer) (n int64, err error) {
87 wt.called = true
88 return wt.Reader.(io.WriterTo).WriteTo(w)
89 }
90
6391 func TestProxyWriterTo(t *testing.T) {
6492 p := mpb.New(mpb.WithOutput(io.Discard))
6593
66 var reader io.Reader = strings.NewReader(content)
67 writerTo := &testWriterTo{&testReader{reader, false}, false}
94 writerTo := &testWriterTo{strings.NewReader(content), false}
6895
6996 bar := p.AddBar(int64(len(content)))
7097