Skip to content

Commit

Permalink
Merge pull request #668 from sklump/unit-tests-complete-conductor-shu…
Browse files Browse the repository at this point in the history
…tdown-admin-server-on-fatal

complete unit tests notifying admin server on fatal error
  • Loading branch information
andrewwhitehead authored Aug 18, 2020
2 parents 4a80d88 + b438234 commit 5fc6394
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
21 changes: 15 additions & 6 deletions aries_cloudagent/core/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def inbound_message_router(
lambda completed: self.dispatch_complete(message, completed),
)
except (LedgerConfigError, LedgerTransactionError) as e:
LOGGER.error("Shutdown with ledger error %s", str(e))
LOGGER.error("Shutdown on ledger error %s", str(e))
if self.admin_server:
self.admin_server.notify_fatal_error()
raise
Expand All @@ -278,10 +278,19 @@ def dispatch_complete(self, message: InboundMessage, completed: CompletedTask):
if isinstance(completed.exc_info[1], LedgerConfigError) or isinstance(
completed.exc_info[1], LedgerTransactionError
):
LOGGER.error("Shutdown with %s", str(completed.exc_info[1]))
self.admin_server.notify_fatal_error()
LOGGER.error(
"%shutdown on ledger error %s",
"S" if self.admin_server else "No admin server to s",
str(completed.exc_info[1]),
)
if self.admin_server:
self.admin_server.notify_fatal_error()
else:
LOGGER.error("DON'T Shutdown with %s", str(completed.exc_info[1]))
LOGGER.error(
"DON'T shutdown on %s %s",
completed.exc_info[0].__name__,
str(completed.exc_info[1]),
)
self.inbound_transport_manager.dispatch_complete(message, completed)

async def get_stats(self) -> dict:
Expand Down Expand Up @@ -331,7 +340,7 @@ def handle_not_returned(self, context: InjectionContext, outbound: OutboundMessa
try:
self.dispatcher.run_task(self.queue_outbound(context, outbound))
except (LedgerConfigError, LedgerTransactionError) as e:
LOGGER.error("Shutdown with ledger error %s", str(e))
LOGGER.error("Shutdown on ledger error %s", str(e))
if self.admin_server:
self.admin_server.notify_fatal_error()
raise
Expand Down Expand Up @@ -362,7 +371,7 @@ async def queue_outbound(
LOGGER.exception("Error preparing outbound message for transmission")
return
except (LedgerConfigError, LedgerTransactionError) as e:
LOGGER.error("Shutdown with ledger error %s", str(e))
LOGGER.error("Shutdown on ledger error %s", str(e))
if self.admin_server:
self.admin_server.notify_fatal_error()
raise
Expand Down
62 changes: 38 additions & 24 deletions aries_cloudagent/core/tests/test_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ async def test_start_x_out(self):
with self.assertRaises(KeyError):
await conductor.start()

async def test_dispatch_complete(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
async def test_dispatch_complete_non_fatal_x(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings_admin)
conductor = test_module.Conductor(builder)

message_body = "{}"
Expand All @@ -609,7 +609,42 @@ async def test_dispatch_complete(self):
)

await conductor.setup()
conductor.dispatch_complete(message, mock_task)

with async_mock.patch.object(
conductor.admin_server, "notify_fatal_error", async_mock.MagicMock()
) as mock_notify:
conductor.dispatch_complete(message, mock_task)
mock_notify.assert_not_called()

async def test_dispatch_complete_fatal_x(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings_admin)
conductor = test_module.Conductor(builder)

message_body = "{}"
receipt = MessageReceipt(direct_response_mode="snail mail")
message = InboundMessage(message_body, receipt)
mock_task = async_mock.MagicMock(
exc_info=(
test_module.LedgerTransactionError,
test_module.LedgerTransactionError("Ledger is wobbly"),
"...",
),
ident="abc",
timing={
"queued": 1234567890,
"unqueued": 1234567899,
"started": 1234567901,
"ended": 1234567999,
},
)

await conductor.setup()

with async_mock.patch.object(
conductor.admin_server, "notify_fatal_error", async_mock.MagicMock()
) as mock_notify:
conductor.dispatch_complete(message, mock_task)
mock_notify.assert_called_once_with()

async def test_print_invite(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
Expand Down Expand Up @@ -663,24 +698,3 @@ async def test_webhook_router(self):
mock_enqueue.assert_called_once_with(
test_topic, test_payload, test_endpoint, test_attempts
)

async def test_dispatch_complete_fatal_x(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
conductor = test_module.Conductor(builder)

message_body = "{}"
receipt = MessageReceipt(direct_response_mode="snail mail")
message = InboundMessage(message_body, receipt)
mock_task = async_mock.MagicMock(
exc_info=(test_module.LedgerTransactionError, ("Ledger is wobbly"), "..."),
ident="abc",
timing={
"queued": 1234567890,
"unqueued": 1234567899,
"started": 1234567901,
"ended": 1234567999,
},
)

await conductor.setup()
conductor.dispatch_complete(message, mock_task)

0 comments on commit 5fc6394

Please sign in to comment.