TestProxyReadCloser
Vladimir Bauer
3 years ago
| 26 | 26 | return r.Reader.Read(p) |
| 27 | 27 | } |
| 28 | 28 | |
| 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 | ||
| 39 | 29 | func TestProxyReader(t *testing.T) { |
| 40 | 30 | p := mpb.New(mpb.WithOutput(io.Discard)) |
| 41 | 31 | |
| 60 | 50 | } |
| 61 | 51 | } |
| 62 | 52 | |
| 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 | ||
| 63 | 91 | func TestProxyWriterTo(t *testing.T) { |
| 64 | 92 | p := mpb.New(mpb.WithOutput(io.Discard)) |
| 65 | 93 | |
| 66 | var reader io.Reader = strings.NewReader(content) | |
| 67 | writerTo := &testWriterTo{&testReader{reader, false}, false} | |
| 94 | writerTo := &testWriterTo{strings.NewReader(content), false} | |
| 68 | 95 | |
| 69 | 96 | bar := p.AddBar(int64(len(content))) |
| 70 | 97 | |