10 // Gocheck boilerplate
11 func Test(t *testing.T) { TestingT(t) }
13 var _ = Suite(&StandaloneSuite{})
16 type StandaloneSuite struct{}
18 func (s *StandaloneSuite) TestReadIntoBuffer(c *C) {
19 ReadIntoBufferHelper(c, 225)
20 ReadIntoBufferHelper(c, 224)
23 func HelperWrite128andCheck(c *C, buffer []byte, writer io.Writer, slices chan nextSlice) {
24 out := make([]byte, 128)
25 for i := 0; i < 128; i += 1 {
30 c.Check(len(s1.slice), Equals, 128)
31 c.Check(s1.reader_error, Equals, nil)
32 for i := 0; i < 128; i += 1 {
33 c.Check(s1.slice[i], Equals, byte(i))
35 for i := 0; i < len(buffer); i += 1 {
37 c.Check(buffer[i], Equals, byte(i))
39 c.Check(buffer[i], Equals, byte(0))
44 func HelperWrite96andCheck(c *C, buffer []byte, writer io.Writer, slices chan nextSlice) {
45 out := make([]byte, 96)
46 for i := 0; i < 96; i += 1 {
51 c.Check(len(s1.slice), Equals, 96)
52 c.Check(s1.reader_error, Equals, nil)
53 for i := 0; i < 96; i += 1 {
54 c.Check(s1.slice[i], Equals, byte(i/2))
56 for i := 0; i < len(buffer); i += 1 {
58 c.Check(buffer[i], Equals, byte(i))
59 } else if i < (128 + 96) {
60 c.Check(buffer[i], Equals, byte((i-128)/2))
62 c.Check(buffer[i], Equals, byte(0))
67 func ReadIntoBufferHelper(c *C, bufsize int) {
68 buffer := make([]byte, bufsize)
70 reader, writer := io.Pipe()
71 slices := make(chan nextSlice)
73 go readIntoBuffer(buffer, reader, slices)
75 HelperWrite128andCheck(c, buffer, writer, slices)
76 HelperWrite96andCheck(c, buffer, writer, slices)
80 c.Check(len(s1.slice), Equals, 0)
81 c.Check(s1.reader_error, Equals, io.EOF)
84 func (s *StandaloneSuite) TestReadIntoShortBuffer(c *C) {
85 buffer := make([]byte, 223)
86 reader, writer := io.Pipe()
87 slices := make(chan nextSlice)
89 go readIntoBuffer(buffer, reader, slices)
91 HelperWrite128andCheck(c, buffer, writer, slices)
93 out := make([]byte, 96)
94 for i := 0; i < 96; i += 1 {
98 // Write will deadlock because it can't write all the data, so
99 // spin it off to a goroutine
103 c.Check(len(s1.slice), Equals, 95)
104 c.Check(s1.reader_error, Equals, nil)
105 for i := 0; i < 95; i += 1 {
106 c.Check(s1.slice[i], Equals, byte(i/2))
108 for i := 0; i < len(buffer); i += 1 {
110 c.Check(buffer[i], Equals, byte(i))
111 } else if i < (128 + 95) {
112 c.Check(buffer[i], Equals, byte((i-128)/2))
114 c.Check(buffer[i], Equals, byte(0))
120 c.Check(len(s1.slice), Equals, 0)
121 c.Check(s1.reader_error, Equals, io.ErrShortBuffer)
124 func (s *StandaloneSuite) TestTransfer(c *C) {
125 reader, writer := io.Pipe()
127 tr := AsyncStreamFromReader(512, reader)
129 br1 := tr.MakeStreamReader()
130 out := make([]byte, 128)
133 // Write some data, and read into a buffer shorter than
135 for i := 0; i < 128; i += 1 {
139 writer.Write(out[:100])
141 in := make([]byte, 64)
142 n, err := br1.Read(in)
144 c.Check(n, Equals, 64)
145 c.Check(err, Equals, nil)
147 for i := 0; i < 64; i += 1 {
148 c.Check(in[i], Equals, out[i])
153 // Write some more data, and read into buffer longer than
155 in := make([]byte, 64)
156 n, err := br1.Read(in)
157 c.Check(n, Equals, 36)
158 c.Check(err, Equals, nil)
160 for i := 0; i < 36; i += 1 {
161 c.Check(in[i], Equals, out[64+i])
167 // Test read before write
173 in := make([]byte, 64)
176 n, err := br1.Read(in)
180 time.Sleep(100 * time.Millisecond)
181 writer.Write(out[100:])
185 c.Check(got.n, Equals, 28)
186 c.Check(got.err, Equals, nil)
188 for i := 0; i < 28; i += 1 {
189 c.Check(in[i], Equals, out[100+i])
193 br2 := tr.MakeStreamReader()
195 // Test 'catch up' reader
196 in := make([]byte, 256)
197 n, err := br2.Read(in)
199 c.Check(n, Equals, 128)
200 c.Check(err, Equals, nil)
202 for i := 0; i < 128; i += 1 {
203 c.Check(in[i], Equals, out[i])
208 // Test closing the reader
211 in := make([]byte, 256)
212 n1, err1 := br1.Read(in)
213 n2, err2 := br2.Read(in)
214 c.Check(n1, Equals, 0)
215 c.Check(err1, Equals, io.EOF)
216 c.Check(n2, Equals, 0)
217 c.Check(err2, Equals, io.EOF)
221 // Test 'catch up' reader after closing
222 br3 := tr.MakeStreamReader()
223 in := make([]byte, 256)
224 n, err := br3.Read(in)
226 c.Check(n, Equals, 128)
227 c.Check(err, Equals, nil)
229 for i := 0; i < 128; i += 1 {
230 c.Check(in[i], Equals, out[i])
233 n, err = br3.Read(in)
235 c.Check(n, Equals, 0)
236 c.Check(err, Equals, io.EOF)
240 func (s *StandaloneSuite) TestTransferShortBuffer(c *C) {
241 reader, writer := io.Pipe()
243 tr := AsyncStreamFromReader(100, reader)
246 sr := tr.MakeStreamReader()
249 out := make([]byte, 101)
252 n, err := sr.Read(out)
253 c.Check(n, Equals, 100)
256 n, err = sr.Read(out)
257 c.Check(n, Equals, 0)
258 c.Check(err, Equals, io.ErrShortBuffer)
261 func (s *StandaloneSuite) TestTransferFromBuffer(c *C) {
262 // Buffer for reads from 'r'
263 buffer := make([]byte, 100)
264 for i := 0; i < 100; i += 1 {
268 tr := AsyncStreamFromSlice(buffer)
270 br1 := tr.MakeStreamReader()
272 in := make([]byte, 64)
274 n, err := br1.Read(in)
276 c.Check(n, Equals, 64)
277 c.Check(err, Equals, nil)
279 for i := 0; i < 64; i += 1 {
280 c.Check(in[i], Equals, buffer[i])
284 n, err := br1.Read(in)
286 c.Check(n, Equals, 36)
287 c.Check(err, Equals, nil)
289 for i := 0; i < 36; i += 1 {
290 c.Check(in[i], Equals, buffer[64+i])
294 n, err := br1.Read(in)
296 c.Check(n, Equals, 0)
297 c.Check(err, Equals, io.EOF)
301 func (s *StandaloneSuite) TestTransferIoCopy(c *C) {
302 // Buffer for reads from 'r'
303 buffer := make([]byte, 100)
304 for i := 0; i < 100; i += 1 {
308 tr := AsyncStreamFromSlice(buffer)
311 br1 := tr.MakeStreamReader()
314 reader, writer := io.Pipe()
317 p := make([]byte, 100)
318 n, err := reader.Read(p)
319 c.Check(n, Equals, 100)
320 c.Check(err, Equals, nil)
321 c.Check(p, DeepEquals, buffer)
327 func (s *StandaloneSuite) TestManyReaders(c *C) {
328 reader, writer := io.Pipe()
330 tr := AsyncStreamFromReader(512, reader)
333 sr := tr.MakeStreamReader()
335 time.Sleep(100 * time.Millisecond)
339 for i := 0; i < 200; i += 1 {
341 br1 := tr.MakeStreamReader()
345 n, err := br1.Read(p)
346 c.Check(n, Equals, 3)
347 c.Check(p[0:3], DeepEquals, []byte("foo"))
350 c.Check(n, Equals, 3)
351 c.Check(p[0:3], DeepEquals, []byte("bar"))
354 c.Check(n, Equals, 3)
355 c.Check(p[0:3], DeepEquals, []byte("baz"))
358 c.Check(n, Equals, 0)
359 c.Check(err, Equals, io.EOF)
363 writer.Write([]byte("foo"))
364 writer.Write([]byte("bar"))
365 writer.Write([]byte("baz"))
369 func (s *StandaloneSuite) TestMultipleCloseNoPanic(c *C) {
370 buffer := make([]byte, 100)
371 tr := AsyncStreamFromSlice(buffer)
372 sr := tr.MakeStreamReader()
373 c.Check(sr.Close(), IsNil)
374 c.Check(sr.Close(), Equals, ErrAlreadyClosed)
375 c.Check(tr.Close(), IsNil)
376 c.Check(tr.Close(), Equals, ErrAlreadyClosed)