Beispiel #1
0
 def test_should_raise_exception_if_bot_already_shutdown(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.close()
     self.assertRaisesRegexp(BotCommunicationError,
                             "not running, cannot get response",
                             localPlayerCommunication.get_response, 0.1)
Beispiel #2
0
    def test_subprocess_ignores_sigterm(self):
        def fake_terminate():
            pass

        localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                       self.workingDir)
        localPlayerCommunication.runningPlayer.terminate = fake_terminate
        localPlayerCommunication.send_message("nahnahstayingalive")
        responseFromBot = localPlayerCommunication.get_response()
        self.assertIn("MY JAM!", responseFromBot)
        self.assert_comms_closed(localPlayerCommunication)
Beispiel #3
0
 def test_should_output_to_file_if_create_with_stream(self):
     file = StringIO.StringIO()
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir, file)
     localPlayerCommunication.send_message("testing")
     localPlayerCommunication.get_response()
     self.assertEqual(normalize_newlines(file.getvalue()),
                      "testing\n1..2..3\n\n")
     file.close()
     localPlayerCommunication.close()
Beispiel #4
0
 def test_should_respond_to_say_something(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.send_message("testing")
     responseFromBot = localPlayerCommunication.get_response()
     self.assertEqual(normalize_newlines(responseFromBot), "1..2..3\n")
     localPlayerCommunication.close()
Beispiel #5
0
 def test_should_raise_exception_if_bot_died_and_eof_read(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.send_message("die")
     self.assertRaisesRegexp(BotCommunicationError,
                             "program didn't say anything",
                             localPlayerCommunication.get_response, 0.1)
     localPlayerCommunication.close()
Beispiel #6
0
 def test_should_raise_bot_communication_error_if_bot_times_out(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.send_message("fail")
     self.assertRaisesRegexp(BotCommunicationError,
                             "Failed to send message because of timeout",
                             localPlayerCommunication.get_response, 0.1)
     localPlayerCommunication.close()
def get_player(cmd, workdir, num):
    player = Player()
    if cmd == "starter_bot":
        starter_bot = StarterBot()
        comm = DirectInvocationCommunication(
            lambda msg: starter_bot.send_message(msg),
            lambda: starter_bot.get_response())
        player.set_communication(comm)
    elif cmd == "interactive":
        bot = InteractiveBot(num)
        comm = DirectInvocationCommunication(lambda msg: bot.send_message(msg),
                                             lambda: bot.get_response())
        player.set_communication(comm)
    else:
        comm = PlayerCommunication(cmd, workdir)
        player.set_communication(comm)
    return player
Beispiel #8
0
def main():
    args = _get_args()
    try:
        comm1 = PlayerCommunication(args.player1_cmd, args.player1_workdir)
        comm2 = PlayerCommunication(args.player2_cmd, args.player2_workdir)
        player1 = SubprocessPlayer(comm1)
        player2 = SubprocessPlayer(comm2)
        outputer = DatabaseOutput(args.host, int(
            args.port), args.database_name) if args.server else Output()
        engine = BattlelineEngine(player1, player2, outputer)
        engine.initialize()
        engine.run_until_game_end()
        print "PLAYER : {} -> {} HAS WON".format(
            engine.get_winning_player(), engine.get_winning_player_name())
    except:
        raise
    finally:
        comm1.close()
        comm2.close()
Beispiel #9
0
 def test_should_raise_bot_communication_error_if_bot_can_not_respond(self):
     localPlayerCommunication = PlayerCommunication(self.nonExistantBot,
                                                    self.workingDir)
     time.sleep(1)
     with self.assertRaises(BotCommunicationError):
         localPlayerCommunication.send_message("testing")
Beispiel #10
0
 def test_should_always_send_without_timout_invalid_program(self):
     localPlayerCommunication = PlayerCommunication(self.nonExistantBot,
                                                    self.workingDir)
     time.sleep(1)
     with self.assertRaises(BotCommunicationError):
         localPlayerCommunication.send_message("testing")
Beispiel #11
0
 def test_should_always_send_without_timout_valid_program(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.send_message("testing")
     localPlayerCommunication.close()
Beispiel #12
0
 def test_subprocess_terminated_on_close_with_bad_input(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     localPlayerCommunication.send_message("fail")
     self.assert_comms_closed(localPlayerCommunication)
Beispiel #13
0
 def test_subprocess_terminated_on_close(self):
     localPlayerCommunication = PlayerCommunication(self.workingBot,
                                                    self.workingDir)
     self.assert_comms_closed(localPlayerCommunication)