-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathproxyreader_test.go
More file actions
181 lines (151 loc) · 4.09 KB
/
proxyreader_test.go
File metadata and controls
181 lines (151 loc) · 4.09 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package mpb_test
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
const content = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.`
type testReader struct {
r io.Reader
called bool
}
func (r *testReader) Read(p []byte) (n int, err error) {
r.called = true
return r.r.Read(p)
}
func TestProxyReader(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)), mpb.NopStyle())
var buf bytes.Buffer
tr := &testReader{strings.NewReader(content), false}
_, err := io.Copy(&buf, bar.ProxyReader(tr))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
if !tr.called {
t.Error("Read not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
p.Wait()
}
func TestEwmaProxyReader(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)),
mpb.NopStyle(),
mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)),
)
var buf bytes.Buffer
tr := &testReader{strings.NewReader(content), false}
_, err := io.Copy(&buf, bar.ProxyReader(tr))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
if !tr.called {
t.Error("Read not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
p.Wait()
}
type testReadCloser struct {
io.Reader
called bool
}
func (r *testReadCloser) Close() error {
r.called = true
return nil
}
func TestProxyReadCloser(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)), mpb.NopStyle())
tr := &testReadCloser{strings.NewReader(content), false}
rc := bar.ProxyReader(tr)
_, err := io.Copy(io.Discard, rc)
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
_ = rc.Close()
if !tr.called {
t.Error("Close not called")
}
p.Wait()
}
func TestEwmaProxyReadCloser(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)),
mpb.NopStyle(),
mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)),
)
tr := &testReadCloser{strings.NewReader(content), false}
rc := bar.ProxyReader(tr)
_, err := io.Copy(io.Discard, rc)
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
_ = rc.Close()
if !tr.called {
t.Error("Close not called")
}
p.Wait()
}
type testReaderWriterTo struct {
r io.Reader
called bool
}
func (r *testReaderWriterTo) Read(p []byte) (n int, err error) {
return 0, errors.New("unexpected")
}
func (r *testReaderWriterTo) WriteTo(w io.Writer) (n int64, err error) {
r.called = true
return r.r.(io.WriterTo).WriteTo(w)
}
func TestProxyReaderWriterTo(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)), mpb.NopStyle())
var buf bytes.Buffer
tr := &testReaderWriterTo{strings.NewReader(content), false}
_, err := io.Copy(&buf, bar.ProxyReader(tr))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
if !tr.called {
t.Error("WriteTo not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
p.Wait()
}
func TestEwmaProxyReaderWriterTo(t *testing.T) {
p := mpb.New(mpb.WithOutput(io.Discard))
bar := p.New(int64(len(content)),
mpb.NopStyle(),
mpb.AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 30)),
)
var buf bytes.Buffer
tr := &testReaderWriterTo{strings.NewReader(content), false}
_, err := io.Copy(&buf, bar.ProxyReader(tr))
if err != nil {
t.Errorf("io.Copy: %s\n", err.Error())
}
if !tr.called {
t.Error("WriteTo not called")
}
if got := buf.String(); got != content {
t.Errorf("Expected content: %s, got: %s\n", content, got)
}
p.Wait()
}