-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwmc_test.go
120 lines (107 loc) · 2.33 KB
/
rwmc_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 rwmc
import (
"io"
"testing"
"bytes"
"io/ioutil"
"github.com/docker/docker/pkg/ioutils"
)
type fakeRC struct {
closed int
read int
reader io.Reader
}
func (f *fakeRC) Read(p []byte) (n int, err error) {
f.read++
return f.reader.Read(p)
}
func (f *fakeRC) Close() error {
f.closed++
return nil
}
func TestReadMultiCloser_Read(t *testing.T) {
exp := []byte("napis")
{
r := bytes.NewReader(exp)
rwc := NewReadMultiCloser(ioutil.NopCloser(r))
b, err := ioutil.ReadAll(rwc)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !bytes.Equal(exp, b) {
t.Errorf("want: %x, got: %x", exp, b)
}
}
{
r := bytes.NewReader(exp)
rwc := &ReadMultiCloser{}
rwc.Push(ioutil.NopCloser(r))
b, err := ioutil.ReadAll(rwc)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !bytes.Equal(exp, b) {
t.Errorf("want: %x, got: %x", exp, b)
}
}
}
func TestReadMultiCloser_Push(t *testing.T) {
closed0 := false
rc0 := ioutils.NewReadCloserWrapper(nil, func() error {
closed0 = true
return io.ErrUnexpectedEOF
})
closed1 := false
rc1 := ioutils.NewReadCloserWrapper(nil, func() error {
closed1 = true
return io.EOF
})
rwc := &ReadMultiCloser{}
rwc.Push(rc1)
rwc.Push(rc0)
if err := rwc.Close(); err == nil {
t.Errorf("want: %v, got: %v", io.ErrUnexpectedEOF, err)
}
if !closed0 {
t.Error("Close was not called on ReadCloser")
}
if !closed1 {
t.Error("Close was not called on Closer")
}
}
func TestReadMultiCloser_CloseOne(t *testing.T) {
closed := false
rc := ioutils.NewReadCloserWrapper(nil, func() error {
closed = true
return io.ErrUnexpectedEOF
})
rwc := NewReadMultiCloser(rc)
if err := rwc.Close(); err == nil {
t.Errorf("want: %v, got: %v", io.ErrUnexpectedEOF, err)
}
if !closed {
t.Error("Close was not called")
}
}
func TestReadMultiCloser_CloseMany(t *testing.T) {
closed0 := false
rc := ioutils.NewReadCloserWrapper(nil, func() error {
closed0 = true
return io.ErrUnexpectedEOF
})
closed1 := false
c := ioutils.NewReadCloserWrapper(nil, func() error {
closed1 = true
return io.EOF
})
rwc := NewReadMultiCloser(rc, c)
if err := rwc.Close(); err == nil {
t.Errorf("want: %v, got: %v", io.ErrUnexpectedEOF, err)
}
if !closed0 {
t.Error("Close was not called on ReadCloser")
}
if !closed1 {
t.Error("Close was not called on Closer")
}
}