Skip to content

Commit

Permalink
Merge pull request #666 from sklump/unit-tests-more-transport
Browse files Browse the repository at this point in the history
cover more transport code
  • Loading branch information
andrewwhitehead authored Aug 17, 2020
2 parents 29069e9 + adcc98f commit c3d12c7
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 2 deletions.
113 changes: 112 additions & 1 deletion aries_cloudagent/transport/outbound/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ....config.injection_context import InjectionContext
from ....connections.models.connection_target import ConnectionTarget

from .. import manager as test_module
from ..manager import (
OutboundDeliveryError,
OutboundTransportManager,
Expand Down Expand Up @@ -83,6 +84,7 @@ async def test_send_message(self):
send_context = InjectionContext()
mgr.enqueue_message(send_context, message)
await mgr.flush()

transport.wire_format.encode_message.assert_awaited_once_with(
send_context,
message.payload,
Expand Down Expand Up @@ -182,9 +184,96 @@ async def test_process_finished_x(self):
mgr.finished_deliver(mock_queued, mock_task)
mgr.finished_deliver(mock_queued, mock_task)

async def test_process_loop_retry_now(self):
mock_queued = async_mock.MagicMock(
state=QueuedOutboundMessage.STATE_RETRY,
retry_at=test_module.get_timer() - 1,
)

context = InjectionContext()
mock_handle_not_delivered = async_mock.MagicMock()
mgr = OutboundTransportManager(context, mock_handle_not_delivered)
mgr.outbound_buffer.append(mock_queued)

with async_mock.patch.object(
test_module, "trace_event", async_mock.MagicMock()
) as mock_trace:
mock_trace.side_effect = KeyError()
with self.assertRaises(KeyError): # cover retry logic and bail
await mgr._process_loop()
assert mock_queued.retry_at is None

async def test_process_loop_retry_later(self):
mock_queued = async_mock.MagicMock(
state=QueuedOutboundMessage.STATE_RETRY,
retry_at=test_module.get_timer() + 3600,
)

context = InjectionContext()
mock_handle_not_delivered = async_mock.MagicMock()
mgr = OutboundTransportManager(context, mock_handle_not_delivered)
mgr.outbound_buffer.append(mock_queued)

with async_mock.patch.object(
test_module.asyncio, "sleep", async_mock.CoroutineMock()
) as mock_sleep_x:
mock_sleep_x.side_effect = KeyError()
with self.assertRaises(KeyError): # cover retry logic and bail
await mgr._process_loop()
assert mock_queued.retry_at is not None

async def test_process_loop_new(self):
context = InjectionContext()
mock_handle_not_delivered = async_mock.MagicMock()
mgr = OutboundTransportManager(context, mock_handle_not_delivered)

mgr.outbound_new = [
async_mock.MagicMock(
state=test_module.QueuedOutboundMessage.STATE_NEW,
message=async_mock.MagicMock(enc_payload=b"encr"),
)
]
with async_mock.patch.object(
mgr, "deliver_queued_message", async_mock.MagicMock()
) as mock_deliver, async_mock.patch.object(
mgr.outbound_event, "wait", async_mock.CoroutineMock()
) as mock_wait, async_mock.patch.object(
test_module, "trace_event", async_mock.MagicMock()
) as mock_trace:
mock_wait.side_effect = KeyError() # cover state=NEW logic and bail

with self.assertRaises(KeyError):
await mgr._process_loop()

async def test_process_loop_new_deliver(self):
context = InjectionContext()
mock_handle_not_delivered = async_mock.MagicMock()
mgr = OutboundTransportManager(context, mock_handle_not_delivered)

mgr.outbound_new = [
async_mock.MagicMock(
state=test_module.QueuedOutboundMessage.STATE_DELIVER,
message=async_mock.MagicMock(enc_payload=b"encr"),
)
]
with async_mock.patch.object(
mgr, "deliver_queued_message", async_mock.MagicMock()
) as mock_deliver, async_mock.patch.object(
mgr.outbound_event, "wait", async_mock.CoroutineMock()
) as mock_wait, async_mock.patch.object(
test_module, "trace_event", async_mock.MagicMock()
) as mock_trace:
mock_wait.side_effect = KeyError() # cover state=DELIVER logic and bail

with self.assertRaises(KeyError):
await mgr._process_loop()

async def test_process_loop_x(self):
mock_queued = async_mock.MagicMock(
state=QueuedOutboundMessage.STATE_DONE, error=KeyError()
state=QueuedOutboundMessage.STATE_DONE,
error=KeyError(),
endpoint="http://1.2.3.4:8081",
payload="Hello world",
)

context = InjectionContext()
Expand All @@ -193,3 +282,25 @@ async def test_process_loop_x(self):
mgr.outbound_buffer.append(mock_queued)

await mgr._process_loop()

async def test_finished_deliver_x_log_debug(self):
mock_queued = async_mock.MagicMock(
state=QueuedOutboundMessage.STATE_DONE, retries=1
)
mock_completed_x = async_mock.MagicMock(exc_info=KeyError("an error occurred"))

context = InjectionContext()
mock_handle_not_delivered = async_mock.MagicMock()
mgr = OutboundTransportManager(context, mock_handle_not_delivered)
mgr.outbound_buffer.append(mock_queued)
with async_mock.patch.object(
test_module.LOGGER, "exception", async_mock.MagicMock()
) as mock_logger_exception, async_mock.patch.object(
test_module.LOGGER, "error", async_mock.MagicMock()
) as mock_logger_error, async_mock.patch.object(
test_module.LOGGER, "isEnabledFor", async_mock.MagicMock()
) as mock_logger_enabled, async_mock.patch.object(
mgr, "process_queued", async_mock.MagicMock()
) as mock_process:
mock_logger_enabled.return_value = True # cover debug logging
mgr.finished_deliver(mock_queued, mock_completed_x)
67 changes: 66 additions & 1 deletion aries_cloudagent/transport/queue/tests/test_basic_queue.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio

from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock, TestCase as AsyncTestCase

from .. import basic as test_module
from ..basic import BasicMessageQueue


Expand Down Expand Up @@ -30,6 +31,70 @@ async def test_enqueue_dequeue(self):
queue.task_done()
await queue.join()

async def test_dequeue_x(self):
queue = BasicMessageQueue()
test_value = "test value"
await queue.enqueue(test_value)

with async_mock.patch.object(
test_module.asyncio, "get_event_loop", async_mock.MagicMock()
) as mock_get_event_loop, async_mock.patch.object(
test_module.asyncio, "wait", async_mock.CoroutineMock()
) as mock_wait:
mock_wait.return_value = (
async_mock.MagicMock(),
[
async_mock.MagicMock(
done=async_mock.MagicMock(), cancel=async_mock.MagicMock()
)
],
)
mock_get_event_loop.return_value = async_mock.MagicMock(
create_task=async_mock.MagicMock(
side_effect=[
async_mock.MagicMock(), # stopped
async_mock.MagicMock( # dequeued
done=async_mock.MagicMock(return_value=True),
exception=async_mock.MagicMock(return_value=KeyError()),
),
]
)
)
with self.assertRaises(KeyError):
await queue.dequeue(timeout=0)

async def test_dequeue_none(self):
queue = BasicMessageQueue()
test_value = "test value"
await queue.enqueue(test_value)

with async_mock.patch.object(
test_module.asyncio, "get_event_loop", async_mock.MagicMock()
) as mock_get_event_loop, async_mock.patch.object(
test_module.asyncio, "wait", async_mock.CoroutineMock()
) as mock_wait:
mock_wait.return_value = (
async_mock.MagicMock(),
[
async_mock.MagicMock(
done=async_mock.MagicMock(), cancel=async_mock.MagicMock()
)
],
)
mock_get_event_loop.return_value = async_mock.MagicMock(
create_task=async_mock.MagicMock(
side_effect=[
async_mock.MagicMock( # stopped
done=async_mock.MagicMock(return_value=True)
),
async_mock.MagicMock( # dequeued
done=async_mock.MagicMock(return_value=False)
),
]
)
)
assert await queue.dequeue(timeout=0) is None

async def test_async_iter(self):
queue = BasicMessageQueue()

Expand Down
22 changes: 22 additions & 0 deletions aries_cloudagent/transport/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from asynctest import TestCase as AsyncTestCase, mock as async_mock

from ...config.injection_context import InjectionContext

from .. import stats as test_module


class TestStatsTracer(AsyncTestCase):
def setUp(self):
self.context = async_mock.MagicMock(
socket_timer=async_mock.MagicMock(
stop=async_mock.MagicMock(side_effect=AttributeError("wrong"))
)
)
self.tracer = test_module.StatsTracer(test_module.Collector(), "test")

async def test_queued_start_stop(self):
await self.tracer.connection_queued_start(None, self.context, None)
await self.tracer.connection_queued_end(None, self.context, None)

async def test_connection_ready_error_pass(self):
await self.tracer.connection_ready(None, self.context, None)

0 comments on commit c3d12c7

Please sign in to comment.