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

Commit

Permalink
Minor test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Juozas Skarbalius committed Sep 3, 2024
1 parent 2d35de8 commit 465af9b
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;

Expand All @@ -28,7 +27,7 @@ public void setUp() throws Exception {
}

@Test
public void handle_throws_exception() {
void handle_throws_exception() {
// Given
Exception e = new IllegalArgumentException();
MessageWrapper<Integer> message = createMessage();
Expand All @@ -38,7 +37,6 @@ public void handle_throws_exception() {

// Then
assertThat(throwable).isInstanceOf(IllegalArgumentException.class);

}

private MessageWrapper<Integer> createMessage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercateo.sqs.utils.message.handling;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.testing.NullPointerTester;
import com.mercateo.sqs.utils.queue.QueueFactory;
Expand All @@ -26,14 +26,14 @@ public class LongRunningMessageHandlerFactoryTest {
private LongRunningMessageHandlerFactory uut;

@BeforeEach
public void setUp() throws Exception {
void setUp() {
MockitoAnnotations.openMocks(this);
uut = new LongRunningMessageHandlerFactory(messageHandlingRunnableFactory,
timeoutExtenderFactory, queueFactory);
}

@Test
public void testNullContracts() throws Exception {
void testNullContracts() {
// given
NullPointerTester nullPointerTester = new NullPointerTester();
nullPointerTester.setDefault(QueueName.class, new QueueName("name"));
Expand All @@ -48,15 +48,14 @@ public void testNullContracts() throws Exception {
}

@Test
public void testConstructor_extractsTheCorrectMessageBatchSize() {
void testConstructor_extractsTheCorrectMessageBatchSize() {
// given
int expectedBatchSize = 8;

// when
uut = new LongRunningMessageHandlerFactory(messageHandlingRunnableFactory,
timeoutExtenderFactory, queueFactory);
uut.setMaxConcurrentMessages(12);
uut.setMaxConcurrentMessages(8);

// then
assertEquals(12, uut.maxNumberOfMessagesPerBatch);
assertThat(uut.getMaxConcurrentMessages()).isEqualTo(expectedBatchSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;
Expand All @@ -29,6 +27,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
Expand All @@ -55,7 +54,7 @@ public class LongRunningMessageHandlerIntegrationTest {
private LongRunningMessageHandler<InputObject, String> uut;

@BeforeEach
public void setUp() {
void setUp() {
MockitoAnnotations.openMocks(this);
Map<QueueAttributeName, String> attributes = new HashMap<>();
attributes.put(QueueAttributeName.VISIBILITY_TIMEOUT, "10");
Expand All @@ -69,10 +68,9 @@ public void setUp() {
}

@Test
public void testHandleMessage_processesOneMessageAndReturns() {
void testHandleMessage_processesOneMessageAndReturns() {
// given
MessageWrapper<InputObject> message = createMessage(1);
String uuid = String.valueOf(message.getMessage().getHeaders().get("id", UUID.class));

// when
Thread thread = new Thread(() -> uut.handleMessage(message.getMessage()));
Expand All @@ -81,16 +79,16 @@ public void testHandleMessage_processesOneMessageAndReturns() {
// then
await().until(() -> !thread.isAlive());
await().until(() -> message.getMessage().getPayload().isRunning());
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuid);
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(message.getMessageId());
}

@Test
public void testHandleMessage_processesTwoMessagesAndBlocks() {
void testHandleMessage_processesTwoMessagesAndBlocks() {
// given
MessageWrapper<InputObject> message1 = createMessage(1);
MessageWrapper<InputObject> message2 = createMessage(2);
List<String> uuids = List.of(String.valueOf(message1.getMessage().getHeaders().get("id", UUID.class)),
String.valueOf(message2.getMessage().getHeaders().get("id", UUID.class)));
List<String> messageIds = List.of(String.valueOf(message1.getMessageId()),
String.valueOf(message2.getMessageId()));

Thread thread1 = new Thread(() -> uut.handleMessage(message1.getMessage()));
thread1.start();
Expand All @@ -104,20 +102,20 @@ public void testHandleMessage_processesTwoMessagesAndBlocks() {
await().until(() -> Thread.State.WAITING == thread2.getState());
await().until(() -> message1.getMessage().getPayload().isRunning());
await().until(() -> message2.getMessage().getPayload().isRunning());
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new));
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsExactlyInAnyOrderElementsOf(messageIds);
}

@Test
public void testHandleMessage_processesFourMessagesAndFillsQueue() {
void testHandleMessage_processesFourMessagesAndFillsQueue() {
// given
MessageWrapper<InputObject> message1 = createMessage(1);
MessageWrapper<InputObject> message2 = createMessage(2);
MessageWrapper<InputObject> message3 = createMessage(3);
MessageWrapper<InputObject> message4 = createMessage(4);
List<String> uuids = List.of(String.valueOf(message1.getMessage().getHeaders().get("id", UUID.class)),
String.valueOf(message2.getMessage().getHeaders().get("id", UUID.class)),
String.valueOf(message3.getMessage().getHeaders().get("id", UUID.class)),
String.valueOf(message4.getMessage().getHeaders().get("id", UUID.class)));
List<String> messageIds = List.of(String.valueOf(message1.getMessageId()),
String.valueOf(message2.getMessageId()),
String.valueOf(message3.getMessageId()),
String.valueOf(message4.getMessageId()));

new Thread(() -> uut.handleMessage(message1.getMessage())).start();
new Thread(() -> uut.handleMessage(message2.getMessage())).start();
Expand All @@ -133,15 +131,15 @@ public void testHandleMessage_processesFourMessagesAndFillsQueue() {
// then
await().until(() -> Thread.State.WAITING == thread3.getState());
await().until(() -> Thread.State.WAITING == thread4.getState());
assertFalse(message3.getMessage().getPayload().isRunning());
assertFalse(message4.getMessage().getPayload().isRunning());
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new));
assertThat(message3.getMessage().getPayload().isRunning()).isFalse();
assertThat(message4.getMessage().getPayload().isRunning()).isFalse();
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsExactlyInAnyOrderElementsOf(messageIds);
verify(scheduledExecutorService, times(4)).scheduleAtFixedRate(any(), anyLong(), anyLong(),
any());
}

@Test
public void testHandleMessage_processesSixMessageAndCrashes() {
void testHandleMessage_processesSixMessageAndCrashes() {
// given
MessageWrapper<InputObject> message1 = createMessage(1);
MessageWrapper<InputObject> message2 = createMessage(2);
Expand All @@ -150,11 +148,11 @@ public void testHandleMessage_processesSixMessageAndCrashes() {
MessageWrapper<InputObject> message5 = createMessage(5);
MessageWrapper<InputObject> message6 = createMessage(6);

List<String> uuids = List.of(message1.getMessage().getHeaders().get("id", UUID.class).toString(),
message2.getMessage().getHeaders().get("id", UUID.class).toString(),
message3.getMessage().getHeaders().get("id", UUID.class).toString(),
message4.getMessage().getHeaders().get("id", UUID.class).toString(),
message5.getMessage().getHeaders().get("id", UUID.class).toString());
List<String> messageIds = List.of(message1.getMessageId(),
message2.getMessageId(),
message3.getMessageId(),
message4.getMessageId(),
message5.getMessageId());

new Thread(() -> uut.handleMessage(message1.getMessage())).start();
new Thread(() -> uut.handleMessage(message2.getMessage())).start();
Expand All @@ -173,20 +171,21 @@ public void testHandleMessage_processesSixMessageAndCrashes() {
await().until(() -> Thread.State.WAITING == thread5.getState());

// when
assertThatThrownBy(() -> uut.handleMessage(message6.getMessage()));
assertThatThrownBy(() -> uut.handleMessage(message6.getMessage()))
.hasCauseInstanceOf(TaskRejectedException.class);

// then
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new));
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsExactlyInAnyOrderElementsOf(messageIds);
}

@Test
public void testHandleMessage_startsQueuedProcess() {
void testHandleMessage_startsQueuedProcess() {
// given
MessageWrapper<InputObject> message1 = createMessage(1);
MessageWrapper<InputObject> message2 = createMessage(2);
MessageWrapper<InputObject> message3 = createMessage(3);
List<String> uuids = List.of(String.valueOf(message1.getMessage().getHeaders().get("id", UUID.class)),
String.valueOf(message3.getMessage().getHeaders().get("id", UUID.class)));
List<String> messageIds = List.of(String.valueOf(message1.getMessageId()),
String.valueOf(message3.getMessageId()));

new Thread(() -> uut.handleMessage(message1.getMessage())).start();
await().until(() -> message1.getMessage().getPayload().isRunning());
Expand All @@ -202,16 +201,16 @@ public void testHandleMessage_startsQueuedProcess() {
// then
await().until(() -> Thread.State.WAITING == thread2.getState());
await().until(() -> Thread.State.WAITING == thread3.getState());
assertTrue(message3.getMessage().getPayload().isRunning());
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new));
assertThat(message3.getMessage().getPayload().isRunning()).isTrue();
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsExactlyInAnyOrderElementsOf(messageIds);
}

@Test
public void testHandleMessage_resumesWaitingThreads() {
void testHandleMessage_resumesWaitingThreads() {
// given
MessageWrapper<InputObject> message1 = createMessage(1);
MessageWrapper<InputObject> message2 = createMessage(2);
List<String> uuids = List.of(String.valueOf(message2.getMessage().getHeaders().get("id", UUID.class)));
List<String> messageId = List.of(String.valueOf(message2.getMessageId()));

Thread thread1 = new Thread(() -> uut.handleMessage(message1.getMessage()));
thread1.start();
Expand All @@ -226,7 +225,7 @@ public void testHandleMessage_resumesWaitingThreads() {

// then
await().until(() -> !thread2.isAlive());
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsOnly(uuids.toArray(String[]::new));
assertThat(uut.getMessagesInProcessing().getBackingSet()).containsExactlyElementsOf(messageId);
}

private MessageWrapper<InputObject> createMessage(int number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;

Expand Down Expand Up @@ -53,7 +52,7 @@ public class LongRunningMessageHandlerTest {
private LongRunningMessageHandler<Integer, String> uut;

@BeforeEach
public void setUp() throws Exception {
void setUp() {
MockitoAnnotations.openMocks(this);
when(queue.getName()).thenReturn(new QueueName("queuename"));
when(queue.getDefaultVisibilityTimeout()).thenReturn(Duration.ofSeconds(120));
Expand All @@ -63,7 +62,7 @@ public void setUp() throws Exception {
}

@Test
public void testNullContracts() throws Exception {
void testNullContracts() {
// given
NullPointerTester nullPointerTester = new NullPointerTester();
nullPointerTester.setDefault(VisibilityTimeoutExtenderFactory.class,
Expand All @@ -75,7 +74,8 @@ public void testNullContracts() throws Exception {
nullPointerTester.testConstructors(uut.getClass(), Visibility.PACKAGE);
}

public void timeUntilTimeOutExtensionTooLarge() throws Exception {
@Test
void timeUntilTimeOutExtensionTooLarge(){

// when
Throwable result = catchThrowable(() -> uut = new LongRunningMessageHandler<>(timeoutExtensionExecutor, 10, 2,
Expand All @@ -86,7 +86,8 @@ public void timeUntilTimeOutExtensionTooLarge() throws Exception {
assertThat(result).isInstanceOf(IllegalStateException.class);
}

public void timeUntilTimeOutNegative() throws Exception {
@Test
void timeUntilTimeOutNegative(){

// when
Throwable result = catchThrowable(() -> uut = new LongRunningMessageHandler<>(timeoutExtensionExecutor, 10, 2,
Expand All @@ -98,7 +99,7 @@ public void timeUntilTimeOutNegative() throws Exception {
}

@Test
public void testHandleMessage_handlesExceptionDuringTimeoutExtension() {
void testHandleMessage_handlesExceptionDuringTimeoutExtension() {
// given
MessageWrapper<Integer> message = createMessage();
RuntimeException exception = new RuntimeException("test exception");
Expand All @@ -114,8 +115,8 @@ public void testHandleMessage_handlesExceptionDuringTimeoutExtension() {

private MessageWrapper<Integer> createMessage() {
Map<String, Object> headers = new HashMap<>();
String uuid = UUID.fromString("bf308aa2-bf48-49b8-a839-61611c710430").toString();
headers.put("id", uuid);
String messageId = UUID.fromString("bf308aa2-bf48-49b8-a839-61611c710430").toString();
headers.put("id", messageId);

MessageHeaders messageHeaders = new MessageHeaders(headers);
return new MessageWrapper<>(new GenericMessage<>(1, messageHeaders));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercateo.sqs.utils.message.handling;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

Expand All @@ -12,7 +12,7 @@
public class MessageWorkerTest {

@Test
public void testWorkDelegatesMethodCall() throws Exception {
void testWorkDelegatesMethodCall() throws Exception {
// given
AtomicInteger counter = new AtomicInteger(0);
MessageWorker<String, Integer> uut = new MessageWorker<String, Integer>() {
Expand All @@ -27,7 +27,7 @@ public Integer work(String object) {
uut.work("dummy value", messageHeaders);

// then
assertEquals(1, counter.intValue());
assertThat(counter.intValue()).isEqualTo(counter.intValue());
verifyNoInteractions(messageHeaders);
}
}
Loading

0 comments on commit 465af9b

Please sign in to comment.