Exemplo n.º 1
0
async def test_abort(game_connection: GameConnection, game: Game, players):
    game_connection.player = players.hosting
    game_connection.game = game

    await game_connection.abort()

    game.remove_game_connection.assert_called_with(game_connection)
Exemplo n.º 2
0
async def test_handle_action_GameMods_not_host(game: Game,
                                               game_connection: GameConnection,
                                               players):
    game_connection.player = players.joining
    mods = game.mods
    await game_connection.handle_action('GameMods', ['uids', 'foo baz'])
    assert game.mods == mods
Exemplo n.º 3
0
async def test_disconnect_all_peers(game_connection: GameConnection,
                                    real_game: Game, players):
    real_game.state = GameState.LOBBY
    game_connection.player = players.hosting
    game_connection.game = real_game

    disconnect_done = mock.Mock()

    async def fake_send_dc(player_id):
        await asyncio.sleep(1)  # Take some time
        disconnect_done.success()
        return "OK"

    # Set up a peer that will disconnect without error
    ok_disconnect = asynctest.create_autospec(GameConnection)
    ok_disconnect.state = GameConnectionState.CONNECTED_TO_HOST
    ok_disconnect.send_DisconnectFromPeer = fake_send_dc

    # Set up a peer that will throw an exception
    fail_disconnect = asynctest.create_autospec(GameConnection)
    fail_disconnect.send_DisconnectFromPeer.return_value = Exception(
        "Test exception")
    fail_disconnect.state = GameConnectionState.CONNECTED_TO_HOST

    # Add the peers to the game
    real_game.add_game_connection(fail_disconnect)
    real_game.add_game_connection(ok_disconnect)

    await game_connection.disconnect_all_peers()

    disconnect_done.success.assert_called_once()
Exemplo n.º 4
0
async def test_handle_action_GameState_launching_calls_launch(game_connection: GameConnection, players, game):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroMock()

    await game_connection.handle_action('GameState', ['Launching'])

    game.launch.assert_any_call()
Exemplo n.º 5
0
async def test_handle_action_GameState_launching_calls_launch(
        game_connection: GameConnection, players, game):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroMock()

    await game_connection.handle_action('GameState', ['Launching'])

    game.launch.assert_any_call()
Exemplo n.º 6
0
async def test_handle_action_GameState_idle_adds_connection(
        game: Game, game_connection: GameConnection, players):
    players.joining.game = game
    game_connection.player = players.hosting
    game_connection.game = game

    await game_connection.handle_action('GameState', ['Idle'])

    game.add_game_connection.assert_called_with(game_connection)
