async def test_no_raw_tx(): """Test no raw tx returned.""" dispatcher = LedgerApiRequestDispatcher(ConnectionStatus()) mock_api = Mock() contract_api_dialogues = ContractApiDialogues() message = LedgerApiMessage( performative=LedgerApiMessage.Performative.GET_RAW_TRANSACTION, dialogue_reference=contract_api_dialogues. new_self_initiated_dialogue_reference(), terms=Terms( ledger_id=EthereumCrypto.identifier, sender_address="1111", counterparty_address="22222", amount_by_currency_id={"ETH": -1}, quantities_by_good_id={"some_service_id": 1}, is_sender_payable_tx_fee=True, nonce="", fee_by_currency_id={"ETH": 10}, chain_id=3, ), ) message.counterparty = "test" dialogue = contract_api_dialogues.update(message) mock_api.get_transfer_transaction.return_value = None msg = dispatcher.get_raw_transaction(mock_api, message, dialogue) assert msg.performative == LedgerApiMessage.Performative.ERROR
def __init__(self, connections: List['Connection'], default_connection_index: int = 0, loop: Optional[AbstractEventLoop] = None): """ Initialize the connection multiplexer. :param connections: the connections. :param default_connection_index: the index of the connection to use as default. | this information is used for envelopes which | don't specify any routing context. :param loop: the event loop to run the multiplexer. If None, a new event loop is created. """ assert len(connections) > 0, "List of connections cannot be empty." assert 0 <= default_connection_index <= len( connections) - 1, "Default connection index out of range." assert len(set(c.connection_id for c in connections)) == len( connections), "Connection names must be unique." self._connections = connections # type: List['Connection'] self._name_to_connection = {c.connection_id: c for c in connections } # type: Dict[str, Connection] self.default_connection = self._connections[ default_connection_index] # type: Connection self._connection_status = ConnectionStatus() self._lock = Lock() self._loop = loop if loop is not None else asyncio.new_event_loop() self._thread = Thread(target=self._run_loop) self._in_queue = queue.Queue() # type: queue.Queue self._out_queue = None # type: Optional[asyncio.Queue] self._recv_loop_task = None # type: Optional[Future] self._send_loop_task = None # type: Optional[Future]
async def test_run_async(): """Test run async error handled.""" # for pydocstyle def _raise(): raise Exception("Expected") contract_api_dialogues = ContractApiDialogues() message = ContractApiMessage( performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION, dialogue_reference=contract_api_dialogues.new_self_initiated_dialogue_reference(), ledger_id=EthereumCrypto.identifier, contract_id="fetchai/erc1155:0.6.0", contract_address="test addr", callable="get_create_batch_transaction", kwargs=ContractApiMessage.Kwargs( { "deployer_address": "test_addr", "token_ids": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], } ), ) message.counterparty = "test" dialogue = contract_api_dialogues.update(message) api = None msg = await ContractApiRequestDispatcher(ConnectionStatus()).run_async( _raise, api, message, dialogue ) assert msg.performative == ContractApiMessage.Performative.ERROR
def __init__( self, connections: Optional[Sequence[Connection]] = None, default_connection_index: int = 0, loop: Optional[AbstractEventLoop] = None, ): """ Initialize the connection multiplexer. :param connections: a sequence of connections. :param default_connection_index: the index of the connection to use as default. This information is used for envelopes which don't specify any routing context. If connections is None, this parameter is ignored. :param loop: the event loop to run the multiplexer. If None, a new event loop is created. :param agent_name: the name of the agent that owns the multiplexer, for logging purposes. """ super().__init__(default_logger) self._connections: List[Connection] = [] self._id_to_connection: Dict[PublicId, Connection] = {} self._default_connection: Optional[Connection] = None self._initialize_connections_if_any(connections, default_connection_index) self._connection_status = ConnectionStatus() self._in_queue = AsyncFriendlyQueue() # type: AsyncFriendlyQueue self._out_queue = None # type: Optional[asyncio.Queue] self._recv_loop_task = None # type: Optional[asyncio.Task] self._send_loop_task = None # type: Optional[asyncio.Task] self._default_routing = {} # type: Dict[PublicId, PublicId] self.set_loop(loop if loop is not None else asyncio.new_event_loop())
async def test_no_balance(): """Test no balance.""" dispatcher = LedgerApiRequestDispatcher(ConnectionStatus()) mock_api = Mock() contract_api_dialogues = ContractApiDialogues() message = LedgerApiMessage( performative=LedgerApiMessage.Performative.GET_BALANCE, dialogue_reference=contract_api_dialogues. new_self_initiated_dialogue_reference(), ledger_id=EthereumCrypto.identifier, address="test", ) message.counterparty = "test" dialogue = contract_api_dialogues.update(message) mock_api.get_balance.return_value = None msg = dispatcher.get_balance(mock_api, message, dialogue) assert msg.performative == LedgerApiMessage.Performative.ERROR
async def test_attempts_get_transaction_receipt(): """Test retry and sleep.""" dispatcher = LedgerApiRequestDispatcher(ConnectionStatus()) dispatcher.connection_status.is_connected = True mock_api = Mock() contract_api_dialogues = ContractApiDialogues() message = LedgerApiMessage( performative=LedgerApiMessage.Performative.GET_TRANSACTION_RECEIPT, dialogue_reference=contract_api_dialogues. new_self_initiated_dialogue_reference(), transaction_digest=TransactionDigest("asdad", "sdfdsf"), ) message.counterparty = "test" dialogue = contract_api_dialogues.update(message) mock_api.get_transaction.return_value = None mock_api.is_transaction_settled.return_value = True with patch.object(dispatcher, "MAX_ATTEMPTS", 2): with patch.object(dispatcher, "TIMEOUT", 0.001): msg = dispatcher.get_transaction_receipt(mock_api, message, dialogue) assert msg.performative == LedgerApiMessage.Performative.ERROR
async def test_get_handler(): """Test failed to get handler.""" with pytest.raises(Exception, match="Performative not recognized."): ContractApiRequestDispatcher(ConnectionStatus()).get_handler( ContractApiMessage.Performative.ERROR )