コード例 #1
0
    def test_send_command(self, mock_emit):
        mock_emit.side_effect = [Expected_1, None]

        test_TCP_Server = TCPServer()
        test_Socket = Socket(MockPythonSocket())
        test_TCP_Server.message_list = []
        with pytest.raises(Expected_1):
            test_TCP_Server.send_command(test_Socket)

        test_TCP_Server.message_list = ['move_at']
        test_TCP_Server.send_command(test_Socket)
        assert test_Socket.get_string() == 'move_at'

        assert not test_TCP_Server.send_command(test_Socket, command='test')
コード例 #2
0
    def test_process_cmds(self, mock_find_socket, mock_emit, mock_read):
        mock_emit.side_effect = [None, Expected_1, Expected_2, Expected_3]

        test_TCP_Server = TCPServer()
        commands = ['Done', 'Infos', 'Info_xml', 'Info', 'test']
        test_TCP_Server.message_list = commands

        assert not test_TCP_Server.process_cmds('unknown')
        with pytest.raises(Expected_1):
            test_TCP_Server.process_cmds('unknown')

        assert not test_TCP_Server.process_cmds('Done')

        test_Socket = Socket(MockPythonSocket())
        test_Socket.send_string('test')
        mock_find_socket.return_value = test_Socket
        with pytest.raises(Expected_2):
            test_TCP_Server.process_cmds('Infos')

        mock_read.side_effect = [ExpectedError]
        test_Socket = Socket(MockPythonSocket())
        test_Socket.send_list([1, 2, 3])
        test_Socket.send_string('test')
        mock_find_socket.return_value = test_Socket
        with pytest.raises(ExpectedError):
            test_TCP_Server.process_cmds('Info_xml')

        test_Socket = Socket(MockPythonSocket())
        test_Socket.send_string('info')
        test_Socket.send_string('data')
        mock_find_socket.return_value = test_Socket
        with pytest.raises(Expected_3):
            test_TCP_Server.process_cmds('Info')

        assert not test_TCP_Server.process_cmds('test')
コード例 #3
0
    def test_listen_client(self, mock_select):
        socket_1 = Socket(MockPythonSocket())
        socket_1.bind(('0.0.0.1', 4455))
        socket_2 = Socket(MockPythonSocket())
        socket_2.bind(('0.0.0.2', 4456))
        socket_3 = Socket(MockPythonSocket())
        socket_3.bind(('0.0.0.3', 4457))
        socket_4 = Socket(MockPythonSocket())
        socket_4.bind(('0.0.0.4', 4458))
        socket_5 = Socket(MockPythonSocket())
        socket_5.bind(('0.0.0.5', 4459))
        socket_6 = Socket(MockPythonSocket())
        socket_6.bind(('0.0.0.6', 4460))

        socket_1.send_string('')
        socket_2.send_string('Done')
        socket_3.send_string('Quit')
        socket_4.send_string('unknown')
        socket_5.send_string('test')
        socket_6.send_string('Server')

        mock_select.return_value = [[socket_2, socket_3, socket_4, socket_5],
                                    [], [socket_1]]

        test_TCP_Server = TCPServer()
        dict_list = [{
            'socket': socket_1,
            'type': 'Server'
        }, {
            'socket': socket_2,
            'type': 'Client'
        }, {
            'socket': socket_3,
            'type': 'Quit'
        }, {
            'socket': socket_4,
            'type': 'Unknown'
        }, {
            'socket': socket_5,
            'type': 'test'
        }, {
            'socket': socket_6,
            'type': 'serversocket'
        }]

        test_TCP_Server.connected_clients = dict_list

        params = [{'name': 'conn_clients', 'value': dict_list}]
        test_TCP_Server.settings = Parameter.create(name='Settings',
                                                    type='group',
                                                    children=params)

        test_TCP_Server.serversocket = socket_5
        test_TCP_Server.socket_types = []
        test_TCP_Server.message_list = ['Done']
        test_TCP_Server.listen_client()

        for socket_dict in test_TCP_Server.connected_clients:
            assert not 'Server' in socket_dict['type']
            assert not 'Quit' in socket_dict['type']

        clients = test_TCP_Server.settings.child('conn_clients').value()
        assert len(clients) == 4

        assert test_TCP_Server.serversocket.socket._closed

        mock_select.return_value = [[socket_6], [], []]

        dict_list = [{
            'socket': socket_2,
            'type': 'Client'
        }, {
            'socket': socket_3,
            'type': 'Quit'
        }, {
            'socket': socket_4,
            'type': 'Unknown'
        }, {
            'socket': socket_5,
            'type': 'test'
        }, {
            'socket': socket_6,
            'type': 'serversocket'
        }]

        params = [{'name': 'conn_clients', 'value': dict_list}]
        test_TCP_Server.settings = Parameter.create(name='Settings',
                                                    type='group',
                                                    children=params)

        test_TCP_Server.serversocket = socket_6
        test_TCP_Server.socket_types = ['Server']
        test_TCP_Server.connected_clients = dict_list
        test_TCP_Server.listen_client()

        is_added = False
        for socket_dict in test_TCP_Server.connected_clients:
            if 'Server' in socket_dict['type']:
                is_added = True
        assert is_added

        assert len(test_TCP_Server.settings.child('conn_clients').value()) == 6