-
Notifications
You must be signed in to change notification settings - Fork 124
/
proxywriter_test.go
120 lines (90 loc) · 2.15 KB
/
proxywriter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package mpb_test
import (
"bytes"
"io"
"strings"
"testing"
"github.com/vbauerster/mpb/v8"
)
type testWriter struct {
io.Writer
called bool
}
func (w *testWriter) Write(p []byte) (n int, err error) {
w.called = true
return w.Writer.Write(p)
}
func TestProxyWriter(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
var buf bytes.Buffer
tw := &testWriter{&buf, false}
bar := p.New(int64(len(content)), mpb.NopStyle())
_, err := io.Copy(bar.ProxyWriter(tw), strings.NewReader(content))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
p.Wait()
if !tw.called {
t.Error("Read not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
}
type testWriteCloser struct {
io.Writer
called bool
}
func (w *testWriteCloser) Close() error {
w.called = true
return nil
}
func TestProxyWriteCloser(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
var buf bytes.Buffer
tw := &testWriteCloser{&buf, false}
bar := p.New(int64(len(content)), mpb.NopStyle())
wc := bar.ProxyWriter(tw)
_, err := io.Copy(wc, strings.NewReader(content))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
_ = wc.Close()
p.Wait()
if !tw.called {
t.Error("Close not called")
}
}
type testWriterReadFrom struct {
io.Writer
called bool
}
func (w *testWriterReadFrom) ReadFrom(r io.Reader) (n int64, err error) {
w.called = true
return w.Writer.(io.ReaderFrom).ReadFrom(r)
}
type dumbReader struct {
r io.Reader
}
func (r dumbReader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
func TestProxyWriterReadFrom(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
var buf bytes.Buffer
tw := &testWriterReadFrom{&buf, false}
bar := p.New(int64(len(content)), mpb.NopStyle())
// To trigger ReadFrom, WriteTo needs to be hidden, hence a dumb wrapper
dr := dumbReader{strings.NewReader(content)}
_, err := io.Copy(bar.ProxyWriter(tw), dr)
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
p.Wait()
if !tw.called {
t.Error("ReadFrom not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
}