63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
This program has been developed by students from the bachelor Computer Science at Utrecht
|
|
University within the Software Project course.
|
|
© Copyright Utrecht University (Department of Information and Computing Sciences)
|
|
"""
|
|
|
|
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}
|