Skip to content

Commit 08e6ee8

Browse files
committed
fix: coverage
Signed-off-by: Marcos Tischer Vallim <tischer@gmail.com>
1 parent e16a14f commit 08e6ee8

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

amazon-sns-java-messaging-lib-template/src/test/java/com/amazon/sns/messaging/lib/core/AbstractAmazonSnsConsumerTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import static org.hamcrest.Matchers.hasKey;
2424
import static org.hamcrest.Matchers.instanceOf;
2525
import static org.hamcrest.Matchers.notNullValue;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
2627
import static org.mockito.ArgumentMatchers.any;
2728
import static org.mockito.Mockito.atLeastOnce;
2829
import static org.mockito.Mockito.doAnswer;
30+
import static org.mockito.Mockito.doThrow;
2931
import static org.mockito.Mockito.verify;
3032
import static org.mockito.Mockito.when;
3133
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
@@ -37,6 +39,7 @@
3739
import java.util.concurrent.ConcurrentHashMap;
3840
import java.util.concurrent.ConcurrentMap;
3941
import java.util.concurrent.ExecutorService;
42+
import java.util.concurrent.RejectedExecutionException;
4043
import java.util.concurrent.TimeUnit;
4144
import java.util.concurrent.atomic.AtomicInteger;
4245
import java.util.function.BiFunction;
@@ -46,12 +49,14 @@
4649
import org.junit.jupiter.api.BeforeEach;
4750
import org.junit.jupiter.api.Test;
4851
import org.junit.jupiter.api.extension.ExtendWith;
52+
import org.mockito.ArgumentCaptor;
4953
import org.mockito.Mock;
5054
import org.mockito.Mock.Strictness;
5155
import org.mockito.junit.jupiter.MockitoExtension;
5256

5357
import com.amazon.sns.messaging.lib.concurrent.RingBufferBlockingQueue;
5458
import com.amazon.sns.messaging.lib.core.RequestEntryInternalFactory.RequestEntryInternal;
59+
import com.amazon.sns.messaging.lib.exception.PoisonRequestEntryException;
5560
import com.amazon.sns.messaging.lib.helpers.TryConsumer;
5661
import com.amazon.sns.messaging.lib.model.RequestEntry;
5762
import com.amazon.sns.messaging.lib.model.ResponseFailEntry;
@@ -112,6 +117,42 @@ void testConstructorInitializesTopicRequests() {
112117
assertThat(topicRequests.isEmpty(), is(true));
113118
}
114119

120+
@Test
121+
void testConstructorThrowsNpeWhenTopicPropertyIsNull() {
122+
final NullPointerException thrown = assertThrows(NullPointerException.class, () ->
123+
new TestableAmazonSnsConsumer(amazonSnsClient, null, objectMapper, pendingRequests, topicRequests, executorService, publishDecorator)
124+
);
125+
126+
assertThat(thrown.getMessage(), containsString("topicProperty cannot be null"));
127+
}
128+
129+
@Test
130+
void testConstructorThrowsNpeWhenAmazonSnsClientIsNull() {
131+
final NullPointerException thrown = assertThrows(NullPointerException.class, () ->
132+
new TestableAmazonSnsConsumer(null, topicProperty, objectMapper, pendingRequests, topicRequests, executorService, publishDecorator)
133+
);
134+
135+
assertThat(thrown.getMessage(), containsString("amazonSnsClient cannot be null"));
136+
}
137+
138+
@Test
139+
void testConstructorThrowsNpeWhenObjectMapperIsNull() {
140+
final NullPointerException thrown = assertThrows(NullPointerException.class, () ->
141+
new TestableAmazonSnsConsumer(amazonSnsClient, topicProperty, null, pendingRequests, topicRequests, executorService, publishDecorator)
142+
);
143+
144+
assertThat(thrown.getMessage(), containsString("objectMapper cannot be null"));
145+
}
146+
147+
@Test
148+
void testConstructorThrowsNpeWhenExecutorServiceIsNull() {
149+
final NullPointerException thrown = assertThrows(NullPointerException.class, () ->
150+
new TestableAmazonSnsConsumer(amazonSnsClient, topicProperty, objectMapper, pendingRequests, topicRequests, null, publishDecorator)
151+
);
152+
153+
assertThat(thrown.getMessage(), containsString("executorService cannot be null"));
154+
}
155+
115156
@Test
116157
void testAwaitReturnCompletableFutureWhenQueuesAreEmpty() throws Exception {
117158
context(consumer -> {
@@ -251,6 +292,22 @@ void testRunRecordsCorrectExceptionOnPublishFailure() throws Exception {
251292
});
252293
}
253294

295+
@Test
296+
void testRunHandlesRejectedExecutionExceptionFromExecutorWithoutCrashing() throws Exception {
297+
when(topicProperty.isFifo()).thenReturn(false);
298+
doThrow(new RejectedExecutionException("executor full")).when(executorService).execute(any(Runnable.class));
299+
300+
context(consumer -> {
301+
topicRequests.put(buildRequestEntry("rejected-message"));
302+
303+
await()
304+
.untilAsserted(() -> {
305+
assertThat(consumer.getHandleErrorCallCount(), greaterThanOrEqualTo(1));
306+
assertThat(consumer.getLastError(), instanceOf(RejectedExecutionException.class));
307+
});
308+
});
309+
}
310+
254311
@Test
255312
void testRunDoesNotPublishWhenQueueIsEmpty() throws Exception {
256313
context(consumer -> {
@@ -494,6 +551,38 @@ void testCanAddPayloadSplitsBatchWhenCumulativeSizeExceedsThreshold() throws Exc
494551
});
495552
}
496553

554+
@Test
555+
void testPoisonRequestEntryRemovesFromPendingRequestsAndFailsListenableFuture() throws Exception {
556+
when(topicProperty.isFifo()).thenReturn(true);
557+
558+
final String poisonId = "poison-id";
559+
final String oversizedPayload = buildPayloadOfBytes(TestableAmazonSnsConsumer.batchSizeBytesThreshold() + 100);
560+
final RequestEntry<String> poisonEntry = RequestEntry.<String>builder()
561+
.withId(poisonId)
562+
.withValue(oversizedPayload)
563+
.build();
564+
565+
pendingRequests.put(poisonId, listenableFutureImpl);
566+
567+
context(consumer -> {
568+
topicRequests.put(poisonEntry);
569+
570+
await()
571+
.untilAsserted(() -> {
572+
assertThat(pendingRequests.containsKey(poisonId), is(false));
573+
574+
final ArgumentCaptor<ResponseFailEntry> captor = ArgumentCaptor.forClass(ResponseFailEntry.class);
575+
verify(listenableFutureImpl, atLeastOnce()).fail(captor.capture());
576+
577+
final ResponseFailEntry failEntry = captor.getValue();
578+
assertThat(failEntry.getId(), is(poisonId));
579+
assertThat(failEntry.getCode(), is("000"));
580+
assertThat(failEntry.getSenderFault(), is(true));
581+
assertThat(failEntry.getThrowable(), instanceOf(PoisonRequestEntryException.class));
582+
});
583+
});
584+
}
585+
497586
@Test
498587
void testCanAddPayloadDoesNotPublishEmptyBatchWhenAllEntriesExceedThreshold() throws Exception {
499588
when(topicProperty.isFifo()).thenReturn(true);

0 commit comments

Comments
 (0)