コード例 #1
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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()
コード例 #2
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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))
コード例 #3
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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)
コード例 #4
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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]
コード例 #5
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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()
コード例 #6
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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
コード例 #7
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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
コード例 #8
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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
コード例 #9
0
ファイル: test_socket.py プロジェクト: EricssonResearch/arms
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
コード例 #10
0
 def __init__(self):
     self.client = SocketClient("abb")