Esempio n. 1
0
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(), [])
Esempio n. 2
0
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(), [])
Esempio n. 3
0
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(), [])
Esempio n. 4
0
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(), [])
Esempio n. 5
0
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(), [])
Esempio n. 6
0
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(), [])
Esempio n. 7
0
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(), [])
Esempio n. 8
0
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"])
Esempio n. 9
0
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(), [])
Esempio n. 10
0
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"])
Esempio n. 11
0
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(), [])
Esempio n. 12
0
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`"
    ])
Esempio n. 13
0
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(), [])
Esempio n. 14
0
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")
Esempio n. 15
0
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(), [])
Esempio n. 16
0
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`"])
Esempio n. 17
0
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(), [])
Esempio n. 18
0
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(), [])
Esempio n. 19
0
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(), [])
Esempio n. 20
0
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(), [])
Esempio n. 21
0
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(), [])
Esempio n. 22
0
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(), [])
Esempio n. 23
0
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(), [])
Esempio n. 24
0
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")
Esempio n. 25
0
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(), [])
Esempio n. 26
0
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(), [])
Esempio n. 27
0
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', [])])
Esempio n. 28
0
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', [])])