4040
4141
4242async def _make_protocol (timeout = None ):
43- proto = PyMongoProtocol (timeout = timeout )
43+ protocol = PyMongoProtocol (timeout = timeout )
4444 mock_transport = MagicMock ()
4545 mock_transport .is_closing .return_value = False
46- proto .transport = mock_transport
47- return proto
46+ protocol .transport = mock_transport
47+ return protocol
4848
4949
5050def _make_header (length , request_id , response_to , op_code ):
@@ -53,9 +53,9 @@ def _make_header(length, request_id, response_to, op_code):
5353
5454class TestSendall (AsyncUnitTest ):
5555 def test_delegates_to_sock_sendall (self ):
56- mock_sock = MagicMock ()
57- sendall (mock_sock , b"hello" )
58- mock_sock .sendall .assert_called_once_with (b"hello" )
56+ mock_socket = MagicMock ()
57+ sendall (mock_socket , b"hello" )
58+ mock_socket .sendall .assert_called_once_with (b"hello" )
5959
6060
6161class TestNetworkingInterfaceBase (AsyncUnitTest ):
@@ -89,218 +89,218 @@ def test_sock_raises(self):
8989
9090class TestNetworkingInterface (AsyncUnitTest ):
9191 def setUp (self ):
92- self .mock_sock = MagicMock ()
93- self .iface = NetworkingInterface (self .mock_sock )
92+ self .mock_socket = MagicMock ()
93+ self .network_interface = NetworkingInterface (self .mock_socket )
9494
9595 def test_gettimeout_delegates (self ):
96- self .mock_sock .gettimeout .return_value = 5.0
97- self .assertEqual (self .iface .gettimeout (), 5.0 )
96+ self .mock_socket .gettimeout .return_value = 5.0
97+ self .assertEqual (self .network_interface .gettimeout (), 5.0 )
9898
9999 def test_settimeout_delegates (self ):
100- self .iface .settimeout (3.0 )
101- self .mock_sock .settimeout .assert_called_once_with (3.0 )
100+ self .network_interface .settimeout (3.0 )
101+ self .mock_socket .settimeout .assert_called_once_with (3.0 )
102102
103103 def test_close_delegates (self ):
104- self .iface .close ()
105- self .mock_sock .close .assert_called_once ()
104+ self .network_interface .close ()
105+ self .mock_socket .close .assert_called_once ()
106106
107107 def test_is_closing_delegates (self ):
108- self .mock_sock .is_closing .return_value = True
109- self .assertTrue (self .iface .is_closing ())
108+ self .mock_socket .is_closing .return_value = True
109+ self .assertTrue (self .network_interface .is_closing ())
110110
111111 def test_fileno_delegates (self ):
112- self .mock_sock .fileno .return_value = 42
113- self .assertEqual (self .iface .fileno (), 42 )
112+ self .mock_socket .fileno .return_value = 42
113+ self .assertEqual (self .network_interface .fileno (), 42 )
114114
115115 def test_recv_into_delegates (self ):
116116 buf = memoryview (bytearray (10 ))
117- self .mock_sock .recv_into .return_value = 7
118- result = self .iface .recv_into (buf )
117+ self .mock_socket .recv_into .return_value = 7
118+ result = self .network_interface .recv_into (buf )
119119 self .assertEqual (result , 7 )
120- self .mock_sock .recv_into .assert_called_once_with (buf )
120+ self .mock_socket .recv_into .assert_called_once_with (buf )
121121
122122 def test_get_conn_returns_socket (self ):
123- self .assertIs (self .iface .get_conn , self .mock_sock )
123+ self .assertIs (self .network_interface .get_conn , self .mock_socket )
124124
125125 def test_sock_returns_socket (self ):
126- self .assertIs (self .iface .sock , self .mock_sock )
126+ self .assertIs (self .network_interface .sock , self .mock_socket )
127127
128128
129129if not _IS_SYNC :
130130
131131 class TestAsyncNetworkingInterface (AsyncUnitTest ):
132- def _make_iface (self ):
132+ def _make_network_interface (self ):
133133 mock_transport = MagicMock ()
134134 mock_protocol = MagicMock ()
135135 mock_protocol .gettimeout = 10.0
136136 return AsyncNetworkingInterface ((mock_transport , mock_protocol ))
137137
138138 def test_gettimeout_returns_protocol_timeout (self ):
139- iface = self ._make_iface ()
140- self .assertEqual (iface .gettimeout , 10.0 )
139+ network_interface = self ._make_network_interface ()
140+ self .assertEqual (network_interface .gettimeout , 10.0 )
141141
142142 def test_settimeout_delegates_to_protocol (self ):
143- iface = self ._make_iface ()
144- iface .settimeout (7.0 )
145- iface .conn [1 ].settimeout .assert_called_once_with (7.0 )
143+ network_interface = self ._make_network_interface ()
144+ network_interface .settimeout (7.0 )
145+ network_interface .conn [1 ].settimeout .assert_called_once_with (7.0 )
146146
147147 def test_is_closing_delegates_to_transport (self ):
148- iface = self ._make_iface ()
149- iface .conn [0 ].is_closing .return_value = False
150- self .assertFalse (iface .is_closing ())
148+ network_interface = self ._make_network_interface ()
149+ network_interface .conn [0 ].is_closing .return_value = False
150+ self .assertFalse (network_interface .is_closing ())
151151
152152 def test_get_conn_returns_protocol (self ):
153- iface = self ._make_iface ()
154- self .assertIs (iface .get_conn , iface .conn [1 ])
153+ network_interface = self ._make_network_interface ()
154+ self .assertIs (network_interface .get_conn , network_interface .conn [1 ])
155155
156156 def test_sock_returns_transport_socket (self ):
157- iface = self ._make_iface ()
157+ network_interface = self ._make_network_interface ()
158158 sentinel = object ()
159- iface .conn [0 ].get_extra_info .return_value = sentinel
160- self .assertIs (iface .sock , sentinel )
161- iface .conn [0 ].get_extra_info .assert_called_once_with ("socket" )
159+ network_interface .conn [0 ].get_extra_info .return_value = sentinel
160+ self .assertIs (network_interface .sock , sentinel )
161+ network_interface .conn [0 ].get_extra_info .assert_called_once_with ("socket" )
162162
163163 class TestPyMongoProtocol (AsyncUnitTest ):
164164 async def _make_proto_with_header (self , header_bytes , max_size = MAX_MESSAGE_SIZE ):
165- proto = await _make_protocol ()
166- proto ._max_message_size = max_size
167- proto ._header = memoryview (bytearray (header_bytes ))
168- return proto
165+ protocol = await _make_protocol ()
166+ protocol ._max_message_size = max_size
167+ protocol ._header = memoryview (bytearray (header_bytes ))
168+ return protocol
169169
170170 async def test_initial_timeout_from_constructor (self ):
171- proto = await _make_protocol (timeout = 3.0 )
172- self .assertEqual (proto .gettimeout , 3.0 )
171+ protocol = await _make_protocol (timeout = 3.0 )
172+ self .assertEqual (protocol .gettimeout , 3.0 )
173173
174174 async def test_settimeout_updates_value (self ):
175- proto = await _make_protocol ()
176- proto .settimeout (7.5 )
177- self .assertEqual (proto .gettimeout , 7.5 )
175+ protocol = await _make_protocol ()
176+ protocol .settimeout (7.5 )
177+ self .assertEqual (protocol .gettimeout , 7.5 )
178178
179179 async def test_default_timeout_is_none (self ):
180- proto = await _make_protocol ()
181- self .assertIsNone (proto .gettimeout )
180+ protocol = await _make_protocol ()
181+ self .assertIsNone (protocol .gettimeout )
182182
183183 async def test_normal_op_msg (self ):
184- hdr = _make_header (32 , 1 , 99 , 2013 )
185- proto = await self ._make_proto_with_header (hdr )
186- body_len , op_code , response_to , expecting_compression = proto .process_header ()
184+ header = _make_header (32 , 1 , 99 , 2013 )
185+ protocol = await self ._make_proto_with_header (header )
186+ body_len , op_code , response_to , expecting_compression = protocol .process_header ()
187187 self .assertEqual (body_len , 16 )
188188 self .assertEqual (op_code , 2013 )
189189 self .assertEqual (response_to , 99 )
190190 self .assertFalse (expecting_compression )
191191
192192 async def test_op_compressed (self ):
193193 # OP_COMPRESSED=2012, length=35 → adjusted=35-9=26 → body=26-16=10
194- hdr = _make_header (35 , 1 , 0 , 2012 )
195- proto = await self ._make_proto_with_header (hdr )
196- body_len , op_code , _response_to , expecting_compression = proto .process_header ()
194+ header = _make_header (35 , 1 , 0 , 2012 )
195+ protocol = await self ._make_proto_with_header (header )
196+ body_len , op_code , _response_to , expecting_compression = protocol .process_header ()
197197 self .assertEqual (body_len , 10 )
198198 self .assertEqual (op_code , 2012 )
199199 self .assertTrue (expecting_compression )
200200
201201 async def test_op_compressed_length_too_small_raises (self ):
202- hdr = _make_header (25 , 1 , 0 , 2012 )
203- proto = await self ._make_proto_with_header (hdr )
202+ header = _make_header (25 , 1 , 0 , 2012 )
203+ protocol = await self ._make_proto_with_header (header )
204204 with self .assertRaises (ProtocolError ):
205- proto .process_header ()
205+ protocol .process_header ()
206206
207207 async def test_non_compressed_length_too_small_raises (self ):
208- hdr = _make_header (16 , 1 , 0 , 2013 )
209- proto = await self ._make_proto_with_header (hdr )
208+ header = _make_header (16 , 1 , 0 , 2013 )
209+ protocol = await self ._make_proto_with_header (header )
210210 with self .assertRaises (ProtocolError ):
211- proto .process_header ()
211+ protocol .process_header ()
212212
213213 async def test_length_exceeds_max_raises (self ):
214- hdr = _make_header (MAX_MESSAGE_SIZE + 1 , 1 , 0 , 2013 )
215- proto = await self ._make_proto_with_header (hdr )
214+ header = _make_header (MAX_MESSAGE_SIZE + 1 , 1 , 0 , 2013 )
215+ protocol = await self ._make_proto_with_header (header )
216216 with self .assertRaises (ProtocolError ):
217- proto .process_header ()
217+ protocol .process_header ()
218218
219219 async def test_op_reply_op_code (self ):
220- hdr = _make_header (20 , 0 , 0 , 1 )
221- proto = await self ._make_proto_with_header (hdr )
222- body_len , op_code , _response_to , expecting_compression = proto .process_header ()
220+ header = _make_header (20 , 0 , 0 , 1 )
221+ protocol = await self ._make_proto_with_header (header )
222+ body_len , op_code , _response_to , expecting_compression = protocol .process_header ()
223223 self .assertEqual (body_len , 4 )
224224 self .assertEqual (op_code , 1 )
225225 self .assertFalse (expecting_compression )
226226
227227 async def test_compression_header_returns_op_code_and_compressor_id (self ):
228- proto = await _make_protocol ()
228+ protocol = await _make_protocol ()
229229 # op_code=2013, uncompressed_size=0, compressor_id=1 (snappy)
230230 data = struct .pack ("<iiB" , 2013 , 0 , 1 )
231- proto ._compression_header = memoryview (bytearray (data ))
232- op_code , compressor_id = proto .process_compression_header ()
231+ protocol ._compression_header = memoryview (bytearray (data ))
232+ op_code , compressor_id = protocol .process_compression_header ()
233233 self .assertEqual (op_code , 2013 )
234234 self .assertEqual (compressor_id , 1 )
235235
236236 async def test_compression_header_zlib_compressor_id (self ):
237- proto = await _make_protocol ()
237+ protocol = await _make_protocol ()
238238 data = struct .pack ("<iiB" , 2013 , 0 , 2 )
239- proto ._compression_header = memoryview (bytearray (data ))
240- _ , compressor_id = proto .process_compression_header ()
239+ protocol ._compression_header = memoryview (bytearray (data ))
240+ _ , compressor_id = protocol .process_compression_header ()
241241 self .assertEqual (compressor_id , 2 )
242242
243243 async def test_message_complete_resolves_pending_future (self ):
244- proto = await _make_protocol ()
245- proto ._expecting_header = False
246- proto ._expecting_compression = False
247- proto ._message_size = 10
248- proto ._message = memoryview (bytearray (10 ))
249- proto ._message_index = 0
250- proto ._op_code = 2013
251- proto ._compressor_id = None
252- proto ._response_to = 42
253-
254- fut = asyncio .get_running_loop ().create_future ()
255- proto ._pending_messages .append (fut )
256-
257- proto .buffer_updated (10 )
258- self .assertTrue (fut .done ())
259- op_code , compressor_id , response_to , _ = fut .result ()
244+ protocol = await _make_protocol ()
245+ protocol ._expecting_header = False
246+ protocol ._expecting_compression = False
247+ protocol ._message_size = 10
248+ protocol ._message = memoryview (bytearray (10 ))
249+ protocol ._message_index = 0
250+ protocol ._op_code = 2013
251+ protocol ._compressor_id = None
252+ protocol ._response_to = 42
253+
254+ future = asyncio .get_running_loop ().create_future ()
255+ protocol ._pending_messages .append (future )
256+
257+ protocol .buffer_updated (10 )
258+ self .assertTrue (future .done ())
259+ op_code , compressor_id , response_to , _ = future .result ()
260260 self .assertEqual (op_code , 2013 )
261261 self .assertIsNone (compressor_id )
262262 self .assertEqual (response_to , 42 )
263263
264264 async def test_close_aborts_transport (self ):
265- proto = await _make_protocol ()
266- proto .close ()
267- self .assertTrue (proto .transport .abort .called )
265+ protocol = await _make_protocol ()
266+ protocol .close ()
267+ self .assertTrue (protocol .transport .abort .called )
268268
269269 async def test_connection_lost_twice_does_not_raise (self ):
270- proto = await _make_protocol ()
271- proto .connection_lost (None )
272- proto .connection_lost (None )
270+ protocol = await _make_protocol ()
271+ protocol .connection_lost (None )
272+ protocol .connection_lost (None )
273273
274274 async def test_close_with_exception_propagates_to_pending (self ):
275- proto = await _make_protocol ()
276- fut = asyncio .get_running_loop ().create_future ()
277- proto ._pending_messages .append (fut )
275+ protocol = await _make_protocol ()
276+ future = asyncio .get_running_loop ().create_future ()
277+ protocol ._pending_messages .append (future )
278278 exc = OSError ("connection reset" )
279- proto .close (exc )
279+ protocol .close (exc )
280280 with self .assertRaises (OSError ) as ctx :
281- await fut
281+ await future
282282 self .assertIn ("connection reset" , str (ctx .exception ))
283283
284284 class TestAsyncSocketReceive (AsyncUnitTest ):
285285 async def test_reads_full_data_in_one_call (self ):
286286 data = b"hello world!"
287287 length = len (data )
288- mock_sock = MagicMock ()
288+ mock_socket = MagicMock ()
289289 loop = asyncio .get_running_loop ()
290290
291291 async def fake_recv_into (sock , buf ):
292292 buf [:length ] = data
293293 return length
294294
295295 with patch .object (loop , "sock_recv_into" , new = AsyncMock (side_effect = fake_recv_into )):
296- result = await _async_socket_receive (mock_sock , length , loop )
296+ result = await _async_socket_receive (mock_socket , length , loop )
297297 self .assertEqual (bytes (result ), data )
298298
299299 async def test_reads_data_in_multiple_chunks (self ):
300300 data = b"abcdefgh"
301301 length = len (data )
302302 chunk1 , chunk2 = data [:4 ], data [4 :]
303- mock_sock = MagicMock ()
303+ mock_socket = MagicMock ()
304304 loop = asyncio .get_running_loop ()
305305 calls = 0
306306
@@ -315,19 +315,19 @@ async def fake_recv_into(sock, buf):
315315 return len (chunk2 )
316316
317317 with patch .object (loop , "sock_recv_into" , new = AsyncMock (side_effect = fake_recv_into )):
318- result = await _async_socket_receive (mock_sock , length , loop )
318+ result = await _async_socket_receive (mock_socket , length , loop )
319319 self .assertEqual (bytes (result ), data )
320320
321321 async def test_raises_on_connection_closed (self ):
322- mock_sock = MagicMock ()
322+ mock_socket = MagicMock ()
323323 loop = asyncio .get_running_loop ()
324324
325325 async def fake_recv_into (sock , buf ):
326326 return 0
327327
328328 with patch .object (loop , "sock_recv_into" , new = AsyncMock (side_effect = fake_recv_into )):
329329 with self .assertRaises (OSError ) as ctx :
330- await _async_socket_receive (mock_sock , 10 , loop )
330+ await _async_socket_receive (mock_socket , 10 , loop )
331331 self .assertIn ("connection closed" , str (ctx .exception ))
332332
333333
0 commit comments