From 6ea870623bd47e261b4acbe652a8e7e97e0c7163 Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Mon, 24 Nov 2025 13:32:31 +0000 Subject: [PATCH] test: added socket base tests --- test/unit/test_socket_base.py | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/unit/test_socket_base.py diff --git a/test/unit/test_socket_base.py b/test/unit/test_socket_base.py new file mode 100644 index 0000000..37aabf4 --- /dev/null +++ b/test/unit/test_socket_base.py @@ -0,0 +1,55 @@ +import mock +import zmq +from robot_interface.endpoints.socket_base import SocketBase + + +def test_close_covers_both_branches(): + """ + Exercise both possible paths inside SocketBase.close(): + - when no socket exists (should just return), + - when a socket object is present (its close() method should be called). + """ + sb = SocketBase("x") + + # First check the case where socket is None. Nothing should happen here. + sb.close() + + # Now simulate a real socket so the close() call is triggered. + fake_socket = mock.Mock() + sb.socket = fake_socket + sb.close() + + fake_socket.close.assert_called_once() + + +def test_create_socket_and_endpoint_description_full_coverage(): + """ + Test the less-commonly used branch of create_socket() where bind=False. + This covers: + - the loop that sets socket options, + - the connect() path, + - the logic in endpoint_description() that inverts self.bound. + """ + fake_context = mock.Mock() + fake_socket = mock.Mock() + + # The context should hand back our fake socket object. + fake_context.socket.return_value = fake_socket + + sb = SocketBase("id") + + # Calling create_socket with bind=False forces the connect() code path. + sb.create_socket( + zmq_context=fake_context, + socket_type=zmq.SUB, + port=9999, + options=[(zmq.CONFLATE, 1)], # one option is enough to hit the loop + bind=False, + ) + + fake_socket.setsockopt.assert_called_once_with(zmq.CONFLATE, 1) + fake_socket.connect.assert_called_once_with("tcp://localhost:9999") + + # Check that endpoint_description reflects bound=False -> "bind": True + desc = sb.endpoint_description() + assert desc == {"id": "id", "port": 9999, "bind": True}