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)
255 n, err = sr.Read(out)
256 c.Check(n, Equals, 0)
257 c.Check(err, Equals, io.ErrShortBuffer)
260 func (s *StandaloneSuite) TestTransferFromBuffer(c *C) {
261 // Buffer for reads from 'r'
262 buffer := make([]byte, 100)
263 for i := 0; i < 100; i += 1 {
267 tr := AsyncStreamFromSlice(buffer)
269 br1 := tr.MakeStreamReader()
271 in := make([]byte, 64)
273 n, err := br1.Read(in)
275 c.Check(n, Equals, 64)
276 c.Check(err, Equals, nil)
278 for i := 0; i < 64; i += 1 {
279 c.Check(in[i], Equals, buffer[i])
283 n, err := br1.Read(in)
285 c.Check(n, Equals, 36)
286 c.Check(err, Equals, nil)
288 for i := 0; i < 36; i += 1 {
289 c.Check(in[i], Equals, buffer[64+i])
293 n, err := br1.Read(in)
295 c.Check(n, Equals, 0)
296 c.Check(err, Equals, io.EOF)
300 func (s *StandaloneSuite) TestTransferIoCopy(c *C) {
301 // Buffer for reads from 'r'
302 buffer := make([]byte, 100)
303 for i := 0; i < 100; i += 1 {
307 tr := AsyncStreamFromSlice(buffer)
310 br1 := tr.MakeStreamReader()
313 reader, writer := io.Pipe()
316 p := make([]byte, 100)
317 n, err := reader.Read(p)
318 c.Check(n, Equals, 100)
319 c.Check(err, Equals, nil)
320 c.Check(p, DeepEquals, buffer)
326 func (s *StandaloneSuite) TestManyReaders(c *C) {
327 reader, writer := io.Pipe()
329 tr := AsyncStreamFromReader(512, reader)
332 sr := tr.MakeStreamReader()
334 time.Sleep(100 * time.Millisecond)
338 for i := 0; i < 200; i += 1 {
340 br1 := tr.MakeStreamReader()
344 n, err := br1.Read(p)
345 c.Check(n, Equals, 3)
346 c.Check(p[0:3], DeepEquals, []byte("foo"))
349 c.Check(n, Equals, 3)
350 c.Check(p[0:3], DeepEquals, []byte("bar"))
353 c.Check(n, Equals, 3)
354 c.Check(p[0:3], DeepEquals, []byte("baz"))
357 c.Check(n, Equals, 0)
358 c.Check(err, Equals, io.EOF)
362 writer.Write([]byte("foo"))
363 writer.Write([]byte("bar"))
364 writer.Write([]byte("baz"))