Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(concurrency): Implement all methods of IrohaClient with async/awaitt #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions playground/iroha.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import binascii
import os

from typing import (
Any,
Coroutine,
Expand All @@ -8,7 +10,8 @@
Iterable,
)
from dataclasses import dataclass
import os

from playground.concurrency import run_in_threadpool

from iroha import Iroha as Iroha
from iroha import IrohaCrypto as IrohaCrypto
Expand Down Expand Up @@ -85,14 +88,14 @@ def get_transactions(
str(tx.payload.reduced_payload.commands),
)

def get_asset_info(self, *, asset_id: str) -> Tuple[str, int]:
async def get_asset_info(self, *, asset_id: str) -> Tuple[str, int]:
def _get_asset_info(*, asset_id: str) -> Tuple[str, int]:
response = self._send_query('GetAssetInfo', asset_id=asset_id)
return (
str(response.asset_response.asset_id),
int(response.asset_response.precision),
)
return _get_asset_info(asset_id=asset_id)
return await run_in_threadpool(_get_asset_info, asset_id=asset_id)

def get_block(self, *, height: int=1) -> Any:
assert height > 0
Expand All @@ -111,4 +114,4 @@ def _send_query(self, name: str , **kwargs: Any) -> Any:
reason=err.reason or None,
error_code=err.error_code or None,
)
return response
return response
9 changes: 4 additions & 5 deletions playground/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ class ResolverException(Exception):


class AssetResolver:
def __call__(self, uri: str) -> typing.Union[Asset, ResolverException]:
async def __call__(self, uri: str) -> Asset:
client: IrohaClient = container.resolve(IrohaClient)
try:
_uri, precision = container.resolve(IrohaClient).get_asset_info(
asset_id=uri
)
_uri, precision = await client.get_asset_info(asset_id=uri)
return Asset(
uri=URI(_uri),
precision=precision,
)
except IrohaException as e:
return ResolverException(e)
raise ResolverException(e)


class TransactionResolver:
Expand Down
5 changes: 4 additions & 1 deletion playground/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

@strawberry.type
class Query:
asset: Asset = strawberry.field(resolver=AssetResolver().__call__)
@strawberry.field
async def asset(self, *, uri: str) -> Asset:
return await AssetResolver()(uri=uri)

transaction: typing.List[Transaction] = strawberry.field(
resolver=TransactionResolver().__call__
)
Expand Down
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
]

Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing
import pytest
from punq import Container

from playground.iroha import IrohaClient, IrohaException
Expand Down Expand Up @@ -68,7 +70,8 @@ def test_mutation_create_asset_exception(self, container: Container) -> None:

assert result.errors

def test_query_asset_ok(self, container: Container) -> None:
@pytest.mark.asyncio
async def test_query_asset_ok(self, container: Container) -> None:
from playground.app import schema

container.resolve(IrohaClient).get_asset_info.return_value = (
Expand Down Expand Up @@ -97,7 +100,7 @@ def test_query_asset_ok(self, container: Container) -> None:
"precision": 0,
}
}
result = schema.execute_sync(query)
result = await schema.execute(query)

assert not result.errors
assert result.data == expected
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/test_iroha.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


class TestIrohaClient:
def test_get_asset_info(self, container: Container) -> None:
@pytest.mark.asyncio
async def test_get_asset_info(self, container: Container) -> None:
mock_net = container.resolve(IrohaGrpc)
mock_net.send_query.return_value = Mock(
HasField=lambda *_: False,
Expand All @@ -24,8 +25,11 @@ def test_get_asset_info(self, container: Container) -> None:
account = container.resolve(IrohaAccount)
client = IrohaClient(account=account, net=mock_net)

(asset_id, precision,) = client.get_asset_info(
asset_id="foo#bar",
(
asset_id,
precision,
) = await client.get_asset_info(
asset_id='foo#bar',
)
assert asset_id == "foo#bar"
assert precision == 1
Expand Down