diff --git a/tests/api_mock_test.py b/tests/api_mock_test.py index b7bd7d6..5445809 100644 --- a/tests/api_mock_test.py +++ b/tests/api_mock_test.py @@ -10,7 +10,7 @@ @pytest.fixture() def non_mocked_hosts() -> list: - return ["9c-main-miner-3.nine-chronicles.com"] + return ["9c-main-full-state.nine-chronicles.com"] @pytest.mark.parametrize("has_header", [True, False]) diff --git a/tests/data_provider_test.py b/tests/data_provider_test.py index 43323b7..2e31056 100644 --- a/tests/data_provider_test.py +++ b/tests/data_provider_test.py @@ -2,9 +2,10 @@ from typing import List import pytest +from pytest_httpx import HTTPXMock from world_boss.app.cache import cache_exists, set_to_cache -from world_boss.app.data_provider import data_provider_client +from world_boss.app.data_provider import DATA_PROVIDER_URLS, data_provider_client from world_boss.app.enums import NetworkType from world_boss.app.stubs import RankingRewardDictionary @@ -45,3 +46,26 @@ def test_get_ranking_rewards( result = data_provider_client.get_ranking_rewards(raid_id, network_type, 0, 1) assert result == expected_result assert cache_exists(cache_key) + + +def test_get_ranking_rewards_error( + redis_proc, + fx_app, + httpx_mock: HTTPXMock, +): + offset = 0 + limit = 1 + raid_id = 99 + network_type = NetworkType.MAIN + cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_{limit}" + httpx_mock.add_response( + method="POST", + url=DATA_PROVIDER_URLS[network_type], + json={ + "errors": [{"message": "can't receive"}], + "data": {"worldBossRankingRewards": None}, + }, + ) + with pytest.raises(Exception): + data_provider_client.get_ranking_rewards(raid_id, network_type, 0, 1) + assert not cache_exists(cache_key) diff --git a/tests/kms_test.py b/tests/kms_test.py index 3312ac5..9da0d5c 100644 --- a/tests/kms_test.py +++ b/tests/kms_test.py @@ -4,6 +4,7 @@ import bencodex import pytest +from gql.transport.exceptions import TransportQueryError from world_boss.app.enums import NetworkType from world_boss.app.kms import HEADLESS_URLS, MINER_URLS, signer @@ -41,7 +42,7 @@ def test_transfer_assets(fx_session) -> None: tx = bencodex.loads(bytes.fromhex(payload)) assert tx[b"n"] == 2 action = tx[b"a"][0] - assert action["type_id"] == "transfer_assets" + assert action["type_id"] == "transfer_assets3" plain_value = action["values"] assert plain_value["memo"] == "test" assert len(plain_value["recipients"]) == 1 @@ -51,9 +52,8 @@ def test_transfer_assets(fx_session) -> None: async def test_stage_transactions_async(fx_session, fx_mainnet_transactions): fx_session.add_all(fx_mainnet_transactions) fx_session.flush() - result = await signer.stage_transactions_async(NetworkType.INTERNAL) - for transaction in fx_mainnet_transactions: - assert transaction.tx_id in result + with pytest.raises(TransportQueryError): + await signer.stage_transactions_async(NetworkType.INTERNAL) @pytest.mark.asyncio @@ -95,7 +95,9 @@ def test_stage_transaction(fx_session, fx_mainnet_transactions): fx_session.flush() urls = HEADLESS_URLS[NetworkType.INTERNAL] for url in urls: - assert signer.stage_transaction(url, tx) == tx.tx_id + with pytest.raises(TransportQueryError) as e: + signer.stage_transaction(url, tx) + assert tx.tx_id in str(e.value) def test_query_transaction_result(fx_session, fx_mainnet_transactions): diff --git a/tests/tasks_test.py b/tests/tasks_test.py index a3f0362..006800a 100644 --- a/tests/tasks_test.py +++ b/tests/tasks_test.py @@ -124,6 +124,38 @@ def test_get_ranking_rewards( assert redisdb.exists(f"world_boss_agents_{raid_id}_{network_type}_100_1") +def test_get_ranking_rewards_error( + redisdb, + celery_session_worker, + httpx_mock: HTTPXMock, + fx_ranking_rewards, +): + raid_id = 20 + network_type = NetworkType.MAIN + offset = 0 + + httpx_mock.add_response( + method="POST", + url=DATA_PROVIDER_URLS[NetworkType.MAIN], + json={ + "errors": [{"message": "can't receive"}], + "data": {"worldBossRankingRewards": None}, + }, + ) + + with unittest.mock.patch( + "world_boss.app.tasks.client.chat_postMessage" + ) as m, pytest.raises(Exception): + get_ranking_rewards.delay("channel_id", raid_id, 101, 1).get(timeout=10) + m.assert_called_once() + kwargs = m.call_args.kwargs + assert ( + kwargs["text"] + == "failed to get rewards from https://api.9c.gg/graphql exc: can't receive" + ) + assert kwargs["channel"] == "channel_id" + + @pytest.mark.parametrize( "nonce, max_nonce, nonce_list, expected_count", [ diff --git a/world_boss/app/data_provider.py b/world_boss/app/data_provider.py index c7ec7b1..83d354c 100644 --- a/world_boss/app/data_provider.py +++ b/world_boss/app/data_provider.py @@ -64,6 +64,8 @@ def get_ranking_rewards( RANKING_REWARDS_QUERY, {"raidId": raid_id, "offset": offset, "limit": limit}, ) + if result.get("errors"): + raise Exception(result["errors"][0]["message"]) rewards = result["data"]["worldBossRankingRewards"] set_to_cache(cache_key, json.dumps(rewards)) return rewards diff --git a/world_boss/app/kms.py b/world_boss/app/kms.py index 03ad70f..6176fc6 100644 --- a/world_boss/app/kms.py +++ b/world_boss/app/kms.py @@ -22,7 +22,7 @@ MINER_URLS: dict[NetworkType, str] = { NetworkType.MAIN: "https://9c-main-full-state.nine-chronicles.com/graphql", - NetworkType.INTERNAL: "http://9c-internal-miner-1.nine-chronicles.com/graphql", + NetworkType.INTERNAL: "http://9c-internal-validator-5.nine-chronicles.com/graphql", } HEADLESS_URLS: dict[NetworkType, typing.List[str]] = { diff --git a/world_boss/app/tasks.py b/world_boss/app/tasks.py index 6a8b2a8..64c13a1 100644 --- a/world_boss/app/tasks.py +++ b/world_boss/app/tasks.py @@ -5,7 +5,7 @@ import bencodex from celery import Celery -from world_boss.app.data_provider import data_provider_client +from world_boss.app.data_provider import DATA_PROVIDER_URLS, data_provider_client from world_boss.app.enums import NetworkType from world_boss.app.kms import MINER_URLS, signer from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount @@ -44,9 +44,16 @@ def get_ranking_rewards( size = 100 results: List[RankingRewardWithAgentDictionary] = [] while len(results) < total_count: - result = data_provider_client.get_ranking_rewards( - raid_id, NetworkType.MAIN, offset, size - ) + try: + result = data_provider_client.get_ranking_rewards( + raid_id, NetworkType.MAIN, offset, size + ) + except Exception as e: + client.chat_postMessage( + channel=channel_id, + text=f"failed to get rewards from {DATA_PROVIDER_URLS[NetworkType.MAIN]} exc: {e}", + ) + raise e rewards = update_agent_address(result, raid_id, NetworkType.MAIN, offset, size) results.extend(reward for reward in rewards if reward not in results) offset = len(results)