2222from __future__ import annotations
2323
2424import sys
25- from unittest .mock import MagicMock , patch
25+ from unittest .mock import MagicMock
2626
2727sys .path [0 :0 ] = ["" ]
2828
@@ -43,47 +43,52 @@ def _make_conn():
4343 return conn
4444
4545
46+ def _mock_recv_into (conn , * chunks : bytes ) -> None :
47+ # Scope the mock to this conn's recv_into to avoid races
48+ # with SDAM monitor threads owned by the shared test client
49+ it = iter (chunks )
50+
51+ def _recv_into (buf ) -> int :
52+ chunk = next (it )
53+ buf [: len (chunk )] = chunk
54+ return len (chunk )
55+
56+ conn .conn .recv_into .side_effect = _recv_into
57+
58+
4659class TestReceiveMessage (UnitTest ):
4760 def test_request_id_mismatch_raises (self ):
48- with patch .object (
49- network_layer ,
50- "receive_data" ,
51- return_value = pack_msg_header (length = 32 , request_id = 0 , response_to = 99 , op_code = 2013 ),
52- ):
53- with self .assertRaisesRegex (ProtocolError , "Got response id" ):
54- network_layer .receive_message (_make_conn (), request_id = 1 )
61+ conn = _make_conn ()
62+ _mock_recv_into (
63+ conn , pack_msg_header (length = 32 , request_id = 0 , response_to = 99 , op_code = 2013 )
64+ )
65+ with self .assertRaisesRegex (ProtocolError , "Got response id" ):
66+ network_layer .receive_message (conn , request_id = 1 )
5567
5668 def test_length_too_small_raises (self ):
57- with patch .object (
58- network_layer ,
59- "receive_data" ,
60- return_value = pack_msg_header (length = 16 , request_id = 0 , response_to = 0 , op_code = 2013 ),
61- ):
62- with self .assertRaisesRegex (ProtocolError , "not longer than standard message header" ):
63- network_layer .receive_message (_make_conn (), request_id = None )
69+ conn = _make_conn ()
70+ _mock_recv_into (conn , pack_msg_header (length = 16 , request_id = 0 , response_to = 0 , op_code = 2013 ))
71+ with self .assertRaisesRegex (ProtocolError , "not longer than standard message header" ):
72+ network_layer .receive_message (conn , request_id = None )
6473
6574 def test_length_exceeds_max_raises (self ):
66- with patch .object (
67- network_layer ,
68- "receive_data" ,
69- return_value = pack_msg_header (
70- length = MAX_MESSAGE_SIZE + 1 , request_id = 0 , response_to = 0 , op_code = 2013
71- ),
72- ):
73- with self .assertRaisesRegex (ProtocolError , "larger than server max" ):
74- network_layer .receive_message (_make_conn (), request_id = None )
75+ conn = _make_conn ()
76+ _mock_recv_into (
77+ conn ,
78+ pack_msg_header (length = MAX_MESSAGE_SIZE + 1 , request_id = 0 , response_to = 0 , op_code = 2013 ),
79+ )
80+ with self .assertRaisesRegex (ProtocolError , "larger than server max" ):
81+ network_layer .receive_message (conn , request_id = None )
7582
7683 def test_unknown_opcode_raises (self ):
77- with patch .object (
78- network_layer ,
79- "receive_data" ,
80- side_effect = [
81- pack_msg_header (length = 20 , request_id = 0 , response_to = 0 , op_code = 9999 ),
82- b"data" ,
83- ],
84- ):
85- with self .assertRaisesRegex (ProtocolError , "Got opcode" ):
86- network_layer .receive_message (_make_conn (), request_id = None )
84+ conn = _make_conn ()
85+ _mock_recv_into (
86+ conn ,
87+ pack_msg_header (length = 20 , request_id = 0 , response_to = 0 , op_code = 9999 ),
88+ b"data" ,
89+ )
90+ with self .assertRaisesRegex (ProtocolError , "Got opcode" ):
91+ network_layer .receive_message (conn , request_id = None )
8792
8893
8994class TestReceiveData (UnitTest ):
0 commit comments