@@ -236,6 +236,80 @@ def test_read_limit(self):
236
236
self .assertEqual (b'chunk' , data )
237
237
self .assertEqual (b'' , stream ._buffer )
238
238
239
+ def test_readinto_empty_buffer (self ):
240
+ # Read zero bytes.
241
+ stream = asyncio .StreamReader (loop = self .loop )
242
+ stream .feed_data (self .DATA )
243
+
244
+ data = bytearray (0 )
245
+ length = self .loop .run_until_complete (stream .readinto (data ))
246
+ self .assertEqual (0 , length )
247
+ self .assertEqual (b'' , data )
248
+ self .assertEqual (self .DATA , stream ._buffer )
249
+
250
+ def test_readinto (self ):
251
+ # Read bytes.
252
+ stream = asyncio .StreamReader (loop = self .loop )
253
+ data = bytearray (30 )
254
+ read_task = self .loop .create_task (stream .readinto (data ))
255
+
256
+ def cb ():
257
+ stream .feed_data (self .DATA )
258
+ self .loop .call_soon (cb )
259
+
260
+ length = self .loop .run_until_complete (read_task )
261
+ self .assertEqual (len (self .DATA ), length )
262
+ self .assertEqual (self .DATA , data [:length ])
263
+ self .assertEqual (b'' , stream ._buffer )
264
+
265
+ def test_readinto_line_breaks (self ):
266
+ # Read bytes without line breaks.
267
+ stream = asyncio .StreamReader (loop = self .loop )
268
+ stream .feed_data (b'line1' )
269
+ stream .feed_data (b'line2' )
270
+
271
+ data = bytearray (5 )
272
+ length = self .loop .run_until_complete (stream .readinto (data ))
273
+
274
+ self .assertEqual (5 , length )
275
+ self .assertEqual (b'line1' , data )
276
+ self .assertEqual (b'line2' , stream ._buffer )
277
+
278
+ def test_readinto_eof (self ):
279
+ # Read bytes, stop at eof.
280
+ stream = asyncio .StreamReader (loop = self .loop )
281
+ data = bytearray (1024 )
282
+ read_task = self .loop .create_task (stream .readinto (data ))
283
+
284
+ def cb ():
285
+ stream .feed_eof ()
286
+ self .loop .call_soon (cb )
287
+
288
+ length = self .loop .run_until_complete (read_task )
289
+ self .assertEqual (0 , length )
290
+ self .assertEqual (b'' , stream ._buffer )
291
+
292
+ def test_readinto_exception (self ):
293
+ stream = asyncio .StreamReader (loop = self .loop )
294
+ stream .feed_data (b'line\n ' )
295
+
296
+ data = bytearray (2 )
297
+ length = self .loop .run_until_complete (stream .readinto (data ))
298
+ self .assertEqual (b'li' , data )
299
+
300
+ stream .set_exception (ValueError ())
301
+ self .assertRaises (
302
+ ValueError , self .loop .run_until_complete , stream .readinto (data ))
303
+
304
+ def test_readinto_limit (self ):
305
+ stream = asyncio .StreamReader (limit = 3 , loop = self .loop )
306
+ stream .feed_data (b'chunk' )
307
+ data = bytearray (5 )
308
+ length = self .loop .run_until_complete (stream .readinto (data ))
309
+ self .assertEqual (5 , length )
310
+ self .assertEqual (b'chunk' , data )
311
+ self .assertEqual (b'' , stream ._buffer )
312
+
239
313
def test_readline (self ):
240
314
# Read one line. 'readline' will need to wait for the data
241
315
# to come from 'cb'
0 commit comments