Skip to content

Commit 034d846

Browse files
[TESTING] Improved testing of HEAR handler slightly (- WIP #241 -)
Changes in file tests/test_hear_data_processing.py: * improved mocking of handler tests to ensure
1 parent 77ec941 commit 034d846

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

tests/test_hear_data_processing.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@
3939
@context.markWithMetaTag("mat", "hear")
4040
class RecvDataProcessingTestSuite(context.BasicUsageTestSuite):
4141
"""
42-
A test suite that checks empty data with the multicast sender and receiver.
42+
A test suite that validates the multicast sender and receiver's handling of empty data.
4343
44+
Test cases:
45+
- Sending empty binary data.
46+
- Sending empty data followed by a stop command.
4447
"""
4548

4649
__module__ = "tests.test_hear_data_processing"
@@ -55,16 +58,18 @@ def test_multicast_sender_with_no_data(self) -> None:
5558
theResult = False
5659
fail_fixture = "SAY -X] RECV? != error"
5760
_fixture_port_num = self._always_generate_random_port_WHEN_called()
61+
_fixture_mcast_addr = "224.0.0.1"
5862
try:
5963
self.assertIsNotNone(_fixture_port_num)
6064
self.assertIsInstance(_fixture_port_num, int)
65+
self.assertIsNotNone(_fixture_mcast_addr)
6166
_fixture_HEAR_args = [
6267
"--port",
6368
str(_fixture_port_num),
6469
"--groups",
65-
"'224.0.0.1'",
70+
f"'{_fixture_mcast_addr}'",
6671
"--group",
67-
"'224.0.0.1'",
72+
f"'{_fixture_mcast_addr}'",
6873
]
6974
p = Process(
7075
target=multicast.__main__.main, name="RECV", args=(
@@ -77,7 +82,7 @@ def test_multicast_sender_with_no_data(self) -> None:
7782
try:
7883
sender = multicast.send.McastSAY()
7984
self.assertIsNotNone(sender)
80-
sender(group='224.0.0.1', port=_fixture_port_num, ttl=1, data=b'')
85+
sender(group=_fixture_mcast_addr, port=_fixture_port_num, ttl=1, data=b'')
8186
self.assertIsNotNone(p)
8287
self.assertTrue(p.is_alive(), fail_fixture)
8388
except Exception as _cause:
@@ -105,16 +110,17 @@ def test_multicast_sender_with_no_data_before_follow_by_stop(self) -> None:
105110
theResult = False
106111
fail_fixture = "SAY -X] HEAR? != error"
107112
_fixture_port_num = self._always_generate_random_port_WHEN_called()
113+
_fixture_mcast_addr = "224.0.0.1"
108114
try:
109115
self.assertIsNotNone(_fixture_port_num)
110116
self.assertIsInstance(_fixture_port_num, int)
111117
_fixture_HEAR_args = [
112118
"--port",
113119
str(_fixture_port_num),
114120
"--groups",
115-
"'224.0.0.1'",
121+
f"'{_fixture_mcast_addr}'",
116122
"--group",
117-
"'224.0.0.1'",
123+
f"'{_fixture_mcast_addr}'",
118124
]
119125
p = Process(
120126
target=multicast.__main__.main,
@@ -130,11 +136,11 @@ def test_multicast_sender_with_no_data_before_follow_by_stop(self) -> None:
130136
try:
131137
sender = multicast.send.McastSAY()
132138
self.assertIsNotNone(sender)
133-
sender(group='224.0.0.1', port=_fixture_port_num, ttl=1, data=b'')
139+
sender(group=_fixture_mcast_addr, port=_fixture_port_num, ttl=1, data=b'')
134140
self.assertIsNotNone(p)
135141
self.assertTrue(p.is_alive(), fail_fixture)
136142
while p.is_alive():
137-
sender(group="224.0.0.1", port=_fixture_port_num, data=["STOP"])
143+
sender(group=_fixture_mcast_addr, port=_fixture_port_num, data=["STOP"])
138144
p.join(1)
139145
self.assertFalse(p.is_alive(), "HEAR ignored STOP")
140146
except Exception as _cause:
@@ -208,11 +214,20 @@ def test_handle_with_invalid_utf8_data(self) -> None:
208214
request=(data, sock), client_address=_fixture_client_addr, server=None
209215
)
210216
try:
217+
# Mock the processing method
218+
handler._process = MagicMock()
211219
# Should silently ignore invalid UTF-8 data
212220
handler.handle() # If no exception is raised, the test passes
213221
# Verify handler state after processing invalid data
214222
self.assertIsNone(handler.server) # Server should remain None
215223
self.assertEqual(handler.client_address, _fixture_client_addr)
224+
# Verify no data was processed
225+
handler._process.assert_not_called()
226+
# Test with different invalid UTF-8 sequences
227+
for invalid_data in [b'\xff', b'\xfe\xff', b'\xff\xff\xff']:
228+
handler.request = (invalid_data, sock)
229+
handler.handle()
230+
handler._process.assert_not_called()
216231
except Exception as e:
217232
self.fail(f"Handler raised an unexpected exception: {e}")
218233
finally:

0 commit comments

Comments
 (0)