Skip to content

Commit

Permalink
Add failure case for threaded command handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmeldrum committed Dec 13, 2023
1 parent 70835f4 commit 94536c3
Showing 1 changed file with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<CommandMessage> 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
Expand All @@ -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<Invocation> 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
Expand Down Expand Up @@ -209,4 +226,17 @@ private CommandMessage getCommandMessage(String suffix) {
.build()));
return message;
}

private void waitForCommandRequestToComplete(long timeout) {
Collection<Invocation> 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();
}
}
}

0 comments on commit 94536c3

Please sign in to comment.