Ejemplo n.º 1
0
    def test_read_info_xml(self, mock_string):
        mock_string.side_effect = [Exception, 'test', 'test']
        test_TCP_Server = TCPServer()
        settings = mock.Mock()
        settings.child.side_effect = [Exception, 'test']
        test_TCP_Server.settings = settings
        test_Socket = Socket(MockPythonSocket())
        test_Socket.send_list(['test'])
        test_Socket.send_string('test')

        with pytest.raises(Exception):
            test_TCP_Server.read_info_xml(test_Socket)

        test_Socket.send_list(['test'])
        test_Socket.send_string('test')
        with pytest.raises(Exception):
            test_TCP_Server.read_info_xml(test_Socket)

        param_here = mock.Mock()
        param_here.restoreState.side_effect = [ExpectedError]
        settings = mock.Mock()
        settings.child.return_value = param_here
        test_TCP_Server.settings = settings
        test_Socket.send_list(['test'])
        test_Socket.send_string('test')
        with pytest.raises(ExpectedError):
            test_TCP_Server.read_info_xml(test_Socket)
Ejemplo n.º 2
0
 def test_select(self, mock_select):
     mock_select.return_value = ([1], [2], [3])
     test_TCP_Server = TCPServer()
     test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     test_mock_socket = MockPythonSocket()
     test_mock_socket.socket = test_socket
     result = np.array(test_TCP_Server.select([test_mock_socket]))
     assert np.array_equal(result, np.array([[1], [2], [3]]))
Ejemplo n.º 3
0
    def test_read_infos(self, mock_string):
        mock_string.return_value = []

        test_TCP_Server = TCPServer()

        params = [{'name': 'settings_client'}]
        test_TCP_Server.settings = Parameter.create(name='Settings',
                                                    children=params)

        test_TCP_Server.read_infos()
Ejemplo n.º 4
0
    def __init__(self, parent=None, params_state=None):
        """

        Parameters
        ----------
        parent
        params_state
        """
        self.client_type = "ACTUATOR"
        DAQ_Move_base.__init__(
            self, parent, params_state
        )  #initialize base class with commom attributes and methods
        self.settings.child(('bounds')).hide()
        self.settings.child(('scaling')).hide()
        self.settings.child(('epsilon')).setValue(1)

        TCPServer.__init__(self, self.client_type)
Ejemplo n.º 5
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')
Ejemplo n.º 6
0
    def __init__(self, parent=None, params_state=None, grabber_type='2D'):
        """

        Parameters
        ----------
        parent
        params_state
        grabber_type: (str) either '0D', '1D' or '2D'
        """
        self.client_type = "GRABBER"
        DAQ_Viewer_base.__init__(self, parent, params_state)  # initialize base class with commom attributes and methods
        TCPServer.__init__(self, self.client_type)

        self.x_axis = None
        self.y_axis = None
        self.data = None
        self.grabber_type = grabber_type
        self.ind_data = 0
        self.data_mock = None
Ejemplo n.º 7
0
    def test_read_info(self, mock_iter_children):
        mock_iter_children.return_value = ['an_info']
        test_TCP_Server = TCPServer()
        settings = mock.Mock()
        child = mock.Mock()
        child.addChild.side_effect = [None, Expected_1]
        child.setValue.side_effect = [Expected_2]
        settings.child.return_value = child
        test_TCP_Server.settings = settings

        test_TCP_Server.read_info(test_info='another_info')
        with pytest.raises(Expected_1):
            test_TCP_Server.read_info(test_info='another_info')

        with pytest.raises(Expected_2):
            test_TCP_Server.read_info()
Ejemplo n.º 8
0
    def test_close_server(self):
        test_TCP_Server = TCPServer()

        socket_1 = Socket(MockPythonSocket())
        socket_1.bind(('0.0.0.1', 4455))
        socket_2 = Socket(MockPythonSocket())
        socket_2.bind(('0.0.0.2', 4456))
        dict_list = [{
            'socket': socket_1,
            'type': 'server'
        }, {
            'socket': socket_2,
            'type': 'Client'
        }]
        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.close_server()
        for socket_dict in test_TCP_Server.connected_clients:
            assert socket_dict['type'] != 'server'

        for socket in test_TCP_Server.settings.child(('conn_clients')).value():
            assert not 'server' in socket
Ejemplo n.º 9
0
    def test_set_connected_clients_table(self):
        test_TCP_Server = TCPServer()

        socket_1 = Socket(MockPythonSocket())
        socket_1.bind(('0.0.0.1', 4455))
        socket_2 = Socket(MockPythonSocket())
        socket_2.bind(('0.0.0.2', 4456))
        dict_list = [{
            'socket': socket_1,
            'type': 'Server'
        }, {
            'socket': socket_2,
            'type': 'Client'
        }]
        test_TCP_Server.connected_clients = dict_list
        result = test_TCP_Server.set_connected_clients_table()
        assert isinstance(result, OrderedDict)
        assert result['Server'] == "('0.0.0.1', 4455)"
        assert result['Client'] == "('0.0.0.2', 4456)"

        socket_except = Socket(MockPythonSocket())
        socket_except._sockname = Exception
        test_TCP_Server.connected_clients = [{
            'socket': socket_except,
            'type': None
        }]
        result = test_TCP_Server.set_connected_clients_table()
        assert result[None] == 'unconnected invalid socket'
