Esempio n. 1
0
def test_connect():
    """WHEN the function function 'connect' is called, and the host name
    (string) and port number (integer) are given, an attempt to connect with
    the respective server (socket) shall be made.
    """
    client = SocketClient("test_connect")

    with mock.patch('socket.socket'):
        client.connect(host, port)
        client.sock.connect.assert_called_with((host, port))
Esempio n. 2
0
def test_close():
    """WHEN the function 'close' is called, the connection to the server
    shall be closed in a timely fashion.
    """
    client = SocketClient("test_close")
    mock_socket = mock.Mock()
    client.sock = mock_socket

    client.close()
    mock_socket.close.assert_called()
Esempio n. 3
0
def test_get_config_and_connect():
    """WHEN the function 'get_config_and_connect' is called, and a config entry
    containing the host name (string) and port number (integer) to the
    specified client (client_name) is available and is of the correct format,
    an attempt to connect with the respective server shall be made.
    """
    client = SocketClient("test_socket")
    data = {"socket": {"test_socket": {"host": host, "port": port}}}
    mock_connect = mock.MagicMock()
    client.connect = mock_connect

    with mock.patch.object(config.var, 'data', data):
        client.get_config_and_connect()
        mock_connect.assert_called_with(host, port)
Esempio n. 4
0
def test_get_config_and_connect_fail(config_data):
    """WHEN the function 'get_config_and_connect' is called, IF the config
    entry containing the host name and port number to the specified client
    (client_name) is of the wrong format or does not exist, THEN no attempt
    to connect with the respective server shall be made and the boolean values
    [False, False] shall be returned.
    """
    client = SocketClient("test_socket")
    mock_connect = mock.MagicMock()
    client.connect = mock_connect

    with mock.patch.object(config.var, 'data', config_data):
        ok = client.get_config_and_connect()
        mock_connect.assert_not_called()
        assert ok == [False, False]
Esempio n. 5
0
def server_client():
    # Setup: create socket.
    server = SocketServer()

    thread = threading.Thread(target=server.create)
    thread.start()

    client = SocketClient("test_recv")
    client.connect(host, port)

    yield server, client

    # Teardown: close socket and thread.
    server.close()
    thread.join()
Esempio n. 6
0
def test_connect_fail():
    """WHEN the function function 'connect' is called, IF the server
    belonging to the given host name (string) and port number (integer) is
    not available, THEN the boolean value False shall be returned and the
    instance attribute "sock" shall be set to None.
    """
    client = SocketClient("test_connect_fail")

    with mock.patch('socket.socket') as mock_socket:
        mock_sock = mock.Mock()
        mock_socket.return_value = mock_sock
        mock_sock.connect.side_effect = socket.error

        ok = client.connect(host, port)
        assert client.sock is None
        assert ok is False
Esempio n. 7
0
def test_send():
    """The function 'send' shall transmit data to the socket in two steps:
    1. Send the length of the data to transmit in bytes followed by
    the delimiter '\n', 2. Send the data; and shall return the boolean
    value True if no error occurred during this operation.
    """
    client = SocketClient("test_send")
    mock_socket = mock.Mock()
    client.sock = mock_socket

    data = 123
    ok = client.send(data)
    expected = [
        call.sendall(b'a%d\n' % len(str(data))),
        call.sendall(str(data).encode())
    ]
    assert mock_socket.mock_calls == expected
    assert ok is True
Esempio n. 8
0
def test_init():
    """The instance attributes 'client_name' and 'sock' shall be changeable."""
    # Test changeability of 'client_name'.
    client = SocketClient("test_init")
    tmp = client.name
    client.name = "test_init"
    assert client.name == "test_init"
    client.name = tmp
    assert client.name == tmp

    # Test changeability of 'sock'.
    tmp = client.sock
    client.sock = "test_init"
    assert client.sock == "test_init"
    client.sock = tmp
    assert client.sock == tmp
Esempio n. 9
0
def test_send_fail():
    """WHEN the function 'send' is called, IF the server is not available,
    THEN the boolean value False shall be returned and the instance variable
    'sock' shall be set to None if its value differ from None.
    """
    # Test 1: Socket error occurs; the variable 'sock' differs from None.
    client = SocketClient("test_send_fail")
    mock_socket = mock.Mock()
    client.sock = mock_socket
    mock_socket.sendall.side_effect = socket.error

    ok = client.send(123)
    assert client.sock is None
    assert ok is False

    # Test 2: Variable 'sock' equals already None.
    client.sock = None
    ok = client.send(123)
    assert ok is False
Esempio n. 10
0
class ABB:
    """Handles the communication with the ABB robot with respect to
        - sending command requests to and
        - receiving/evaluating the responds from the ABB robot.

    Attributes:
        client: The socket interface.
    """
    def __init__(self):
        self.client = SocketClient("abb")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def connect(self):
        """Connects to the socket server."""
        ok, connected = self.client.get_config_and_connect()
        return ok, connected
    
    def close(self):
        """Close connection."""
        case = 0
        self._send(case)
        self.client.close()

    def get_system_information(self):
        """Get system information."""
        case = 1
        ok, data = self._exchange(case)
        return ok, data

    def get_joint_coordinates(self):
        """Get joint coordinates."""
        case = 10
        ok, data = self._exchange(case)
        return ok, data

    def get_position(self):
        """Get the current position."""
        case = 29
        ok, data = self._exchange(case)
        return ok, data
        
    def move(self, x, y, z, q1, q2, q3, q4, cf1, cf4, cf6, cfx):
        """Sends absolute movement coordinates."""
        log.abb.debug("Move to (x, y, z, q1, q2, q3, q4, cf1, cf4, cf6, cfx) = "
                      "({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})"
                      .format(x, y, z, q1, q2, q3, q4, cf1, cf4, cf6, cfx))
        case = 30
        data = [x, y, z, q1, q2]
        ok, data = self._exchange(case, data)

        if ok:
            case = 31
            data = [0, 0, 0, 0, 0, q3, q4, cf1, cf4, cf6, cfx]
            ok, data = self._exchange(case, data)
                      
        return ok, data
Esempio n. 11
0
 def __init__(self):
     self.client = SocketClient("abb")