From 94536c36124b0266df9b3ae5cea49aa37c300b6d Mon Sep 17 00:00:00 2001 From: Greg Meldrum Date: Wed, 13 Dec 2023 17:28:45 -0500 Subject: [PATCH] Add failure case for threaded command handling --- .../commandManager/CommandManagerTests.java | 48 +++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/service/application/src/test/java/com/solace/maas/ep/event/management/agent/commandManager/CommandManagerTests.java b/service/application/src/test/java/com/solace/maas/ep/event/management/agent/commandManager/CommandManagerTests.java index c8bea2e91..8c08a03a1 100644 --- a/service/application/src/test/java/com/solace/maas/ep/event/management/agent/commandManager/CommandManagerTests.java +++ b/service/application/src/test/java/com/solace/maas/ep/event/management/agent/commandManager/CommandManagerTests.java @@ -8,6 +8,7 @@ import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandBundle; import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandType; import com.solace.maas.ep.event.management.agent.plugin.command.model.ExecutionType; +import com.solace.maas.ep.event.management.agent.plugin.command.model.JobStatus; import com.solace.maas.ep.event.management.agent.plugin.mop.MOPSvcType; import com.solace.maas.ep.event.management.agent.plugin.service.MessagingServiceDelegateService; import com.solace.maas.ep.event.management.agent.plugin.solace.processor.semp.SempClient; @@ -39,6 +40,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -139,6 +141,28 @@ public void testMultiThreadedCommandManager() throws InterruptedException { receivedCorrelationIds.containsAll(expectedCorrelationIds) && expectedCorrelationIds.containsAll(receivedCorrelationIds)); } + @Test + void failSendingResponseBackToEp() { + // Create a command request message + CommandMessage message = getCommandMessage("1"); + + doNothing().when(terraformManager).execute(any(), any(), any()); + doThrow(new RuntimeException("Error sending response back to EP")).when(commandPublisher).sendCommandResponse(any(), any()); + commandManager.execute(message); + // Wait for all the threads to complete (add a timeout just in case) + long timeout = System.currentTimeMillis() + 10_000; + waitForCommandRequestToComplete(timeout); + + ArgumentCaptor messageArgCaptor = ArgumentCaptor.forClass(CommandMessage.class); + verify(commandPublisher, times(2)).sendCommandResponse(messageArgCaptor.capture(), any()); + + // Check that we attempted to set Error in the response message + messageArgCaptor.getAllValues().forEach(commandMessage -> { + assert commandMessage.getCommandCorrelationId().equals(message.getCommandCorrelationId()); + assert commandMessage.getCommandBundles().get(0).getCommands().get(0).getResult().getStatus().equals(JobStatus.error); + }); + } + @Test public void testCommandManager() { // Create a command request message @@ -156,17 +180,10 @@ public void testCommandManager() { .build())); commandManager.execute(message); + // Wait for all the threads to complete (add a timeout just in case) - Collection invocations = Mockito.mockingDetails(commandPublisher).getInvocations(); long timeout = System.currentTimeMillis() + 10_000; - while (invocations.isEmpty() && System.currentTimeMillis() < timeout) { - try { - TimeUnit.MILLISECONDS.sleep(500); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - invocations = Mockito.mockingDetails(commandPublisher).getInvocations(); - } + waitForCommandRequestToComplete(timeout); // Verify terraform manager is called @@ -209,4 +226,17 @@ private CommandMessage getCommandMessage(String suffix) { .build())); return message; } + + private void waitForCommandRequestToComplete(long timeout) { + Collection invocations = Mockito.mockingDetails(commandPublisher).getInvocations(); + + while (invocations.isEmpty() && System.currentTimeMillis() < timeout) { + try { + TimeUnit.MILLISECONDS.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + invocations = Mockito.mockingDetails(commandPublisher).getInvocations(); + } + } }