...
1 package gbytes_test
2
3 import (
4 "fmt"
5 "io"
6 "time"
7
8 . "github.com/onsi/gomega/gbytes"
9
10 . "github.com/onsi/ginkgo/v2"
11 . "github.com/onsi/gomega"
12 )
13
14 type FakeCloser struct {
15 err error
16 duration time.Duration
17 }
18
19 func (f FakeCloser) Close() error {
20 time.Sleep(f.duration)
21 return f.err
22 }
23
24 type FakeReader struct {
25 err error
26 duration time.Duration
27 }
28
29 func (f FakeReader) Read(p []byte) (int, error) {
30 time.Sleep(f.duration)
31 if f.err != nil {
32 return 0, f.err
33 }
34
35 for i := 0; i < len(p); i++ {
36 p[i] = 'a'
37 }
38
39 return len(p), nil
40 }
41
42 type FakeWriter struct {
43 err error
44 duration time.Duration
45 }
46
47 func (f FakeWriter) Write(p []byte) (int, error) {
48 time.Sleep(f.duration)
49 if f.err != nil {
50 return 0, f.err
51 }
52
53 return len(p), nil
54 }
55
56 var _ = Describe("Io Wrappers", func() {
57 Describe("TimeoutCloser", func() {
58 var innerCloser io.Closer
59 var timeoutCloser io.Closer
60
61 JustBeforeEach(func() {
62 timeoutCloser = TimeoutCloser(innerCloser, 20*time.Millisecond)
63 })
64
65 When("the underlying Closer closes with no error", func() {
66 BeforeEach(func() {
67 innerCloser = FakeCloser{}
68 })
69
70 It("returns with no error", func() {
71 Expect(timeoutCloser.Close()).Should(Succeed())
72 })
73 })
74
75 When("the underlying Closer closes with an error", func() {
76 BeforeEach(func() {
77 innerCloser = FakeCloser{err: fmt.Errorf("boom")}
78 })
79
80 It("returns the error", func() {
81 Expect(timeoutCloser.Close()).Should(MatchError("boom"))
82 })
83 })
84
85 When("the underlying Closer hangs", func() {
86 BeforeEach(func() {
87 innerCloser = FakeCloser{
88 err: fmt.Errorf("boom"),
89 duration: time.Hour,
90 }
91 })
92
93 It("returns ErrTimeout", func() {
94 Expect(timeoutCloser.Close()).Should(MatchError(ErrTimeout))
95 })
96 })
97 })
98
99 Describe("TimeoutReader", func() {
100 var innerReader io.Reader
101 var timeoutReader io.Reader
102
103 JustBeforeEach(func() {
104 timeoutReader = TimeoutReader(innerReader, 20*time.Millisecond)
105 })
106
107 When("the underlying Reader returns no error", func() {
108 BeforeEach(func() {
109 innerReader = FakeReader{}
110 })
111
112 It("returns with no error", func() {
113 p := make([]byte, 5)
114 n, err := timeoutReader.Read(p)
115 Expect(n).Should(Equal(5))
116 Expect(err).ShouldNot(HaveOccurred())
117 Expect(p).Should(Equal([]byte("aaaaa")))
118 })
119 })
120
121 When("the underlying Reader returns an error", func() {
122 BeforeEach(func() {
123 innerReader = FakeReader{err: fmt.Errorf("boom")}
124 })
125
126 It("returns the error", func() {
127 p := make([]byte, 5)
128 _, err := timeoutReader.Read(p)
129 Expect(err).Should(MatchError("boom"))
130 })
131 })
132
133 When("the underlying Reader hangs", func() {
134 BeforeEach(func() {
135 innerReader = FakeReader{err: fmt.Errorf("boom"), duration: time.Hour}
136 })
137
138 It("returns ErrTimeout", func() {
139 p := make([]byte, 5)
140 _, err := timeoutReader.Read(p)
141 Expect(err).Should(MatchError(ErrTimeout))
142 })
143 })
144 })
145
146 Describe("TimeoutWriter", func() {
147 var innerWriter io.Writer
148 var timeoutWriter io.Writer
149
150 JustBeforeEach(func() {
151 timeoutWriter = TimeoutWriter(innerWriter, 20*time.Millisecond)
152 })
153
154 When("the underlying Writer returns no error", func() {
155 BeforeEach(func() {
156 innerWriter = FakeWriter{}
157 })
158
159 It("returns with no error", func() {
160 n, err := timeoutWriter.Write([]byte("aaaaa"))
161 Expect(n).Should(Equal(5))
162 Expect(err).ShouldNot(HaveOccurred())
163 })
164 })
165
166 When("the underlying Writer returns an error", func() {
167 BeforeEach(func() {
168 innerWriter = FakeWriter{err: fmt.Errorf("boom")}
169 })
170
171 It("returns the error", func() {
172 _, err := timeoutWriter.Write([]byte("aaaaa"))
173 Expect(err).Should(MatchError("boom"))
174 })
175 })
176
177 When("the underlying Writer hangs", func() {
178 BeforeEach(func() {
179 innerWriter = FakeWriter{err: fmt.Errorf("boom"), duration: time.Hour}
180 })
181
182 It("returns ErrTimeout", func() {
183 _, err := timeoutWriter.Write([]byte("aaaaa"))
184 Expect(err).Should(MatchError(ErrTimeout))
185 })
186 })
187 })
188 })
189
View as plain text