Skip to content

Commit d64fc74

Browse files
committed
Noah + Copilot review
1 parent cdc07e3 commit d64fc74

2 files changed

Lines changed: 27 additions & 21 deletions

File tree

test/asynchronous/test_network_layer.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_delegates_to_sock_sendall(self):
5959

6060

6161
class TestNetworkingInterfaceBase(AsyncUnitTest):
62-
def setUp(self):
62+
async def asyncSetUp(self):
6363
self.base = NetworkingInterfaceBase(MagicMock())
6464

6565
def test_gettimeout_raises(self):
@@ -181,7 +181,7 @@ async def test_default_timeout_is_none(self):
181181
self.assertIsNone(protocol.gettimeout)
182182

183183
async def test_normal_op_msg(self):
184-
header = _make_header(32, 1, 99, 2013)
184+
header = _make_header(length=32, request_id=1, response_to=99, op_code=2013)
185185
protocol = await self._make_proto_with_header(header)
186186
body_len, op_code, response_to, expecting_compression = protocol.process_header()
187187
self.assertEqual(body_len, 16)
@@ -190,41 +190,45 @@ async def test_normal_op_msg(self):
190190
self.assertFalse(expecting_compression)
191191

192192
async def test_op_compressed(self):
193-
# OP_COMPRESSED=2012, length=35 → adjusted=35-9=26 → body=26-16=10
194-
header = _make_header(35, 1, 0, 2012)
193+
# OP_COMPRESSED=2012; process_header strips the 9-byte compression sub-header
194+
# (op code + uncompressed size + compressor id), then the 16-byte standard header.
195+
# length=35 → after compression sub-header: 26 → body: 10
196+
header = _make_header(length=35, request_id=1, response_to=0, op_code=2012)
195197
protocol = await self._make_proto_with_header(header)
196198
body_len, op_code, _response_to, expecting_compression = protocol.process_header()
197199
self.assertEqual(body_len, 10)
198200
self.assertEqual(op_code, 2012)
199201
self.assertTrue(expecting_compression)
200202

201203
async def test_op_compressed_length_too_small_raises(self):
202-
header = _make_header(25, 1, 0, 2012)
204+
header = _make_header(length=25, request_id=1, response_to=0, op_code=2012)
203205
protocol = await self._make_proto_with_header(header)
204206
with self.assertRaises(ProtocolError):
205207
protocol.process_header()
206208

207209
async def test_non_compressed_length_too_small_raises(self):
208-
header = _make_header(16, 1, 0, 2013)
210+
header = _make_header(length=16, request_id=1, response_to=0, op_code=2013)
209211
protocol = await self._make_proto_with_header(header)
210212
with self.assertRaises(ProtocolError):
211213
protocol.process_header()
212214

213215
async def test_length_exceeds_max_raises(self):
214-
header = _make_header(MAX_MESSAGE_SIZE + 1, 1, 0, 2013)
216+
header = _make_header(
217+
length=MAX_MESSAGE_SIZE + 1, request_id=1, response_to=0, op_code=2013
218+
)
215219
protocol = await self._make_proto_with_header(header)
216220
with self.assertRaises(ProtocolError):
217221
protocol.process_header()
218222

219223
async def test_op_reply_op_code(self):
220-
header = _make_header(20, 0, 0, 1)
224+
header = _make_header(length=20, request_id=0, response_to=0, op_code=1)
221225
protocol = await self._make_proto_with_header(header)
222226
body_len, op_code, _response_to, expecting_compression = protocol.process_header()
223227
self.assertEqual(body_len, 4)
224228
self.assertEqual(op_code, 1)
225229
self.assertFalse(expecting_compression)
226230

227-
async def test_compression_header_returns_op_code_and_compressor_id(self):
231+
async def test_compression_header_snappy_compressor_id(self):
228232
protocol = await _make_protocol()
229233
# <iiB: little-endian, i32 op code=2013, i32 uncompressed size=0, u8 compressor id=1 (snappy)
230234
data = struct.pack("<iiB", 2013, 0, 1)
@@ -277,9 +281,8 @@ async def test_close_with_exception_propagates_to_pending(self):
277281
protocol._pending_messages.append(future)
278282
exc = OSError("connection reset")
279283
protocol.close(exc)
280-
with self.assertRaises(OSError) as ctx:
284+
with self.assertRaisesRegex(OSError, "connection reset"):
281285
await future
282-
self.assertIn("connection reset", str(ctx.exception))
283286

