def test_controller_end_game(self): con = Controller() g_id = con.get('get_new_game_instance_ox', '0', 'True') out = con.get('end_game_ox', str(g_id), 'name1', '0') self.assertEqual(out, None)
def test_controller_get_new_game_instance(self, model_mock): con = Controller() old_guid = con._Controller__game_guid model_mock.return_value = model_mock model_mock.get_new_game_instance.return_value = True g_id = con.get('get_new_game_instance_ox', '0', 'True') self.assertEqual(old_guid + 1, g_id) model_mock.assert_called_with('0', 'True')
def test_controller_check_game_result_error(self, console_view_mock, model_mock): con = Controller() model_mock.return_value = model_mock model_mock.check_game_result.return_value = False g_id = con.get('get_new_game_instance_ox', '0', 'True') con.get('check_game_result_ox', str(g_id), 'name1', '0') model_mock.check_game_result.assert_called_with(model_mock, 'name1', '0') console_view_mock.error.assert_called_with(console_view_mock, model_mock, False)
def test_controller_add_player_error(self, console_view_mock, model_mock): con = Controller() model_mock.return_value = model_mock model_mock.add_player.return_value = False # Model's method must return sthg other than None, 0, False g_id = con.get('get_new_game_instance_ox', '0', 'True') con.get('add_player_ox', str(g_id), 'name1', '0') model_mock.add_player.assert_called_with(model_mock, 'name1', '0') console_view_mock.error.assert_called_with(console_view_mock, model_mock, False)
def test_controller_add_player(self, console_view_mock, model_mock): con = Controller() model_mock.return_value = model_mock model_mock.add_player.return_value = True # Model's method must return sthg other than None, 0, False console_view_mock.add_player.return_value = None # unless we want the Controller to Call OXConsoleView.error g_id = con.get('get_new_game_instance_ox', '0', 'True') out = con.get('add_player_ox', str(g_id), 'name1', '0') self.assertEqual(out, None) # console_view_mock.add_player.return_value == None model_mock.add_player.assert_called_with(model_mock, 'name1', '0') console_view_mock.add_player.assert_called_with(console_view_mock, model_mock, True)
def test_controller_check_game_result(self, console_view_mock, model_mock): con = Controller() model_mock.return_value = model_mock model_mock.check_game_result.return_value = True console_view_mock.check_game_result.return_value = None g_id = con.get('get_new_game_instance_ox', '0', 'True') out = con.get('check_game_result_ox', str(g_id), 'name1', '0') self.assertEqual(out, None) model_mock.check_game_result.assert_called_with(model_mock, 'name1', '0') console_view_mock.check_game_result.assert_called_with(console_view_mock, model_mock, True)
class OXServer: TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 1024 controller = Controller() def _bind_and_listen(self): s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((self.TCP_IP, self.TCP_PORT)) s.listen(8) except (socket.error, socket.herror, socket.gaierror, socket.timeout): s = None if s is None: Logger.log('Could not open socket') exit(1) return s def run(self): s = self._bind_and_listen() # server loop while True: conn = addr = None try: # accept next connection conn, addr = s.accept() # get data from the client data = conn.recv(self.BUFFER_SIZE) # constant BUFFER_SIZE for GET requests prevents simple DoS attacks if not data: Logger.log('Empty request from', addr) continue # call the controller get method action, game_id, optional_params = Messages.parse_incoming_request(data) ret = self.controller.get(action, game_id, *optional_params) json_ret = Messages.prepare_response(ret) conn.sendall(str(json_ret).encode()) except Exception as e: # log exception and client address Logger.log_request(e, addr) finally: conn.close() # print('Games active: ' + str(self.controller.games_active())) s.close() # this line should remain here in the case of using os signals to close the server process
def test_controller_get_new_game_instance_too_many_games(self, model_mock): con = Controller() con._Controller__games = {0: '', 1: ''} model_mock.return_value = model_mock model_mock.get_new_game_instance.return_value = True self.assertRaises(OverflowError, con.get('get_new_game_instance_ox', 0))