Ejemplo n.º 10
0
    def test_init_server(self, mock_socket, mock_timer):
        mock_socket.socket.return_value = MockPythonSocket()
        mock_timer.side_effect = [Expected_1]

        test_TCP_Server = TCPServer()

        params = [{
            'name': 'socket_ip',
            'value': '0.0.0.0'
        }, {
            'name': 'port_id',
            'value': 4455
        }, {
            'name': 'conn_clients',
            'value': None
        }]

        test_TCP_Server.settings = Parameter.create(name='Settings',
                                                    type='group',
                                                    children=params)

        with pytest.raises(Expected_1):
            test_TCP_Server.init_server()
Ejemplo n.º 11
0
    def test_remove_client(self, mock_emit):
        mock_emit.side_effect = [Expected_1, Expected_2]

        test_TCP_Server = TCPServer()
        socket_1 = Socket(MockPythonSocket())
        socket_1.bind(('0.0.0.1', 4455))
        socket_2 = Socket(MockPythonSocket())
        socket_2.bind(('0.0.0.2', 4456))
        dict_list = [{
            'socket': socket_1,
            'type': 'Server'
        }, {
            'socket': socket_2,
            'type': 'Client'
        }]
        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)

        with pytest.raises(Expected_1):
            test_TCP_Server.remove_client(socket_1)

        clients = test_TCP_Server.settings.child('conn_clients').value()
        assert not 'Server' in clients
        assert 'Client' in clients

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

        socket_except = mock.Mock()
        socket_except.close.side_effect = [Exception]

        dict_list = [{'socket': socket_except, 'type': 'Exception'}]
        test_TCP_Server.connected_clients = dict_list

        with pytest.raises(Expected_2):
            test_TCP_Server.remove_client(socket_except)
Ejemplo n.º 12
0
    def test_find_socket_type_within_connected_clients(self):
        test_TCP_Server = TCPServer()
        dict_list = [{
            'socket': 'Client_1',
            'type': 'Server'
        }, {
            'socket': 'Client_2',
            'type': 'Client'
        }]
        test_TCP_Server.connected_clients = dict_list

        assert test_TCP_Server.find_socket_type_within_connected_clients(
            None) is None
        assert test_TCP_Server.find_socket_type_within_connected_clients(
            'Client_1') == 'Server'
        assert test_TCP_Server.find_socket_type_within_connected_clients(
            'Client_2') == 'Client'
Ejemplo n.º 13
0
 def test_timerEvent(self, mock_select, mock_emit):
     mock_select.return_value = Exception
     mock_emit.side_effect = [ExpectedError]
     test_TCP_Server = TCPServer()
     with pytest.raises(ExpectedError):
         test_TCP_Server.timerEvent(None)
Ejemplo n.º 14
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')
Ejemplo n.º 15
0
 def test_command_to_from_client(self):
     test_TCP_Server = TCPServer()
     assert not test_TCP_Server.command_to_from_client(MockPythonSocket())
Ejemplo n.º 16
0
 def test_command_done(self):
     test_TCP_Server = TCPServer()
     assert not test_TCP_Server.command_done(MockPythonSocket())
Ejemplo n.º 17
0
 def test_send_data(self):
     test_TCP_Server = TCPServer()
     assert not test_TCP_Server.send_data(MockPythonSocket(), [1, 2, 3])
Ejemplo n.º 18
0
 def test_read_data(self):
     test_TCP_Server = TCPServer()
     assert not test_TCP_Server.read_data(MockPythonSocket())
Ejemplo n.º 19
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
Ejemplo n.º 20
0
 def test_print_status(self, mock_print):
     mock_print.side_effect = [ExpectedError]
     test_TCP_Server = TCPServer()
     with pytest.raises(ExpectedError):
         test_TCP_Server.print_status('test')
Ejemplo n.º 21
0
    def test_emit_status(self, mock_print):
        mock_print.side_effect = [Expected_1]
        test_TCP_Server = TCPServer()

        with pytest.raises(Expected_1):
            test_TCP_Server.emit_status('status')
Ejemplo n.º 22
0
 def test_init(self):
     test_TCP_Server = TCPServer()
     assert isinstance(test_TCP_Server, TCPServer)
     assert test_TCP_Server.client_type == 'GRABBER'
     assert not test_TCP_Server.connected_clients