@@ -40,6 +40,7 @@ def setUp(self):
40
40
self .loop ._proactor = self .proactor
41
41
self .protocol = test_utils .make_test_protocol (asyncio .Protocol )
42
42
self .sock = mock .Mock (socket .socket )
43
+ self .buffer_size = 32768
43
44
44
45
def socket_transport (self , waiter = None ):
45
46
transport = _ProactorSocketTransport (self .loop , self .sock ,
@@ -53,41 +54,45 @@ def test_ctor(self):
53
54
test_utils .run_briefly (self .loop )
54
55
self .assertIsNone (fut .result ())
55
56
self .protocol .connection_made (tr )
56
- self .proactor .recv .assert_called_with (self .sock , 32768 )
57
+ self .proactor .recv_into .assert_called_with (self .sock , bytearray ( self . buffer_size ) )
57
58
58
59
def test_loop_reading (self ):
59
60
tr = self .socket_transport ()
60
61
tr ._loop_reading ()
61
- self .loop ._proactor .recv .assert_called_with (self .sock , 32768 )
62
+ self .loop ._proactor .recv_into .assert_called_with (self .sock , bytearray ( self . buffer_size ) )
62
63
self .assertFalse (self .protocol .data_received .called )
63
64
self .assertFalse (self .protocol .eof_received .called )
64
65
65
66
def test_loop_reading_data (self ):
67
+ buf = b'data'
66
68
res = self .loop .create_future ()
67
- res .set_result (b'data' )
69
+ res .set_result (len ( buf ) )
68
70
69
71
tr = self .socket_transport ()
70
72
tr ._read_fut = res
73
+ tr ._data [:len (buf )] = buf
71
74
tr ._loop_reading (res )
72
- self .loop ._proactor .recv .assert_called_with (self .sock , 32768 )
73
- self .protocol .data_received .assert_called_with (b'data' )
75
+ called_buf = bytearray (self .buffer_size )
76
+ called_buf [:len (buf )] = buf
77
+ self .loop ._proactor .recv_into .assert_called_with (self .sock , called_buf )
78
+ self .protocol .data_received .assert_called_with (bytearray (buf ))
74
79
75
80
def test_loop_reading_no_data (self ):
76
81
res = self .loop .create_future ()
77
- res .set_result (b'' )
82
+ res .set_result (0 )
78
83
79
84
tr = self .socket_transport ()
80
85
self .assertRaises (AssertionError , tr ._loop_reading , res )
81
86
82
87
tr .close = mock .Mock ()
83
88
tr ._read_fut = res
84
89
tr ._loop_reading (res )
85
- self .assertFalse (self .loop ._proactor .recv .called )
90
+ self .assertFalse (self .loop ._proactor .recv_into .called )
86
91
self .assertTrue (self .protocol .eof_received .called )
87
92
self .assertTrue (tr .close .called )
88
93
89
94
def test_loop_reading_aborted (self ):
90
- err = self .loop ._proactor .recv .side_effect = ConnectionAbortedError ()
95
+ err = self .loop ._proactor .recv_into .side_effect = ConnectionAbortedError ()
91
96
92
97
tr = self .socket_transport ()
93
98
tr ._fatal_error = mock .Mock ()
@@ -97,7 +102,7 @@ def test_loop_reading_aborted(self):
97
102
'Fatal read error on pipe transport' )
98
103
99
104
def test_loop_reading_aborted_closing (self ):
100
- self .loop ._proactor .recv .side_effect = ConnectionAbortedError ()
105
+ self .loop ._proactor .recv_into .side_effect = ConnectionAbortedError ()
101
106
102
107
tr = self .socket_transport ()
103
108
tr ._closing = True
@@ -106,15 +111,15 @@ def test_loop_reading_aborted_closing(self):
106
111
self .assertFalse (tr ._fatal_error .called )
107
112
108
113
def test_loop_reading_aborted_is_fatal (self ):
109
- self .loop ._proactor .recv .side_effect = ConnectionAbortedError ()
114
+ self .loop ._proactor .recv_into .side_effect = ConnectionAbortedError ()
110
115
tr = self .socket_transport ()
111
116
tr ._closing = False
112
117
tr ._fatal_error = mock .Mock ()
113
118
tr ._loop_reading ()
114
119
self .assertTrue (tr ._fatal_error .called )
115
120
116
121
def test_loop_reading_conn_reset_lost (self ):
117
- err = self .loop ._proactor .recv .side_effect = ConnectionResetError ()
122
+ err = self .loop ._proactor .recv_into .side_effect = ConnectionResetError ()
118
123
119
124
tr = self .socket_transport ()
120
125
tr ._closing = False
@@ -125,7 +130,7 @@ def test_loop_reading_conn_reset_lost(self):
125
130
tr ._force_close .assert_called_with (err )
126
131
127
132
def test_loop_reading_exception (self ):
128
- err = self .loop ._proactor .recv .side_effect = (OSError ())
133
+ err = self .loop ._proactor .recv_into .side_effect = (OSError ())
129
134
130
135
tr = self .socket_transport ()
131
136
tr ._fatal_error = mock .Mock ()
@@ -351,44 +356,55 @@ def test_write_eof_duplex_pipe(self):
351
356
352
357
def test_pause_resume_reading (self ):
353
358
tr = self .socket_transport ()
354
- futures = []
355
- for msg in [b'data1' , b'data2' , b'data3' , b'data4' , b'data5' , b'' ]:
359
+ index = 0
360
+ msgs = [b'data1' , b'data2' , b'data3' , b'data4' , b'data5' , b'' ]
361
+ reversed_msgs = list (reversed (msgs ))
362
+
363
+ def recv_into (sock , data ):
356
364
f = self .loop .create_future ()
357
- f .set_result (msg )
358
- futures .append (f )
365
+ msg = reversed_msgs .pop ()
366
+
367
+ result = f .result
368
+ def monkey ():
369
+ data [:len (msg )] = msg
370
+ return result ()
371
+ f .result = monkey
372
+
373
+ f .set_result (len (msg ))
374
+ return f
359
375
360
- self .loop ._proactor .recv .side_effect = futures
376
+ self .loop ._proactor .recv_into .side_effect = recv_into
361
377
self .loop ._run_once ()
362
378
self .assertFalse (tr ._paused )
363
379
self .assertTrue (tr .is_reading ())
364
- self . loop . _run_once ()
365
- self . protocol . data_received . assert_called_with ( b'data1' )
366
- self .loop ._run_once ()
367
- self .protocol .data_received .assert_called_with (b'data2' )
380
+
381
+ for msg in msgs [: 2 ]:
382
+ self .loop ._run_once ()
383
+ self .protocol .data_received .assert_called_with (bytearray ( msg ) )
368
384
369
385
tr .pause_reading ()
370
386
tr .pause_reading ()
371
387
self .assertTrue (tr ._paused )
372
388
self .assertFalse (tr .is_reading ())
373
389
for i in range (10 ):
374
390
self .loop ._run_once ()
375
- self .protocol .data_received .assert_called_with (b'data2' )
391
+ self .protocol .data_received .assert_called_with (bytearray ( msgs [ 1 ]) )
376
392
377
393
tr .resume_reading ()
378
394
tr .resume_reading ()
379
395
self .assertFalse (tr ._paused )
380
396
self .assertTrue (tr .is_reading ())
381
- self . loop . _run_once ()
382
- self . protocol . data_received . assert_called_with ( b'data3' )
383
- self .loop ._run_once ()
384
- self .protocol .data_received .assert_called_with (b'data4' )
397
+
398
+ for msg in msgs [ 2 : 4 ]:
399
+ self .loop ._run_once ()
400
+ self .protocol .data_received .assert_called_with (bytearray ( msg ) )
385
401
386
402
tr .pause_reading ()
387
403
tr .resume_reading ()
388
404
self .loop .call_exception_handler = mock .Mock ()
389
405
self .loop ._run_once ()
390
406
self .loop .call_exception_handler .assert_not_called ()
391
- self .protocol .data_received .assert_called_with (b'data5' )
407
+ self .protocol .data_received .assert_called_with (bytearray ( msgs [ 4 ]) )
392
408
tr .close ()
393
409
394
410
self .assertFalse (tr .is_reading ())
0 commit comments