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_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_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_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_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_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_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_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', [])])