Exemplo n.º 7
0
async def test_handle_action_GameState_idle_non_searching_player_aborts(game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.lobby = mock.Mock()
    game_connection.abort = mock.Mock()
    players.hosting.state = PlayerState.IDLE

    await game_connection.handle_action('GameState', ['Idle'])

    game_connection.abort.assert_any_call()
Exemplo n.º 8
0
async def test_handle_action_GameState_launching_calls_launch(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroutineMock()

    await game_connection.handle_action("GameState", ["Launching"])

    game.launch.assert_any_call()
Exemplo n.º 9
0
async def test_handle_action_GameState_lobby_sends_HostGame(game_connection: GameConnection, loop, players, game):
    game_connection.player = players.hosting
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
Exemplo n.º 10
0
async def test_handle_action_GameState_idle_non_searching_player_aborts(
        game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.lobby = mock.Mock()
    game_connection.abort = mock.Mock()
    players.hosting.state = PlayerState.IDLE

    await game_connection.handle_action('GameState', ['Idle'])

    game_connection.abort.assert_any_call()
Exemplo n.º 11
0
async def test_handle_action_GameState_lobby_sends_HostGame(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.player = players.hosting
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    await exhaust_callbacks(event_loop)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
Exemplo n.º 12
0
async def test_handle_action_GameState_lobby_sends_HostGame(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.player = players.hosting
    game.map_file_path = "maps/some_map.zip"
    game.map_folder_name = "some_map"

    await game_connection.handle_action("GameState", ["Lobby"])
    await exhaust_callbacks(event_loop)

    assert_message_sent(game_connection, "HostGame", [game.map_folder_name])
Exemplo n.º 13
0
async def test_handle_action_GameState_lobby_sends_HostGame(
        game_connection: GameConnection, loop, players, game):
    game_connection.player = players.hosting
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
Exemplo n.º 14
0
 def make_connection(player, connectivity):
     lc = LobbyConnection(loop)
     lc.protocol = mock.Mock()
     conn = GameConnection(loop=loop,
                           lobby_connection=lc,
                           player_service=player_service,
                           games=game_service)
     conn.player = player
     conn.game = game
     conn._transport = transport
     conn._connectivity_state.set_result(connectivity)
     return conn
Exemplo n.º 15
0
 def make_connection(player, connectivity):
     lc = LobbyConnection(loop)
     lc.protocol = mock.Mock()
     conn = GameConnection(loop=loop,
                           lobby_connection=lc,
                           player_service=player_service,
                           games=game_service)
     conn.player = player
     conn.game = game
     conn._transport = transport
     conn._connectivity_state.set_result(connectivity)
     return conn
Exemplo n.º 16
0
async def test_handle_action_GameState_lobby_calls_ConnectToHost(game_connection: GameConnection, players, game):
    game_connection.send_message = mock.MagicMock()
    game_connection.ConnectToHost = CoroMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.map_file_path = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.ConnectToHost.assert_called_with(players.hosting.game_connection)
Exemplo n.º 17
0
async def test_handle_action_GameState_lobby_calls_ConnectToHost(
        game_connection: GameConnection, players, game):
    game_connection.send_message = mock.MagicMock()
    game_connection.ConnectToHost = CoroMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.map_file_path = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.ConnectToHost.assert_called_with(
        players.hosting.game_connection)
Exemplo n.º 18
0
async def test_handle_action_GameState_lobby_calls_abort(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.send = CoroutineMock()
    game_connection.abort = CoroutineMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.host.state = PlayerState.IDLE
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    await exhaust_callbacks(event_loop)

    game_connection.abort.assert_called_once()
Exemplo n.º 19
0
async def test_handle_action_GameState_lobby_calls_ConnectToHost(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.send = CoroutineMock()
    game_connection.connect_to_host = CoroutineMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.map_file_path = "maps/some_map.zip"
    game.map_folder_name = "some_map"

    await game_connection.handle_action("GameState", ["Lobby"])
    await exhaust_callbacks(event_loop)

    game_connection.connect_to_host.assert_called_with(
        players.hosting.game_connection)
Exemplo n.º 20
0
async def test_handle_lobby_state_handles_GameError(
        real_game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.abort = CoroutineMock()
    game_connection.connect_to_host = CoroutineMock()
    game_connection.player = players.joining
    game_connection.game = real_game

    players.joining.game = real_game

    real_game.host = players.hosting
    real_game.state = GameState.ENDED

    await game_connection.handle_action('GameState', ['Lobby'])
    await exhaust_callbacks(event_loop)

    game_connection.abort.assert_called_once()
Exemplo n.º 21
0
def game_connection(request, game, loop, player_service, players, game_service, transport):
    from server import GameConnection, LobbyConnection
    conn = GameConnection(loop=loop,
                          lobby_connection=mock.create_autospec(LobbyConnection(loop)),
                          player_service=player_service,
                          games=game_service)
    conn._transport = transport
    conn.player = players.hosting
    conn.game = game
    conn.lobby = mock.Mock(spec=LobbyConnection)

    def fin():
        conn.abort()

    request.addfinalizer(fin)
    return conn
Exemplo n.º 22
0
def game_connection(request, game, loop, player_service, players, game_service,
                    transport):
    from server import GameConnection, LobbyConnection
    conn = GameConnection(loop=loop,
                          lobby_connection=mock.create_autospec(
                              LobbyConnection(loop)),
                          player_service=player_service,
                          games=game_service)
    conn._transport = transport
    conn.player = players.hosting
    conn.game = game
    conn.lobby = mock.Mock(spec=LobbyConnection)

    def fin():
        conn.abort()

    request.addfinalizer(fin)
    return conn
Exemplo n.º 23
0
async def test_handle_action_GameState_lobby_calls_ConnectToPeer(
        game: Game, game_connection: GameConnection, players):
    game_connection.send_message = mock.MagicMock()
    game_connection.connect_to_host = CoroMock()
    game_connection.connect_to_peer = CoroMock()
    game_connection.player = players.joining

    players.joining.game = game

    game.host = players.hosting
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'
    game.connections = [players.peer.game_connection]

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.connect_to_peer.assert_called_with(
        players.peer.game_connection)
Exemplo n.º 24
0
async def test_handle_action_GameState_lobby_calls_ConnectToPeer(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.send = CoroutineMock()
    game_connection.connect_to_host = CoroutineMock()
    game_connection.connect_to_peer = CoroutineMock()
    game_connection.player = players.joining

    players.joining.game = game

    game.host = players.hosting
    game.map_file_path = 'maps/some_map.zip'
    game.map_folder_name = 'some_map'
    peer_conn = mock.Mock()
    players.peer.game_connection = peer_conn
    game.connections = [peer_conn]

    await game_connection.handle_action('GameState', ['Lobby'])
    await exhaust_callbacks(event_loop)

    game_connection.connect_to_peer.assert_called_with(peer_conn)
Exemplo n.º 25
0
async def test_handle_action_PlayerOption_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    await game_connection.handle_action('PlayerOption', [1, 'Color', 2])
    game.set_player_option.assert_not_called()
Exemplo n.º 26
0
async def test_handle_action_AIOption_not_host(game: Game,
                                               game_connection: GameConnection,
                                               players):
    game_connection.player = players.joining
    await game_connection.handle_action('AIOption', ['QAI', 'StartSpot', 1])
    game.set_ai_option.assert_not_called()
Exemplo n.º 27
0
async def test_handle_action_GameOption_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    game.gameOptions = {"Victory": "asdf"}
    await game_connection.handle_action('GameOption', ['Victory', 'sandbox'])
    assert game.gameOptions == {"Victory": "asdf"}
Exemplo n.º 28
0
async def test_handle_action_ClearSlot_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    await game_connection.handle_action('ClearSlot', [1])
    game.clear_slot.assert_not_called()