284287
class TestAsyncSocketReceive(AsyncUnitTest):
285288
async def test_reads_full_data_in_one_call(self):

test/test_network_layer.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_default_timeout_is_none(self):
180180
self.assertIsNone(protocol.gettimeout)
181181

182182
def test_normal_op_msg(self):
183-
header = _make_header(32, 1, 99, 2013)
183+
header = _make_header(length=32, request_id=1, response_to=99, op_code=2013)
184184
protocol = self._make_proto_with_header(header)
185185
body_len, op_code, response_to, expecting_compression = protocol.process_header()
186186
self.assertEqual(body_len, 16)
@@ -189,41 +189,45 @@ def test_normal_op_msg(self):
189189
self.assertFalse(expecting_compression)
190190

191191
def test_op_compressed(self):
192-
# OP_COMPRESSED=2012, length=35 → adjusted=35-9=26 → body=26-16=10
193-
header = _make_header(35, 1, 0, 2012)
192+
# OP_COMPRESSED=2012; process_header strips the 9-byte compression sub-header
193+
# (op code + uncompressed size + compressor id), then the 16-byte standard header.
194+
# length=35 → after compression sub-header: 26 → body: 10
195+
header = _make_header(length=35, request_id=1, response_to=0, op_code=2012)
194196
protocol = self._make_proto_with_header(header)
195197
body_len, op_code, _response_to, expecting_compression = protocol.process_header()
196198
self.assertEqual(body_len, 10)
197199
self.assertEqual(op_code, 2012)
198200
self.assertTrue(expecting_compression)
199201

200202
def test_op_compressed_length_too_small_raises(self):
201-
header = _make_header(25, 1, 0, 2012)
203+
header = _make_header(length=25, request_id=1, response_to=0, op_code=2012)
202204
protocol = self._make_proto_with_header(header)
203205
with self.assertRaises(ProtocolError):
204206
protocol.process_header()
205207

206208
def test_non_compressed_length_too_small_raises(self):
207-
header = _make_header(16, 1, 0, 2013)
209+
header = _make_header(length=16, request_id=1, response_to=0, op_code=2013)
208210
protocol = self._make_proto_with_header(header)
209211
with self.assertRaises(ProtocolError):
210212
protocol.process_header()
211213

212214
def test_length_exceeds_max_raises(self):
213-
header = _make_header(MAX_MESSAGE_SIZE + 1, 1, 0, 2013)
215+
header = _make_header(
216+
length=MAX_MESSAGE_SIZE + 1, request_id=1, response_to=0, op_code=2013
217+
)
214218
protocol = self._make_proto_with_header(header)
215219
with self.assertRaises(ProtocolError):
216220
protocol.process_header()
217221

218222
def test_op_reply_op_code(self):
219-
header = _make_header(20, 0, 0, 1)
223+
header = _make_header(length=20, request_id=0, response_to=0, op_code=1)
220224
protocol = self._make_proto_with_header(header)
221225
body_len, op_code, _response_to, expecting_compression = protocol.process_header()
222226
self.assertEqual(body_len, 4)
223227
self.assertEqual(op_code, 1)
224228
self.assertFalse(expecting_compression)
225229

226-
def test_compression_header_returns_op_code_and_compressor_id(self):
230+
def test_compression_header_snappy_compressor_id(self):
227231
protocol = _make_protocol()
228232
# <iiB: little-endian, i32 op code=2013, i32 uncompressed size=0, u8 compressor id=1 (snappy)
229233
data = struct.pack("<iiB", 2013, 0, 1)
@@ -276,9 +280,8 @@ def test_close_with_exception_propagates_to_pending(self):
276280
protocol._pending_messages.append(future)
277281
exc = OSError("connection reset")
278282
protocol.close(exc)
279-
with self.assertRaises(OSError) as ctx:
283+
with self.assertRaisesRegex(OSError, "connection reset"):
280284
future
281-
self.assertIn("connection reset", str(ctx.exception))
282285

283286
class TestAsyncSocketReceive(UnitTest):
284287
def test_reads_full_data_in_one_call(self):

0 commit comments

Comments
 (0)