예제 #1
0
 def test_socket(self):
     with patch.object(Channel, "url_scheme", self.test_url_scheme), \
          patch.object(Channel, "url_wildcard", self.test_url_wildcard), \
          patch.object(Channel, "_bind_socket", _test_bind):
         req_socket = Channel(ChannelType.REQUEST, 1)
         rep_socket = Channel(ChannelType.REPLY, 0)
         value = Command(type=CommandType.FIRE)
         req_socket.send(value)
         assert rep_socket.recv() == value
예제 #2
0
 def do_scan(self, angle=10):
     self._cmd_socket.send(
         serialize(Command(type=CommandType.SCAN, value=angle)))
     self._print_result()
예제 #3
0
 def do_fire(self):
     self._cmd_socket.send(serialize(Command(type=CommandType.FIRE)))
     self._print_result()
예제 #4
0
 def do_rotate(self, angle=10):
     self._cmd_socket.send(
         serialize(Command(type=CommandType.ROTATE, value=angle)))
     self._print_result()
예제 #5
0
 def do_move(self, distance=10):
     self._cmd_socket.send(
         serialize(Command(type=CommandType.MOVE, value=distance)))
     self._print_result()
예제 #6
0
 def test_bot_command_receives_reply(self):
     self.send_on_mock_channel(self.bot.cmd_channel,
                               Command(type=CommandType.MOVE, value=10))
     self.server._run_once()
     self.bot.cmd_channel.send.assert_called_once_with(
         CommandReply(result=CommandResult.ACCEPTED))
예제 #7
0
class TestGame(Shared):
    def setup(self):
        super(TestGame, self).setup()
        self.server._handle_bot_registration(
            Registration(client_type=ClientType.BOT,
                         id=Id(name="bot", version=1)))
        self.bot = self.server._bots[0]
        self.bot._tank.status = BotStatus.IDLE

    def test_game_data_sent_once_per_loop(self):
        game_data = GameData(bullets=[], tanks=[])
        type(self.world).gamedata = PropertyMock(return_value=game_data)
        self.server._run_once()
        self.viewer_channel.send.assert_called_with(game_data)

    def test_events_gathered_once_per_loop(self):
        self.world.get_events.return_value = {}
        self.server._run_once()
        self.world.get_events.assert_called_once_with()

    def test_events_sent_when_gathered(self):
        scan_result = Event(scan=ScanResult(tanks=[]))
        self.world.get_events.return_value = {self.bot.tank_id: [scan_result]}
        self.server._run_once()
        self.bot.event_channel.send.assert_called_once_with(scan_result)

    def test_events_sent_to_all_when_no_tank_id(self):
        death = Event(death=Death(victim=Tank(), perpetrator=Tank()))
        self.world.get_events.return_value = {None: [death]}
        self.server._run_once()
        self.bot.event_channel.send.assert_called_once_with(death)

    def test_bot_command_receives_reply(self):
        self.send_on_mock_channel(self.bot.cmd_channel,
                                  Command(type=CommandType.MOVE, value=10))
        self.server._run_once()
        self.bot.cmd_channel.send.assert_called_once_with(
            CommandReply(result=CommandResult.ACCEPTED))

    @pytest.mark.parametrize("command, name, param", (
        (Command(type=CommandType.MOVE, value=10), "move", 10),
        (Command(type=CommandType.ROTATE, value=10), "rotate", 10),
        (Command(type=CommandType.AIM, value=10), "aim", 10),
        (Command(type=CommandType.SCAN, value=10), "scan", 10),
        (Command(type=CommandType.FIRE), "fire", None),
        (Command(type=CommandType.MOVE, value=0), "move", 0),
        (Command(type=CommandType.ROTATE, value=0), "rotate", 0),
        (Command(type=CommandType.AIM, value=0), "aim", 0),
        (Command(type=CommandType.SCAN, value=0), "scan", 0),
    ))
    def test_command(self, command, name, param):
        self.send_on_mock_channel(self.bot.cmd_channel, command)
        self.server._run_once()
        if param is None:
            getattr(self.bot._tank, name).assert_called_once_with()
        else:
            getattr(self.bot._tank, name).assert_called_once_with(param)

    @pytest.fixture(params=(s for s in BotStatus.values()
                            if s != BotStatus.IDLE))
    def command_abort_if_busy_states(self, request):
        yield request.param

    @pytest.mark.parametrize("command",
                             (Command(type=CommandType.MOVE, value=10),
                              Command(type=CommandType.ROTATE, value=1),
                              Command(type=CommandType.AIM, value=-1)))
    def test_command_abort_if_busy(self, command_abort_if_busy_states,
                                   command):
        self.bot._tank.status = command_abort_if_busy_states
        self.send_on_mock_channel(self.bot.cmd_channel, command)
        self.server._run_once()
        getattr(self.bot._tank,
                CommandType.Name(command.type).lower()).assert_not_called()
        self.bot.cmd_channel.send.assert_called_once_with(
            CommandReply(result=CommandResult.BUSY))

    def test_game_started_after_fourth_bot(self):
        # One from setup, and another three here
        for i in range(PLAYER_COUNT - 1):
            assert not self.server.started()
            self.server._handle_bot_registration(
                Registration(client_type=ClientType.BOT,
                             id=Id(name="bot", version=1)))
        assert self.server.started()

    def test_update_not_called_before_game_started(self):
        self.server._run_once()
        self.world.update.assert_not_called()

    def test_game_does_not_end_when_only_one_bot_registered(self):
        self.world.number_of_live_bots = 1
        assert not self.server.finished()