Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Juozas Skarbalius committed Aug 23, 2024
1 parent 9bfc911 commit fff4453
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +17,7 @@
public class LogAndRethrowStrategyTest {

@Mock
private Acknowledgment acknowledgment;
private Acknowledgement acknowledgment;

private DefaultErrorHandlingStrategy<Integer> uut;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputObject, String> worker = new TestWorkerWithHeaders();

Expand All @@ -48,7 +49,7 @@ public class LongRunningMessageHandlerIntegrationTest {

@Spy
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

@Spy
private ErrorHandlingStrategy<InputObject> errorHandlingStrategy;

Expand All @@ -57,8 +58,8 @@ public class LongRunningMessageHandlerIntegrationTest {
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
Map<String, String> attributes = new HashMap<>();
attributes.put("VisibilityTimeout", "10");
Map<QueueAttributeName, String> attributes = new HashMap<>();
attributes.put(QueueAttributeName.VISIBILITY_TIMEOUT, "10");
Queue queue = new Queue(new QueueName("queueName"), "queueUrl", attributes);
VisibilityTimeoutExtenderFactory timeoutExtenderFactory = new VisibilityTimeoutExtenderFactory(
sqsClient);
Expand Down Expand Up @@ -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<InputObject> message1_1 = createMessage(1);
Message<InputObject> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +31,7 @@ class MessageHandlingRunnableTest {
private MessageWorkerWithHeaders<Integer, String> worker;

@Mock
private Acknowledgment acknowledgment;
private Acknowledgement acknowledgment;

private Message<Integer> message;

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down
29 changes: 15 additions & 14 deletions src/test/java/com/mercateo/sqs/utils/queue/QueueFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,14 +46,20 @@ public void testGet() {
QueueName qn = new QueueName("q1");
GetQueueUrlResponse queueUrlResult = mock(GetQueueUrlResponse.class);
when(queueUrlResult.queueUrl()).thenReturn("url1");
CompletableFuture<GetQueueUrlResponse> mockGetQueueUrlResult = new CompletableFuture<>();
mockGetQueueUrlResult.complete(queueUrlResult);

GetQueueAttributesResponse attributesResult = mock(GetQueueAttributesResponse.class);
HashMap<software.amazon.awssdk.services.sqs.model.QueueAttributeName, String> 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<QueueAttributeName, String> attributes = new HashMap<>();
attributes.put(QueueAttributeName.fromValue("1"), "3");
attributes.put(QueueAttributeName.fromValue("hi"), "ho");
CompletableFuture<GetQueueAttributesResponse> 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);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/mercateo/sqs/utils/queue/QueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> queueAttributes;
private Map<QueueAttributeName, String> queueAttributes;

private Queue uut;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -61,8 +66,11 @@ void testNullContracts() throws Exception {
}

@Test
void testRun() {
void testRun() throws ExecutionException, InterruptedException {
// given
CompletableFuture<ChangeMessageVisibilityResponse> future = new CompletableFuture<>();
future.complete(ChangeMessageVisibilityResponse.builder().build());
when(sqsClient.changeMessageVisibility(any(ChangeMessageVisibilityRequest.class))).thenReturn(future);

// when
uut.run();
Expand All @@ -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)))
Expand All @@ -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);
Expand Down

0 comments on commit fff4453

Please sign in to comment.