Skip to content

Commit

Permalink
Merge pull request #41 from planetarium/handle-ranking-rewards-error
Browse files Browse the repository at this point in the history
Handle ranking rewards exception
  • Loading branch information
ipdae authored Oct 11, 2023
2 parents d591979 + 7603cf2 commit 8e0ad33
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tests/api_mock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
26 changes: 25 additions & 1 deletion tests/data_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
12 changes: 7 additions & 5 deletions tests/kms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions tests/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
2 changes: 2 additions & 0 deletions world_boss/app/data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion world_boss/app/kms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {
Expand Down
15 changes: 11 additions & 4 deletions world_boss/app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8e0ad33

Please sign in to comment.