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
182
183
184
185
186
187
188
|
package gbytes_test
import (
"fmt"
"io"
"time"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type FakeCloser struct {
err error
duration time.Duration
}
func (f FakeCloser) Close() error {
time.Sleep(f.duration)
return f.err
}
type FakeReader struct {
err error
duration time.Duration
}
func (f FakeReader) Read(p []byte) (int, error) {
time.Sleep(f.duration)
if f.err != nil {
return 0, f.err
}
for i := 0; i < len(p); i++ {
p[i] = 'a'
}
return len(p), nil
}
type FakeWriter struct {
err error
duration time.Duration
}
func (f FakeWriter) Write(p []byte) (int, error) {
time.Sleep(f.duration)
if f.err != nil {
return 0, f.err
}
return len(p), nil
}
var _ = Describe("Io Wrappers", func() {
Describe("TimeoutCloser", func() {
var innerCloser io.Closer
var timeoutCloser io.Closer
JustBeforeEach(func() {
timeoutCloser = TimeoutCloser(innerCloser, 20*time.Millisecond)
})
Context("when the underlying Closer closes with no error", func() {
BeforeEach(func() {
innerCloser = FakeCloser{}
})
It("returns with no error", func() {
Expect(timeoutCloser.Close()).Should(Succeed())
})
})
Context("when the underlying Closer closes with an error", func() {
BeforeEach(func() {
innerCloser = FakeCloser{err: fmt.Errorf("boom")}
})
It("returns the error", func() {
Expect(timeoutCloser.Close()).Should(MatchError("boom"))
})
})
Context("when the underlying Closer hangs", func() {
BeforeEach(func() {
innerCloser = FakeCloser{
err: fmt.Errorf("boom"),
duration: time.Hour,
}
})
It("returns ErrTimeout", func() {
Expect(timeoutCloser.Close()).Should(MatchError(ErrTimeout))
})
})
})
Describe("TimeoutReader", func() {
var innerReader io.Reader
var timeoutReader io.Reader
JustBeforeEach(func() {
timeoutReader = TimeoutReader(innerReader, 20*time.Millisecond)
})
Context("when the underlying Reader returns no error", func() {
BeforeEach(func() {
innerReader = FakeReader{}
})
It("returns with no error", func() {
p := make([]byte, 5)
n, err := timeoutReader.Read(p)
Expect(n).Should(Equal(5))
Expect(err).ShouldNot(HaveOccurred())
Expect(p).Should(Equal([]byte("aaaaa")))
})
})
Context("when the underlying Reader returns an error", func() {
BeforeEach(func() {
innerReader = FakeReader{err: fmt.Errorf("boom")}
})
It("returns the error", func() {
p := make([]byte, 5)
_, err := timeoutReader.Read(p)
Expect(err).Should(MatchError("boom"))
})
})
Context("when the underlying Reader hangs", func() {
BeforeEach(func() {
innerReader = FakeReader{err: fmt.Errorf("boom"), duration: time.Hour}
})
It("returns ErrTimeout", func() {
p := make([]byte, 5)
_, err := timeoutReader.Read(p)
Expect(err).Should(MatchError(ErrTimeout))
})
})
})
Describe("TimeoutWriter", func() {
var innerWriter io.Writer
var timeoutWriter io.Writer
JustBeforeEach(func() {
timeoutWriter = TimeoutWriter(innerWriter, 20*time.Millisecond)
})
Context("when the underlying Writer returns no error", func() {
BeforeEach(func() {
innerWriter = FakeWriter{}
})
It("returns with no error", func() {
n, err := timeoutWriter.Write([]byte("aaaaa"))
Expect(n).Should(Equal(5))
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("when the underlying Writer returns an error", func() {
BeforeEach(func() {
innerWriter = FakeWriter{err: fmt.Errorf("boom")}
})
It("returns the error", func() {
_, err := timeoutWriter.Write([]byte("aaaaa"))
Expect(err).Should(MatchError("boom"))
})
})
Context("when the underlying Writer hangs", func() {
BeforeEach(func() {
innerWriter = FakeWriter{err: fmt.Errorf("boom"), duration: time.Hour}
})
It("returns ErrTimeout", func() {
_, err := timeoutWriter.Write([]byte("aaaaa"))
Expect(err).Should(MatchError(ErrTimeout))
})
})
})
})
|