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"))
Exemple #2
0
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_check_protocol_version_2(tc):
    channel = Preprogrammed_gtp_channel("= 1\n\n? error\n\n# unreached\n\n")
    controller = Gtp_controller(channel, 'pv2 test')
    with tc.assertRaises(BadGtpResponse) as ar:
        controller.check_protocol_version()
    tc.assertEqual(str(ar.exception), "pv2 test reports GTP protocol version 1")
    tc.assertEqual(ar.exception.gtp_error_message, None)
    # check error is not treated as a check failure
    controller.check_protocol_version()
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'])
Exemple #5
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(), [])
Exemple #6
0
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_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_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(), [])
Exemple #9
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(), [])
Exemple #10
0
def test_subprocess_channel_with_controller(tc):
    # Also tests that leaving 'env' and 'cwd' unset works
    fx = gtp_engine_fixtures.State_reporter_fixture(tc)
    channel = gtp_controller.Subprocess_gtp_channel(fx.cmd, stderr=fx.devnull)
    controller = Gtp_controller(channel, 'subprocess test')
    tc.assertEqual(controller.do_command("tell"),
                   "cwd: %s\nGOMILL_TEST:None" % os.getcwd())
    controller.close()
    tc.assertEqual(channel.exit_status, 0)
    rusage = channel.resource_usage
    tc.assertTrue(hasattr(rusage, 'ru_utime'))
def test_subprocess_channel_with_controller(tc):
    # Also tests that leaving 'env' and 'cwd' unset works
    fx = gtp_engine_fixtures.State_reporter_fixture(tc)
    channel = gtp_controller.Subprocess_gtp_channel(fx.cmd, stderr=fx.devnull)
    controller = Gtp_controller(channel, 'subprocess test')
    tc.assertEqual(controller.do_command("tell"),
                   "cwd: %s\nGOMILL_TEST:None" % os.getcwd())
    controller.close()
    tc.assertEqual(channel.exit_status, 0)
    rusage = channel.resource_usage
    tc.assertTrue(hasattr(rusage, 'ru_utime'))
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(), [])
Exemple #13
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"])
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"])
Exemple #15
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`"
    ])
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_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`"])
Exemple #18
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(), [])
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(), [])
Exemple #20
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")
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_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(), [])
Exemple #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(), [])
Exemple #24
0
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)
Exemple #25
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', [])])
Exemple #26
0
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")
Exemple #27
0
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(), [])
Exemple #29
0
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"))
Exemple #30
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(), [])
Exemple #31
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(), [])
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', [])])
Exemple #33
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(), [])
Exemple #34
0
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)
Exemple #35
0
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)
Exemple #36
0
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)
Exemple #37
0
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")
Exemple #38
0
def test_check_protocol_version_2(tc):
    channel = Preprogrammed_gtp_channel("= 1\n\n? error\n\n# unreached\n\n")
    controller = Gtp_controller(channel, 'pv2 test')
    with tc.assertRaises(BadGtpResponse) as ar:
        controller.check_protocol_version()
    tc.assertEqual(str(ar.exception),
                   "pv2 test reports GTP protocol version 1")
    tc.assertEqual(ar.exception.gtp_error_message, None)
    # check error is not treated as a check failure
    controller.check_protocol_version()
Exemple #39
0
def test_known_command_2(tc):
    # Checking that known_command caches its responses
    # and that it treats an error or unknown value the same as 'false'.
    channel = Preprogrammed_gtp_channel(
        "= true\n\n= absolutely not\n\n? error\n\n# unreached\n\n")
    controller = Gtp_controller(channel, 'kc2 test')
    tc.assertTrue(controller.known_command("one"))
    tc.assertFalse(controller.known_command("two"))
    tc.assertFalse(controller.known_command("three"))
    tc.assertTrue(controller.known_command("one"))
    tc.assertFalse(controller.known_command("two"))
    tc.assertEqual(
        channel.get_command_stream(),
        "known_command one\nknown_command two\nknown_command three\n")
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_known_command_2(tc):
    # Checking that known_command caches its responses
    # and that it treats an error or unknown value the same as 'false'.
    channel = Preprogrammed_gtp_channel(
        "= true\n\n= absolutely not\n\n? error\n\n# unreached\n\n")
    controller = Gtp_controller(channel, 'kc2 test')
    tc.assertTrue(controller.known_command("one"))
    tc.assertFalse(controller.known_command("two"))
    tc.assertFalse(controller.known_command("three"))
    tc.assertTrue(controller.known_command("one"))
    tc.assertFalse(controller.known_command("two"))
    tc.assertEqual(
        channel.get_command_stream(),
        "known_command one\nknown_command two\nknown_command three\n")
Exemple #43
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(), [])
Exemple #44
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(), [])
Exemple #45
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(), [])
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_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_check_protocol_version(tc):
    channel = gtp_engine_fixtures.get_test_channel()
    controller = Gtp_controller(channel, 'pv test')
    controller.check_protocol_version()
Exemple #49
0
def test_check_protocol_version(tc):
    channel = gtp_engine_fixtures.get_test_channel()
    controller = Gtp_controller(channel, 'pv test')
    controller.check_protocol_version()
Exemple #50
0
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', []),
    ])
Exemple #51
0
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")