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 test_controller(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.name, 'player test') tc.assertIs(controller.channel, channel) tc.assertFalse(controller.channel_is_bad) tc.assertEqual(controller.do_command("test", "ab", "cd"), "args: ab cd") with tc.assertRaises(BadGtpResponse) as ar: controller.do_command("error") tc.assertEqual(ar.exception.gtp_error_message, "normal error") tc.assertEqual(ar.exception.gtp_command, "error") tc.assertSequenceEqual(ar.exception.gtp_arguments, []) tc.assertEqual( str(ar.exception), "failure response from 'error' to player test:\n" "normal error") with tc.assertRaises(BadGtpResponse) as ar: controller.do_command("fatal") tc.assertFalse(controller.channel_is_bad) with tc.assertRaises(GtpChannelClosed) as ar: controller.do_command("test") tc.assertEqual( str(ar.exception), "error sending 'test' to player test:\n" "engine has closed the command channel") tc.assertTrue(controller.channel_is_bad) controller.close() tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_known_command(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'kc test') tc.assertTrue(controller.known_command("test")) tc.assertFalse(controller.known_command("nonesuch")) tc.assertTrue(controller.known_command("test")) tc.assertFalse(controller.known_command("nonesuch"))
def test_controller(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.name, 'player test') tc.assertIs(controller.channel, channel) tc.assertFalse(controller.channel_is_bad) tc.assertEqual(controller.do_command("test", "ab", "cd"), "args: ab cd") with tc.assertRaises(BadGtpResponse) as ar: controller.do_command("error") tc.assertEqual(ar.exception.gtp_error_message, "normal error") tc.assertEqual(ar.exception.gtp_command, "error") tc.assertSequenceEqual(ar.exception.gtp_arguments, []) tc.assertEqual(str(ar.exception), "failure response from 'error' to player test:\n" "normal error") with tc.assertRaises(BadGtpResponse) as ar: controller.do_command("fatal") tc.assertFalse(controller.channel_is_bad) with tc.assertRaises(GtpChannelClosed) as ar: controller.do_command("test") tc.assertEqual(str(ar.exception), "error sending 'test' to player test:\n" "engine has closed the command channel") tc.assertTrue(controller.channel_is_bad) controller.close() tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_safe_do_command_closed_channel(tc): # check it's ok to call safe_do_command() on a closed channel channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') controller.safe_close() tc.assertIsNone(controller.safe_do_command("test")) tc.assertListEqual(channel.engine.commands_handled, [('quit', [])]) tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_list_commands(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'lc test') channel.engine.add_command("xyzzy", None) channel.engine.add_command("pl ugh", None) tc.assertListEqual(controller.list_commands(), [ 'error', 'fatal', 'known_command', 'list_commands', 'multiline', 'protocol_version', 'quit', 'test', 'xyzzy' ])
def test_list_commands(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'lc test') channel.engine.add_command("xyzzy", None) channel.engine.add_command("pl ugh", None) tc.assertListEqual( controller.list_commands(), ['error', 'fatal', 'known_command', 'list_commands', 'multiline', 'protocol_version', 'quit', 'test', 'xyzzy'])
def test_controller_close_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.fail_close = True with tc.assertRaises(GtpTransportError) as ar: controller.close() tc.assertEqual(str(ar.exception), "error closing player test:\n" "forced failure for close") tc.assertListEqual(controller.retrieve_error_messages(), [])
def __init__(self, tc): self.tc = tc self.channel = gtp_engine_fixtures.get_test_channel() self.underlying_engine = self.channel.engine self.controller = gtp_controller.Gtp_controller( self.channel, 'testbackend') self.proxy = gtp_proxy.Gtp_proxy() self.proxy.set_back_end_controller(self.controller) self.engine = self.proxy.engine self.commands_handled = self.underlying_engine.commands_handled
def test_controller_first_command_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') with tc.assertRaises(BadGtpResponse) as ar: controller.do_command("error") tc.assertEqual( str(ar.exception), "failure response from first command (error) to player test:\n" "normal error") tc.assertListEqual(controller.retrieve_error_messages(), [])
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_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_controller_safe_close_with_error_from_close(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.fail_close = True controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertListEqual(channel.engine.commands_handled, [('quit', [])]) tc.assertListEqual( controller.retrieve_error_messages(), ["error closing player test:\n" "forced failure for close"])
def test_controller_close_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.fail_close = True with tc.assertRaises(GtpTransportError) as ar: controller.close() tc.assertEqual( str(ar.exception), "error closing player test:\n" "forced failure for close") tc.assertListEqual(controller.retrieve_error_messages(), [])
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_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_describe_engine(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') short_s, long_s = gtp_controller.describe_engine(controller) tc.assertEqual(short_s, "unknown") tc.assertEqual(long_s, "unknown") channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") controller = Gtp_controller(channel, 'player test') short_s, long_s = gtp_controller.describe_engine(controller) tc.assertEqual(short_s, "test engine") tc.assertEqual(long_s, "test engine") channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") channel.engine.add_command('version', lambda args:"1.2.3") controller = Gtp_controller(channel, 'player test') short_s, long_s = gtp_controller.describe_engine(controller) tc.assertEqual(short_s, "test engine:1.2.3") tc.assertEqual(long_s, "test engine:1.2.3") channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") channel.engine.add_command('version', lambda args:"1.2.3") channel.engine.add_command( 'gomill-describe_engine', lambda args:"test engine (v1.2.3):\n pl\xc3\xa1yer \xa3") controller = Gtp_controller(channel, 'player test') short_s, long_s = gtp_controller.describe_engine(controller) tc.assertEqual(short_s, "test engine:1.2.3") tc.assertEqual(long_s, "test engine (v1.2.3):\n pl\xc3\xa1yer ?") channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") channel.engine.add_command('version', lambda args:"test engine v1.2.3") controller = Gtp_controller(channel, 'player test') short_s, long_s = gtp_controller.describe_engine(controller) tc.assertEqual(short_s, "test engine:v1.2.3") tc.assertEqual(long_s, "test engine:v1.2.3")
def test_error_from_list_commands(tc): channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_error("list_commands") controller = gtp_controller.Gtp_controller(channel, 'testbackend') proxy = gtp_proxy.Gtp_proxy() with tc.assertRaises(BackEndError) as ar: proxy.set_back_end_controller(controller) tc.assertEqual( str(ar.exception), "failure response from first command " "(list_commands) to testbackend:\n" "handler forced to fail") tc.assertIsInstance(ar.exception.cause, BadGtpResponse) proxy.close()
def test_controller_safe_close_with_error_from_quit(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.force_next_response = "# error\n\n" controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertListEqual(channel.engine.commands_handled, [('quit', [])]) tc.assertListEqual(controller.retrieve_error_messages(), [ "GTP protocol error reading response to first command (quit) " "from player test:\n" "no success/failure indication from engine: first line is `# error`" ])
def test_controller_command_transport_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.do_command("test"), "test response") tc.assertFalse(controller.channel_is_bad) channel.fail_next_command = True with tc.assertRaises(GtpTransportError) as ar: controller.do_command("test") tc.assertEqual( str(ar.exception), "transport error sending 'test' to player test:\n" "forced failure for send_command_line") tc.assertTrue(controller.channel_is_bad) tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_error_from_list_commands(tc): channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_error("list_commands") controller = gtp_controller.Gtp_controller(channel, 'testbackend') proxy = gtp_proxy.Gtp_proxy() with tc.assertRaises(BackEndError) as ar: proxy.set_back_end_controller(controller) tc.assertEqual(str(ar.exception), "failure response from first command " "(list_commands) to testbackend:\n" "handler forced to fail") tc.assertIsInstance(ar.exception.cause, BadGtpResponse) proxy.close()
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_controller_response_protocol_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.do_command("test"), "test response") tc.assertFalse(controller.channel_is_bad) channel.force_next_response = "# error\n\n" with tc.assertRaises(GtpProtocolError) as ar: controller.do_command("test") tc.assertEqual( str(ar.exception), "GTP protocol error reading response to 'test' from player test:\n" "no success/failure indication from engine: first line is `# error`") tc.assertTrue(controller.channel_is_bad) tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_controller_safe_close_with_error_from_quit(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.force_next_response = "# error\n\n" controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertListEqual(channel.engine.commands_handled, [('quit', [])]) tc.assertListEqual( controller.retrieve_error_messages(), ["GTP protocol error reading response to first command (quit) " "from player test:\n" "no success/failure indication from engine: first line is `# error`"])
def test_controller_response_transport_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertFalse(controller.channel_is_bad) channel.fail_next_response = True with tc.assertRaises(GtpTransportError) as ar: controller.do_command("test") tc.assertEqual( str(ar.exception), "transport error reading response to first command (test) " "from player test:\n" "forced failure for get_response_line") tc.assertTrue(controller.channel_is_bad) tc.assertListEqual(controller.retrieve_error_messages(), [])
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_controller_alt_exit(tc): channel = gtp_engine_fixtures.get_test_channel() channel.engine_exit_breaks_commands = False controller = Gtp_controller(channel, 'player test') controller.do_command("quit") tc.assertFalse(controller.channel_is_bad) with tc.assertRaises(GtpChannelClosed) as ar: controller.do_command("test") tc.assertEqual(str(ar.exception), "error reading response to 'test' from player test:\n" "engine has closed the response channel") tc.assertTrue(controller.channel_is_bad) controller.close() tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_controller_safe_close_with_failure_response_from_quit(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') channel.engine.force_error("quit") controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertListEqual(channel.engine.commands_handled, [('quit', [])]) error_messages = controller.retrieve_error_messages() tc.assertEqual(len(error_messages), 1) tc.assertEqual( error_messages[0], "failure response from first command (quit) to player test:\n" "handler forced to fail")
def test_engine_description_from_channel(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertIsNone(ed.raw_name) tc.assertIsNone(ed.raw_version) tc.assertIsNone(ed.description) channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args: "test engine") channel.engine.add_command('version', lambda args: "1.2.3") controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertEqual(ed.raw_name, "test engine") tc.assertEqual(ed.raw_version, "1.2.3") tc.assertIsNone(ed.description) channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args: "test engine") channel.engine.add_command('version', lambda args: "1.2.3") channel.engine.add_command('gomill-describe_engine', lambda args: "foo\nbar") controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertEqual(ed.raw_name, "test engine") tc.assertEqual(ed.raw_version, "1.2.3") tc.assertEqual(ed.description, "foo\nbar") channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_error('name') channel.engine.force_error('version') channel.engine.force_error('gomill-describe_engine') controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertIsNone(ed.raw_name) tc.assertIsNone(ed.raw_version) tc.assertIsNone(ed.description)
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_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_controller_close(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertFalse(controller.channel_is_closed) tc.assertEqual(controller.do_command("test"), "test response") tc.assertFalse(controller.channel_is_closed) tc.assertFalse(controller.channel.is_closed) controller.close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertRaisesRegexp(StandardError, "^channel is closed$", controller.do_command, "test") tc.assertRaisesRegexp(StandardError, "^channel is closed$", controller.close) tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_controller_alt_exit(tc): channel = gtp_engine_fixtures.get_test_channel() channel.engine_exit_breaks_commands = False controller = Gtp_controller(channel, 'player test') controller.do_command("quit") tc.assertFalse(controller.channel_is_bad) with tc.assertRaises(GtpChannelClosed) as ar: controller.do_command("test") tc.assertEqual( str(ar.exception), "error reading response to 'test' from player test:\n" "engine has closed the response channel") tc.assertTrue(controller.channel_is_bad) controller.close() tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_controller_safe_close(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertFalse(controller.channel_is_closed) tc.assertEqual(controller.do_command("test"), "test response") tc.assertFalse(controller.channel_is_closed) tc.assertFalse(controller.channel.is_closed) controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertListEqual(channel.engine.commands_handled, [('test', []), ('quit', [])]) # safe to call twice controller.safe_close() tc.assertListEqual(controller.retrieve_error_messages(), [])
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_engine_description_from_channel(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertIsNone(ed.raw_name) tc.assertIsNone(ed.raw_version) tc.assertIsNone(ed.description) channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") channel.engine.add_command('version', lambda args:"1.2.3") controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertEqual(ed.raw_name, "test engine") tc.assertEqual(ed.raw_version, "1.2.3") tc.assertIsNone(ed.description) channel = gtp_engine_fixtures.get_test_channel() channel.engine.add_command('name', lambda args:"test engine") channel.engine.add_command('version', lambda args:"1.2.3") channel.engine.add_command('gomill-describe_engine', lambda args:"foo\nbar") controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertEqual(ed.raw_name, "test engine") tc.assertEqual(ed.raw_version, "1.2.3") tc.assertEqual(ed.description, "foo\nbar") channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_error('name') channel.engine.force_error('version') channel.engine.force_error('gomill-describe_engine') controller = Gtp_controller(channel, 'player test') ed = gtp_controller.Engine_description.from_controller(controller) tc.assertIsNone(ed.raw_name) tc.assertIsNone(ed.raw_version) tc.assertIsNone(ed.description)
def test_controller_safe_close_after_error(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.do_command("test"), "test response") tc.assertFalse(controller.channel_is_bad) channel.force_next_response = "# error\n\n" with tc.assertRaises(GtpProtocolError) as ar: controller.do_command("test") tc.assertTrue(controller.channel_is_bad) # doesn't send quit when channel_is_bad controller.safe_close() tc.assertTrue(controller.channel_is_closed) tc.assertTrue(controller.channel.is_closed) tc.assertListEqual(channel.engine.commands_handled, [('test', []), ('test', [])]) tc.assertListEqual(controller.retrieve_error_messages(), [])
def test_safe_do_command(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.safe_do_command("test", "ab"), "args: ab") with tc.assertRaises(BadGtpResponse) as ar: controller.safe_do_command("error") tc.assertFalse(controller.channel_is_bad) channel.fail_next_response = True tc.assertIsNone(controller.safe_do_command("test")) tc.assertTrue(controller.channel_is_bad) tc.assertIsNone(controller.safe_do_command("test")) tc.assertListEqual( controller.retrieve_error_messages(), ["transport error reading response to 'test' from player test:\n" "forced failure for get_response_line"]) controller.safe_close() # check that third 'test' wasn't sent, and nor was 'quit' tc.assertListEqual(channel.engine.commands_handled, [('test', ['ab']), ('error', []), ('test', [])])
def test_gtp_aliases_safe(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'alias test') controller.set_gtp_aliases({ 'aliased' : 'test', 'aliased2' : 'nonesuch', }) tc.assertIs(controller.safe_known_command("test"), True) tc.assertIs(controller.safe_known_command("aliased"), True) tc.assertIs(controller.safe_known_command("nonesuch"), False) tc.assertIs(controller.safe_known_command("test"), True) tc.assertIs(controller.safe_known_command("aliased"), True) tc.assertIs(controller.safe_known_command("nonesuch"), False) tc.assertEqual(controller.safe_do_command("test"), "test response") tc.assertEqual(controller.safe_do_command("aliased"), "test response") with tc.assertRaises(BadGtpResponse) as ar: controller.safe_do_command("aliased2") tc.assertEqual(ar.exception.gtp_error_message, "unknown command") tc.assertEqual(ar.exception.gtp_command, "nonesuch")
def test_gtp_aliases_safe(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'alias test') controller.set_gtp_aliases({ 'aliased': 'test', 'aliased2': 'nonesuch', }) tc.assertIs(controller.safe_known_command("test"), True) tc.assertIs(controller.safe_known_command("aliased"), True) tc.assertIs(controller.safe_known_command("nonesuch"), False) tc.assertIs(controller.safe_known_command("test"), True) tc.assertIs(controller.safe_known_command("aliased"), True) tc.assertIs(controller.safe_known_command("nonesuch"), False) tc.assertEqual(controller.safe_do_command("test"), "test response") tc.assertEqual(controller.safe_do_command("aliased"), "test response") with tc.assertRaises(BadGtpResponse) as ar: controller.safe_do_command("aliased2") tc.assertEqual(ar.exception.gtp_error_message, "unknown command") tc.assertEqual(ar.exception.gtp_command, "nonesuch")
def test_safe_do_command(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'player test') tc.assertEqual(controller.safe_do_command("test", "ab"), "args: ab") with tc.assertRaises(BadGtpResponse) as ar: controller.safe_do_command("error") tc.assertFalse(controller.channel_is_bad) channel.fail_next_response = True tc.assertIsNone(controller.safe_do_command("test")) tc.assertTrue(controller.channel_is_bad) tc.assertIsNone(controller.safe_do_command("test")) tc.assertListEqual(controller.retrieve_error_messages(), [ "transport error reading response to 'test' from player test:\n" "forced failure for get_response_line" ]) controller.safe_close() # check that third 'test' wasn't sent, and nor was 'quit' tc.assertListEqual(channel.engine.commands_handled, [('test', ['ab']), ('error', []), ('test', [])])
def test_check_protocol_version(tc): channel = gtp_engine_fixtures.get_test_channel() controller = Gtp_controller(channel, 'pv test') controller.check_protocol_version()
def controller5(): channel = gtp_engine_fixtures.get_test_channel() channel.engine.force_fatal_error('gomill-cpu_time') return Gtp_controller(channel, 'fatalerror')
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(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', []), ])