def test_game_controller_partial_close(tc): # checking close() works even if one or both players didn't start channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player one') gc1 = gtp_controller.Game_controller('one', 'two') gc1.close_players() tc.assertIsNone(gc1.describe_late_errors()) gc2 = gtp_controller.Game_controller('one', 'two') gc2.set_player_controller('w', controller) gc2.close_players() tc.assertIsNone(gc2.describe_late_errors()) tc.assertIs(controller.channel_is_closed, True)
def test_game_controller_cautious_mode(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') gc = gtp_controller.Game_controller('one', 'two') gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2) tc.assertIs(gc.in_cautious_mode, False) gc.set_cautious_mode(True) tc.assertIs(gc.in_cautious_mode, True) channel1.fail_command = "list_commands" tc.assertEqual(gc.maybe_send_command('b', 'test'), "test response") tc.assertIsNone(gc.maybe_send_command('b', 'error')) tc.assertIsNone(gc.maybe_send_command('b', 'list_commands')) channel2.fail_command = "known_command" tc.assertEqual(gc.send_command('w', 'test'), "test response") tc.assertIs(gc.known_command('w', 'list_commands'), False) gc.close_players() tc.assertEqual( gc.describe_late_errors(), "transport error sending 'list_commands' to player one:\n" "forced failure for send_command_line\n" "transport error sending 'known_command list_commands' to player two:\n" "forced failure for send_command_line")
def __init__(self, tc, player_b=None, player_w=None, **kwargs): self.tc = tc kwargs.setdefault('board_size', 9) game_controller = gtp_controller.Game_controller('one', 'two') if player_b is None: player_b = gtp_engine_fixtures.Test_player() if player_w is None: player_w = gtp_engine_fixtures.Test_player() engine_b = gtp_engine_fixtures.make_player_engine(player_b) engine_w = gtp_engine_fixtures.make_player_engine(player_w) channel_b = gtp_controller_test_support.Testing_gtp_channel(engine_b) channel_w = gtp_controller_test_support.Testing_gtp_channel(engine_w) controller_b = gtp_controller.Gtp_controller(channel_b, 'player one') controller_w = gtp_controller.Gtp_controller(channel_w, 'player two') game_controller.set_player_controller('b', controller_b) game_controller.set_player_controller('w', controller_w) game = gtp_games.Gtp_game(game_controller, **kwargs) self.game_controller = game_controller self.game = game self.controller_b = controller_b self.controller_w = controller_w self.channel_b = channel_b self.channel_w = channel_w self.engine_b = channel_b.engine self.engine_w = channel_w.engine self.player_b = channel_b.engine.player self.player_w = channel_w.engine.player
def test_game_controller_set_player_subprocess(tc): msf = gtp_engine_fixtures.Mock_subprocess_fixture(tc) engine = gtp_engine_fixtures.get_test_engine() engine.add_command("name", lambda args: 'blackplayer') msf.register_engine('named', engine) gc = gtp_controller.Game_controller('one', 'two') gc.set_player_subprocess('b', ['testb', 'id=one', 'engine=named'], check_protocol_version=False) gc.set_player_subprocess('w', ['testw', 'id=two'], env={'a': 'b'}) tc.assertEqual(gc.get_controller('b').name, "player one") tc.assertEqual(gc.get_controller('w').name, "player two") tc.assertEqual(gc.engine_descriptions['b'].raw_name, "blackplayer") tc.assertIsNone(gc.engine_descriptions['w'].raw_name) channel1 = msf.get_channel('one') channel2 = msf.get_channel('two') tc.assertEqual(channel1.engine.commands_handled[0][0], 'name') tc.assertIsNone(channel1.requested_env) tc.assertEqual(channel2.engine.commands_handled[0][0], 'protocol_version') tc.assertEqual(channel2.requested_env, {'a': 'b'}) gc.close_players() tc.assertEqual(gc.get_resource_usage_cpu_times(), {'b': 546.2, 'w': 567.2})
def test_game_controller_protocol_version(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel1.engine.add_command('protocol_version', lambda args: "3") gc = gtp_controller.Game_controller('one', 'two') with tc.assertRaises(BadGtpResponse) as ar: gc.set_player_controller('b', controller1) tc.assertEqual(str(ar.exception), "player one reports GTP protocol version 3") tc.assertIs(gc.get_controller('b'), controller1)
def test_game_controller_set_player_subprocess_error(tc): msf = gtp_engine_fixtures.Mock_subprocess_fixture(tc) gc = gtp_controller.Game_controller('one', 'two') with tc.assertRaises(GtpChannelError) as ar: gc.set_player_subprocess('b', ['testb', 'fail=startup']) tc.assertEqual( str(ar.exception), "error starting subprocess for player one:\nexec forced to fail") tc.assertRaises(KeyError, gc.get_controller, 'b') tc.assertEqual(gc.get_resource_usage_cpu_times(), {'b': None, 'w': None})
def _run(self): warnings = [] log_entries = [] try: game_controller = gtp_controller.Game_controller( self.player_b.code, self.player_w.code) game = gtp_games.Gtp_game(game_controller, self.board_size, self.komi, self.move_limit) game.set_game_id(self.game_id) except ValueError, e: raise job_manager.JobFailed("error creating game: %s" % e)
def test_game_controller_leave_cautious_mode(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') gc = gtp_controller.Game_controller('one', 'two') gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2) channel1.fail_command = "list_commands" gc.set_cautious_mode(True) tc.assertIs(gc.in_cautious_mode, True) tc.assertEqual(gc.send_command('b', 'test'), "test response") gc.set_cautious_mode(False) tc.assertIs(gc.in_cautious_mode, False) with tc.assertRaises(GtpTransportError) as ar: gc.send_command('b', 'list_commands') tc.assertEqual( str(ar.exception), "transport error sending 'list_commands' to player one:\n" "forced failure for send_command_line")
def test_game_controller_cautious_mode_send_command(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') gc = gtp_controller.Game_controller('one', 'two') gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2) gc.set_cautious_mode(True) channel1.fail_command = "list_commands" tc.assertEqual(gc.send_command('b', 'test'), "test response") with tc.assertRaises(BadGtpResponse) as ar: gc.send_command('b', 'list_commands') tc.assertEqual(str(ar.exception), "late low-level error from player one") tc.assertIsNone(ar.exception.gtp_command) gc.close_players() tc.assertEqual( gc.describe_late_errors(), "transport error sending 'list_commands' to player one:\n" "forced failure for send_command_line")
def test_game_controller_channel_errors(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') gc = gtp_controller.Game_controller('one', 'two') gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2) channel1.fail_command = "test" with tc.assertRaises(GtpTransportError) as ar: gc.send_command('b', 'test') tc.assertEqual( str(ar.exception), "transport error sending 'test' to player one:\n" "forced failure for send_command_line") channel2.fail_command = "list_commands" with tc.assertRaises(GtpTransportError) as ar: gc.maybe_send_command('w', 'list_commands') tc.assertEqual( str(ar.exception), "transport error sending 'list_commands' to player two:\n" "forced failure for send_command_line") channel2.fail_command = "known_command" with tc.assertRaises(GtpTransportError) as ar: gc.known_command('w', 'test') tc.assertEqual( str(ar.exception), "transport error sending 'known_command test' to player two:\n" "forced failure for send_command_line") channel1.fail_close = True gc.close_players() tc.assertEqual(gc.describe_late_errors(), "error closing player one:\n" "forced failure for close")
def test_game_controller_engine_descriptions(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') channel1.engine.add_command('name', lambda args: "some-name") channel1.engine.add_command('version', lambda args: "v123") channel1.engine.add_command('gomill-describe_engine', lambda args: "foo\nbar") channel2.engine.force_error('gomill-describe_engine') gc = gtp_controller.Game_controller('one', 'two') # This isn't documented behaviour tc.assertEqual(gc.engine_descriptions, {'b': None, 'w': None}) gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2) tc.assertEqual(gc.engine_descriptions['b'].raw_name, "some-name") tc.assertEqual(gc.engine_descriptions['b'].raw_version, "v123") tc.assertEqual(gc.engine_descriptions['b'].description, "foo\nbar") tc.assertIsNone(gc.engine_descriptions['w'].raw_name) tc.assertIsNone(gc.engine_descriptions['w'].raw_version) tc.assertIsNone(gc.engine_descriptions['w'].description)
def test_game_controller(tc): channel1 = gtp_engine_fixtures.get_test_channel() controller1 = Gtp_controller(channel1, 'player one') channel2 = gtp_engine_fixtures.get_test_channel() controller2 = Gtp_controller(channel2, 'player two') channel1.engine.add_command('b_only', lambda args: "yes") gc = gtp_controller.Game_controller('one', 'two') tc.assertRaises(KeyError, gc.get_controller, 'b') gc.set_player_controller('b', controller1) gc.set_player_controller('w', controller2, check_protocol_version=False) tc.assertIsNone(gc.engine_descriptions['b'].raw_name) tc.assertIsNone(gc.engine_descriptions['b'].raw_version) tc.assertIsNone(gc.engine_descriptions['b'].description) tc.assertIsNone(gc.engine_descriptions['w'].raw_name) tc.assertIsNone(gc.engine_descriptions['w'].raw_version) tc.assertIsNone(gc.engine_descriptions['w'].description) tc.assertEqual(gc.players, {'b': 'one', 'w': 'two'}) tc.assertIs(gc.get_controller('b'), controller1) tc.assertIs(gc.get_controller('w'), controller2) tc.assertRaises(KeyError, gc.get_controller, 'x') tc.assertIs(gc.known_command('b', 'b_only'), True) tc.assertIs(gc.known_command('w', 'b_only'), False) tc.assertEqual(gc.send_command('b', 'test'), "test response") tc.assertEqual(gc.send_command('w', 'test', 'abc', 'def'), "args: abc def") tc.assertEqual(gc.send_command('b', 'b_only'), "yes") with tc.assertRaises(BadGtpResponse) as ar: gc.send_command('w', 'b_only') tc.assertEqual(ar.exception.gtp_error_message, "unknown command") tc.assertEqual(ar.exception.gtp_command, "b_only") with tc.assertRaises(BadGtpResponse) as ar: gc.send_command('b', 'error') tc.assertEqual(ar.exception.gtp_error_message, "normal error") tc.assertEqual(ar.exception.gtp_command, "error") tc.assertEqual(gc.maybe_send_command('b', 'b_only'), "yes") tc.assertIsNone(gc.maybe_send_command('w', 'b_only')) tc.assertIsNone(gc.maybe_send_command('b', 'error')) tc.assertIsNone(gc.describe_late_errors()) tc.assertIs(controller1.channel_is_closed, False) tc.assertIs(controller2.channel_is_closed, False) gc.close_players() tc.assertIs(controller1.channel_is_closed, True) tc.assertIs(controller2.channel_is_closed, True) tc.assertIsNone(gc.describe_late_errors()) tc.assertEqual(gc.get_resource_usage_cpu_times(), {'b': None, 'w': None}) tc.assertEqual(channel1.engine.commands_handled, [ ('protocol_version', []), ('name', []), ('version', []), ('known_command', ['gomill-describe_engine']), ('known_command', ['b_only']), ('test', []), ('b_only', []), ('error', []), ('b_only', []), ('known_command', ['error']), ('error', []), ('quit', []), ]) tc.assertEqual(channel2.engine.commands_handled, [ ('name', []), ('version', []), ('known_command', ['gomill-describe_engine']), ('known_command', ['b_only']), ('test', ['abc', 'def']), ('b_only', []), ('quit', []), ])
def test_game_controller_get_gtp_cpu_times(tc): def controller1(): channel = gtp_engine_fixtures.get_test_channel() return Gtp_controller(channel, 'notimplemented') def controller2(): channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('gomill-cpu_time', lambda args: "3.525") return Gtp_controller(channel, 'good') def controller3(): channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('gomill-cpu_time', lambda args: "invalid") return Gtp_controller(channel, 'bad') def controller4(): channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_error('gomill-cpu_time') return Gtp_controller(channel, 'error') def controller5(): channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_fatal_error('gomill-cpu_time') return Gtp_controller(channel, 'fatalerror') gc1 = gtp_controller.Game_controller('x', 'y') gc1.set_player_controller('b', controller1()) gc1.set_player_controller('w', controller2()) tc.assertEqual(gc1.get_gtp_cpu_times(), ({'w': 3.525}, set([]))) gc2 = gtp_controller.Game_controller('x', 'y') gc2.set_player_controller('b', controller3()) gc2.set_player_controller('w', controller2()) tc.assertEqual(gc2.get_gtp_cpu_times(), ({'w': 3.525}, set(['b']))) gc3 = gtp_controller.Game_controller('x', 'y') gc3.set_player_controller('b', controller3()) gc3.set_player_controller('w', controller4()) tc.assertEqual(gc3.get_gtp_cpu_times(), ({}, set(['b', 'w']))) gc4 = gtp_controller.Game_controller('x', 'y') gc4.set_player_controller('b', controller2()) gc4.set_player_controller('w', controller5()) tc.assertEqual(gc4.get_gtp_cpu_times(), ({'b': 3.525}, set(['w']))) gc4.close_players() tc.assertEqual( gc4.describe_late_errors(), "error sending 'quit' to fatalerror:\n" "engine has closed the command channel") gc5 = gtp_controller.Game_controller('x', 'y') gc5.set_player_controller('b', controller1()) gc5.set_player_controller('w', controller2()) gc5.set_cautious_mode(True) gc5.get_controller('w').channel.fail_command = 'gomill-cpu_time' tc.assertEqual(gc5.get_gtp_cpu_times(), ({}, set(['w']))) gc5.close_players() tc.assertEqual( gc5.describe_late_errors(), "transport error sending 'gomill-cpu_time' to good:\n" "forced failure for send_command_line") gc6 = gtp_controller.Game_controller('x', 'y') gc6.set_player_controller('b', controller1()) gc6.set_player_controller('w', controller2()) gc6.get_controller('w').channel.fail_command = 'gomill-cpu_time' with tc.assertRaises(GtpTransportError) as ar: gc6.get_gtp_cpu_times() tc.assertEqual( str(ar.exception), "transport error sending 'gomill-cpu_time' to good:\n" "forced failure for send_command_line") gc6.close_players() tc.assertIsNone(gc6.describe_late_errors())
def main(): ## usage = "%prog [options] --black='<command>' --white='<command>'" ## parser = OptionParser(usage=usage) ## parser.add_option("--black", help=SUPPRESS_HELP) ## parser.add_option("--white", help=SUPPRESS_HELP) ## parser.add_option("--komi", type="float", default=7.5) ## parser.add_option("--size", type="int", default=19) ## parser.add_option("--games", type="int", default=1, metavar="COUNT") ## parser.add_option("--verbose", type="choice", choices=('0','1','2'), ## default=0, metavar="0|1|2") ## parser.add_option("--sgfbase", type="string", metavar="FILENAME-PREFIX") ## ## (options, args) = parser.parse_args() ## if args: ## parser.error("too many arguments") ## if not options.black or not options.white: ## parser.error("players not specified") ## ## black_command = shlex.split(options.black) ## white_command = shlex.split(options.white) ## b_code = black_command[0] ## w_code = white_command[0] ## if b_code == w_code: ## b_code += '-b' ## w_code += '-w' #these used to be in an options object verbose = '2' size = 13 komi = 5.5 b_code = 'b' w_code = 'w' white_command = 'gnugo\gnugo --mode gtp --silent --color white' #video_source = 'tests/go game.mp4' video_source = 0 game_controller = gtp_controller.Game_controller(b_code, w_code) try: player = video_player.VideoPlayer("camera_params.npz", video_source, size, "Gowatcher", rotate_pic=True, debug=True) engine = video_player.make_engine(player) channel = gtp_controller.Internal_gtp_channel(engine) black_controller = gtp_controller.Gtp_controller(channel, 'b') game_controller.set_player_controller('b', black_controller) game_controller.set_player_subprocess('w', white_command) except GtpChannelError as e: game_controller.close_players() sys.exit("error creating players:\n%s\n" % e) eds = game_controller.engine_descriptions if verbose == '1': print('Black: %s' % eds['b'].get_short_description()) print('White: %s' % eds['w'].get_short_description()) elif verbose == '2': print('Black: %s\n' % eds['b'].get_long_description()) print('White: %s\n' % eds['w'].get_long_description()) game = gtp_games.Gtp_game(game_controller, board_size=size, komi=komi, move_limit=1000) if verbose == '1': game.set_move_callback(print_move) elif verbose == '2': game.set_move_callback(print_board) game.allow_scorer('b') game.allow_scorer('w') try: game.prepare() game.run() except (GtpChannelError, BadGtpResponse) as e: game_controller.close_players() sys.exit("aborting run due to error:\n%s\n" % e) print(game.result.describe()) ## if options.sgfbase is not None: ## sgf_game = game.make_sgf() ## sgf_game.get_root().set("AP", ("Gomill twogtp", __version__)) ## try: ## write_sgf(sgf_game, game_number, options.sgfbase) ## except EnvironmentError, e: ## sys.exit("error writing SGF file: %s" % e) game_controller.close_players() late_error_messages = game_controller.describe_late_errors() if late_error_messages: sys.exit(late_error_messages)