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'
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
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)
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'
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