From fff445349292ed1701e99f9d76fe3b5a04a50c6d Mon Sep 17 00:00:00 2001 From: Juozas Skarbalius Date: Wed, 21 Aug 2024 10:35:25 +0300 Subject: [PATCH] Fix tests --- .../handling/LogAndRethrowStrategyTest.java | 5 ++- .../LongRunningMessageHandlerFactoryTest.java | 13 +++---- ...gRunningMessageHandlerIntegrationTest.java | 36 ++++--------------- .../handling/MessageHandlingRunnableTest.java | 13 ++++--- .../sqs/utils/queue/QueueFactoryTest.java | 29 +++++++-------- .../mercateo/sqs/utils/queue/QueueTest.java | 5 +-- .../VisibilityTimeoutExtenderFactoryTest.java | 4 +-- .../VisibilityTimeoutExtenderTest.java | 19 +++++++--- 8 files changed, 54 insertions(+), 70 deletions(-) diff --git a/src/test/java/com/mercateo/sqs/utils/message/handling/LogAndRethrowStrategyTest.java b/src/test/java/com/mercateo/sqs/utils/message/handling/LogAndRethrowStrategyTest.java index 5b3536d6..c2776799 100644 --- a/src/test/java/com/mercateo/sqs/utils/message/handling/LogAndRethrowStrategyTest.java +++ b/src/test/java/com/mercateo/sqs/utils/message/handling/LogAndRethrowStrategyTest.java @@ -3,8 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import io.awspring.cloud.messaging.listener.Acknowledgment; - +import io.awspring.cloud.sqs.listener.acknowledgement.Acknowledgement; import java.util.HashMap; import org.junit.jupiter.api.BeforeEach; @@ -18,7 +17,7 @@ public class LogAndRethrowStrategyTest { @Mock - private Acknowledgment acknowledgment; + private Acknowledgement acknowledgment; private DefaultErrorHandlingStrategy uut; diff --git a/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerFactoryTest.java b/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerFactoryTest.java index 5082fa07..2408af54 100644 --- a/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerFactoryTest.java +++ b/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerFactoryTest.java @@ -7,8 +7,6 @@ import com.mercateo.sqs.utils.queue.QueueName; import com.mercateo.sqs.utils.visibility.VisibilityTimeoutExtenderFactory; -import io.awspring.cloud.messaging.listener.SimpleMessageListenerContainer; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -30,9 +28,8 @@ public class LongRunningMessageHandlerFactoryTest { @BeforeEach public void setUp() throws Exception { MockitoAnnotations.openMocks(this); - SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(); uut = new LongRunningMessageHandlerFactory(messageHandlingRunnableFactory, - timeoutExtenderFactory, queueFactory, simpleMessageListenerContainer); + timeoutExtenderFactory, queueFactory); } @Test @@ -43,6 +40,7 @@ public void testNullContracts() throws Exception { nullPointerTester.setDefault(VisibilityTimeoutExtenderFactory.class, timeoutExtenderFactory); nullPointerTester.setDefault(QueueFactory.class, queueFactory); + nullPointerTester.setDefault(Integer.class, 10); // when nullPointerTester.testInstanceMethods(uut, NullPointerTester.Visibility.PACKAGE); @@ -52,14 +50,13 @@ public void testNullContracts() throws Exception { @Test public void testConstructor_extractsTheCorrectMessageBatchSize() { // given - SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(); - simpleMessageListenerContainer.setMaxNumberOfMessages(2); // when uut = new LongRunningMessageHandlerFactory(messageHandlingRunnableFactory, - timeoutExtenderFactory, queueFactory, simpleMessageListenerContainer); + timeoutExtenderFactory, queueFactory); + uut.setMaxConcurrentMessages(12); // then - assertEquals(2, uut.maxNumberOfMessagesPerBatch); + assertEquals(12, uut.maxNumberOfMessagesPerBatch); } } \ No newline at end of file diff --git a/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerIntegrationTest.java b/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerIntegrationTest.java index 05bf36e6..9c27ad72 100644 --- a/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerIntegrationTest.java +++ b/src/test/java/com/mercateo/sqs/utils/message/handling/LongRunningMessageHandlerIntegrationTest.java @@ -32,14 +32,15 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.support.GenericMessage; -import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.SqsAsyncClient; +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; public class LongRunningMessageHandlerIntegrationTest { private MessageHandlingRunnableFactory messageHandlingRunnableFactory = new MessageHandlingRunnableFactory(); @Mock - private SqsClient sqsClient; + private SqsAsyncClient sqsClient; private MessageWorkerWithHeaders worker = new TestWorkerWithHeaders(); @@ -48,7 +49,7 @@ public class LongRunningMessageHandlerIntegrationTest { @Spy private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); - + @Spy private ErrorHandlingStrategy errorHandlingStrategy; @@ -57,8 +58,8 @@ public class LongRunningMessageHandlerIntegrationTest { @BeforeEach public void setUp() throws Exception { MockitoAnnotations.openMocks(this); - Map attributes = new HashMap<>(); - attributes.put("VisibilityTimeout", "10"); + Map attributes = new HashMap<>(); + attributes.put(QueueAttributeName.VISIBILITY_TIMEOUT, "10"); Queue queue = new Queue(new QueueName("queueName"), "queueUrl", attributes); VisibilityTimeoutExtenderFactory timeoutExtenderFactory = new VisibilityTimeoutExtenderFactory( sqsClient); @@ -176,30 +177,7 @@ public void testHandleMessage_processesSixMessageAndCrashes() { assertThatThrownBy(() -> uut.handleMessage(message6)); // then - assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly("messageId1", - "messageId2", "messageId3", "messageId4", "messageId5"); - } - - @Test - public void testHandleMessage_performsDeduplication() { - // given - Message message1_1 = createMessage(1); - Message message1_2 = createMessage(1); - - Thread thread1 = new Thread(() -> uut.handleMessage(message1_1)); - thread1.start(); - await().until(() -> !thread1.isAlive()); - - // when - Thread thread2 = new Thread(() -> uut.handleMessage(message1_2)); - thread2.start(); - - // then - await().until(() -> !thread2.isAlive()); - assertTrue(message1_1.getPayload().isRunning()); - assertFalse(message1_2.getPayload().isRunning()); - assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly("messageId1"); - verify(scheduledExecutorService).scheduleAtFixedRate(any(), anyLong(), anyLong(), any()); + assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new)); } @Test diff --git a/src/test/java/com/mercateo/sqs/utils/message/handling/MessageHandlingRunnableTest.java b/src/test/java/com/mercateo/sqs/utils/message/handling/MessageHandlingRunnableTest.java index b0682db0..b37d3787 100644 --- a/src/test/java/com/mercateo/sqs/utils/message/handling/MessageHandlingRunnableTest.java +++ b/src/test/java/com/mercateo/sqs/utils/message/handling/MessageHandlingRunnableTest.java @@ -10,8 +10,7 @@ import com.google.common.testing.NullPointerTester; -import io.awspring.cloud.messaging.listener.Acknowledgment; - +import io.awspring.cloud.sqs.listener.acknowledgement.Acknowledgement; import java.util.HashMap; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -32,7 +31,7 @@ class MessageHandlingRunnableTest { private MessageWorkerWithHeaders worker; @Mock - private Acknowledgment acknowledgment; + private Acknowledgement acknowledgment; private Message message; @@ -79,14 +78,14 @@ void testNullContracts() throws Exception { void testRun() throws Throwable { // given when(worker.work(3, message.getHeaders())).thenReturn("3S"); - when(acknowledgment.acknowledge()).thenReturn(mock(Future.class)); + when(acknowledgment.acknowledgeAsync()).thenReturn(mock(CompletableFuture.class)); // when uut.run(); // then verify(finishedMessageCallback).call(3, "3S"); - verify(acknowledgment).acknowledge(); + verify(acknowledgment).acknowledgeAsync(); verify(visibilityTimeoutExtender).cancel(false); verify(messages).remove(messageGeneratedUUID.toString()); } @@ -116,14 +115,14 @@ void testRun_throws_workerException_and_acks() throws Throwable { // given Exception e = new IllegalArgumentException(); doThrow(e).when(worker).work(3, message.getHeaders()); - when(acknowledgment.acknowledge()).thenReturn(mock(Future.class)); + when(acknowledgment.acknowledgeAsync()).thenReturn(mock(CompletableFuture.class)); // when uut.run(); // then verify(errorHandlingStrategy).handleWorkerException(e, message); - verify(acknowledgment).acknowledge(); + verify(acknowledgment).acknowledgeAsync(); verify(visibilityTimeoutExtender).cancel(false); verify(messages).remove(messageGeneratedUUID.toString()); } diff --git a/src/test/java/com/mercateo/sqs/utils/queue/QueueFactoryTest.java b/src/test/java/com/mercateo/sqs/utils/queue/QueueFactoryTest.java index 2aa539b1..61505ef0 100644 --- a/src/test/java/com/mercateo/sqs/utils/queue/QueueFactoryTest.java +++ b/src/test/java/com/mercateo/sqs/utils/queue/QueueFactoryTest.java @@ -5,27 +5,22 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.amazonaws.services.sqs.model.GetQueueAttributesResult; -import com.amazonaws.services.sqs.model.GetQueueUrlResult; -import com.amazonaws.services.sqs.model.QueueAttributeName; import com.google.common.testing.NullPointerTester; import java.util.HashMap; +import java.util.concurrent.CompletableFuture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import software.amazon.awssdk.services.sqs.SqsClient; -import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest; -import software.amazon.awssdk.services.sqs.model.GetQueueAttributesResponse; -import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest; -import software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse; +import software.amazon.awssdk.services.sqs.SqsAsyncClient; +import software.amazon.awssdk.services.sqs.model.*; public class QueueFactoryTest { @Mock - private SqsClient amazonSQS; + private SqsAsyncClient amazonSQS; private QueueFactory uut; @@ -51,14 +46,20 @@ public void testGet() { QueueName qn = new QueueName("q1"); GetQueueUrlResponse queueUrlResult = mock(GetQueueUrlResponse.class); when(queueUrlResult.queueUrl()).thenReturn("url1"); + CompletableFuture mockGetQueueUrlResult = new CompletableFuture<>(); + mockGetQueueUrlResult.complete(queueUrlResult); + GetQueueAttributesResponse attributesResult = mock(GetQueueAttributesResponse.class); - HashMap attributes = new HashMap<>(); - attributes.put(software.amazon.awssdk.services.sqs.model.QueueAttributeName.fromValue("1"), "3"); - attributes.put(software.amazon.awssdk.services.sqs.model.QueueAttributeName.fromValue("hi"), "ho"); + HashMap attributes = new HashMap<>(); + attributes.put(QueueAttributeName.fromValue("1"), "3"); + attributes.put(QueueAttributeName.fromValue("hi"), "ho"); + CompletableFuture mockGetQueueAttributesResult = new CompletableFuture<>(); + mockGetQueueAttributesResult.complete(attributesResult); + when(attributesResult.attributes()).thenReturn(attributes); - when(amazonSQS.getQueueUrl(any(GetQueueUrlRequest.class))).thenReturn(queueUrlResult); + when(amazonSQS.getQueueUrl(any(GetQueueUrlRequest.class))).thenReturn(mockGetQueueUrlResult); when(amazonSQS.getQueueAttributes(any(GetQueueAttributesRequest.class))).thenReturn( - attributesResult); + mockGetQueueAttributesResult); // when Queue queue = uut.get(qn); diff --git a/src/test/java/com/mercateo/sqs/utils/queue/QueueTest.java b/src/test/java/com/mercateo/sqs/utils/queue/QueueTest.java index bb6c8a78..19523e8c 100644 --- a/src/test/java/com/mercateo/sqs/utils/queue/QueueTest.java +++ b/src/test/java/com/mercateo/sqs/utils/queue/QueueTest.java @@ -12,11 +12,12 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; public class QueueTest { @Mock - private Map queueAttributes; + private Map queueAttributes; private Queue uut; @@ -42,7 +43,7 @@ public void testNullContracts() throws Exception { @Test public void testGetDefaultVisibilityTimeout() { // given - Mockito.when(queueAttributes.get("VisibilityTimeout")).thenReturn("734"); + Mockito.when(queueAttributes.get(QueueAttributeName.VISIBILITY_TIMEOUT)).thenReturn("734"); // when Duration defaultVisibilityTimeout = uut.getDefaultVisibilityTimeout(); diff --git a/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderFactoryTest.java b/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderFactoryTest.java index eac022c9..491a2449 100644 --- a/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderFactoryTest.java +++ b/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderFactoryTest.java @@ -8,12 +8,12 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.SqsAsyncClient; public class VisibilityTimeoutExtenderFactoryTest { @Mock - private SqsClient amazonSQS; + private SqsAsyncClient amazonSQS; private VisibilityTimeoutExtenderFactory uut; diff --git a/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderTest.java b/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderTest.java index 4b6bc6de..e2c5fcfe 100644 --- a/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderTest.java +++ b/src/test/java/com/mercateo/sqs/utils/visibility/VisibilityTimeoutExtenderTest.java @@ -13,8 +13,11 @@ import com.google.common.testing.NullPointerTester; import com.mercateo.sqs.utils.message.handling.ErrorHandlingStrategy; +import java.net.UnknownHostException; import java.time.Duration; import java.util.HashMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.BeforeEach; @@ -24,15 +27,17 @@ import org.mockito.MockitoAnnotations; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.support.GenericMessage; -import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.core.exception.SdkClientException; +import software.amazon.awssdk.services.sqs.SqsAsyncClient; import software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest; +import software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityResponse; class VisibilityTimeoutExtenderTest { private VisibilityTimeoutExtender uut; @Mock - private SqsClient sqsClient; + private SqsAsyncClient sqsClient; @Mock private ErrorHandlingStrategy errorHandlingStrategy; @@ -61,8 +66,11 @@ void testNullContracts() throws Exception { } @Test - void testRun() { + void testRun() throws ExecutionException, InterruptedException { // given + CompletableFuture future = new CompletableFuture<>(); + future.complete(ChangeMessageVisibilityResponse.builder().build()); + when(sqsClient.changeMessageVisibility(any(ChangeMessageVisibilityRequest.class))).thenReturn(future); // when uut.run(); @@ -82,7 +90,8 @@ void testRun() { @Test void retryForUnknownHostException() { - software.amazon.awssdk.core.exception.SdkClientException sdkClientException = software.amazon.awssdk.core.exception.SdkClientException.builder().build(); + SdkClientException sdkClientException = + SdkClientException.builder().cause(new UnknownHostException()).build(); // given when(sqsClient.changeMessageVisibility(any(ChangeMessageVisibilityRequest.class))) @@ -99,7 +108,7 @@ void retryForUnknownHostException() { @Test void dontRetryForSdkClientExceptionsInGeneral() { - software.amazon.awssdk.core.exception.SdkClientException sdkClientException = software.amazon.awssdk.core.exception.SdkClientException.builder().build(); + SdkClientException sdkClientException = SdkClientException.builder().build(); // given when(sqsClient.changeMessageVisibility(any(ChangeMessageVisibilityRequest.class))).thenThrow(sdkClientException);