You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import (
|
|
Any,
|
|
)
|
|
from unittest import (
|
|
IsolatedAsyncioTestCase,
|
|
)
|
|
from unittest.mock import (
|
|
MagicMock,
|
|
)
|
|
|
|
from enjo_lib.comm.method.internal import (
|
|
InternalExchanger,
|
|
InternalTransport,
|
|
)
|
|
|
|
|
|
# adapted from https://stackoverflow.com/a/32498408
|
|
class AsyncCallableMock(MagicMock):
|
|
async def __call__(self, *args: Any, **kwargs: Any) -> Any:
|
|
return super().__call__(*args, **kwargs)
|
|
|
|
|
|
class InternalTransportTwoPeers(IsolatedAsyncioTestCase):
|
|
|
|
async def test_simple_two_peers(self) -> None:
|
|
# TODO move much stuff to setup methods
|
|
exchange = InternalExchanger()
|
|
# setup peers
|
|
peer_alice = InternalTransport()
|
|
exchange.register_transport(peer_alice)
|
|
peer_bob = InternalTransport()
|
|
exchange.register_transport(peer_bob)
|
|
# setup mock receiving services
|
|
mock_alice = AsyncCallableMock()
|
|
peer_alice.on_receive(mock_alice)
|
|
mock_bob = AsyncCallableMock()
|
|
peer_bob.on_receive(mock_bob)
|
|
# send from Alice to Bob
|
|
await peer_alice.send(b"test Alice -> Bob")
|
|
mock_alice.assert_not_called()
|
|
mock_bob.assert_called_once_with(b"test Alice -> Bob")
|
|
mock_alice.reset_mock()
|
|
mock_bob.reset_mock()
|
|
# send from Bob to Alice
|
|
await peer_bob.send(b"test Bob -> Alice")
|
|
mock_alice.assert_called_once_with(b"test Bob -> Alice")
|
|
mock_bob.assert_not_called()
|