Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TH2-5063] Refactored tests #108

Open
wants to merge 1 commit into
base: dev-version-5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 47 additions & 37 deletions src/test/java/com/exactpro/th2/mstore/TestMessagePersistor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.OngoingStubbing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,19 +47,23 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static com.exactpro.th2.common.utils.ExecutorServiceUtilsKt.shutdownGracefully;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class TestMessagePersistor {
public static final StoredMessage[] EMPTY_STORED_MESSAGE_ARRAY = new StoredMessage[0];
private final Logger logger = LoggerFactory.getLogger(TestMessagePersistor.class);
Expand All @@ -70,12 +77,14 @@ public class TestMessagePersistor {
private static final int MAX_MESSAGE_QUEUE_TASK_SIZE = 8;
private static final long MAX_MESSAGE_QUEUE_DATA_SIZE = 10_000L;

private static final long STORE_ACTION_REJECTION_THRESHOLD = 30000L;
private static final BookId BOOK_ID = new BookId("test-book");

private final CradleStorage storageMock = mock(CradleStorage.class);
private final Callback<GroupedMessageBatchToStore> callback = mock(Callback.class);
private final ErrorCollector errorCollector = mock(ErrorCollector.class);
@Mock
private CradleStorage storageMock;
@Mock
private Callback<GroupedMessageBatchToStore> callback;
@Mock
private ErrorCollector errorCollector;

private MessagePersistor persistor;

Expand All @@ -98,6 +107,8 @@ void setUp() throws InterruptedException, CradleStorageException {
.build();
persistor = spy(new MessagePersistor(errorCollector, storageMock, config));
persistor.start();

reset(persistor);
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved
}

@AfterEach
Expand All @@ -114,17 +125,16 @@ public void testSingleRawMessage() throws Exception {

Instant timestamp = Instant.now();
String group = "test-group";
GroupedMessageBatchToStore batch = batchOf(group, createMessage(BOOK_ID, "test-session", Direction.FIRST,
GroupedMessageBatchToStore batch = batchOf(group, createMessage(
12, timestamp, "raw message".getBytes()));

persistor.persist(batch, callback);
pause(MESSAGE_PERSIST_TIMEOUT);

ArgumentCaptor<GroupedMessageBatchToStore> capture = ArgumentCaptor.forClass(GroupedMessageBatchToStore.class);
verify(storageMock, times(1)).storeGroupedMessageBatchAsync(capture.capture());
verify(persistor, times(1)).processTask(any());
verify(callback, times(1)).onSuccess(any());
verify(callback, times(0)).onFail(any());
verify(storageMock, timeout(MESSAGE_PERSIST_TIMEOUT).times(1)).storeGroupedMessageBatchAsync(capture.capture());
verify(persistor, timeout(MESSAGE_PERSIST_TIMEOUT).times(1)).processTask(any());
verify(callback, timeout(MESSAGE_PERSIST_TIMEOUT).times(1)).onSuccess(any());
verify(callback, timeout(MESSAGE_PERSIST_TIMEOUT).times(0)).onFail(any());

GroupedMessageBatchToStore value = capture.getValue();
assertNotNull(value, "Captured message batch");
Expand All @@ -142,16 +152,16 @@ public void testRawMessageResubmitted() throws Exception {

Instant timestamp = Instant.now();
String group = "test-group";
GroupedMessageBatchToStore batch = batchOf(group, createMessage(BOOK_ID, "test-session", Direction.FIRST,
GroupedMessageBatchToStore batch = batchOf(group, createMessage(
12, timestamp, "raw message".getBytes()));
persistor.persist(batch, callback);
pause(MESSAGE_PERSIST_TIMEOUT * 2);
long timeout = MESSAGE_PERSIST_TIMEOUT * 2;

ArgumentCaptor<GroupedMessageBatchToStore> capture = ArgumentCaptor.forClass(GroupedMessageBatchToStore.class);
verify(persistor, times(2)).processTask(any());
verify(storageMock, times(2)).storeGroupedMessageBatchAsync(capture.capture());
verify(callback, times(1)).onSuccess(any());
verify(callback, times(0)).onFail(any());
verify(persistor, timeout(timeout).times(2)).processTask(any());
verify(storageMock, timeout(timeout).times(2)).storeGroupedMessageBatchAsync(capture.capture());
verify(callback, timeout(timeout).times(1)).onSuccess(any());
verify(callback, timeout(timeout).times(0)).onFail(any());

GroupedMessageBatchToStore value = capture.getValue();
assertNotNull(value, "Captured stored message batch");
Expand All @@ -171,26 +181,31 @@ public void testEventResubmittedLimitedTimes() throws Exception {

Instant timestamp = Instant.now();
String group = "test-group";
GroupedMessageBatchToStore batch = batchOf(group, createMessage(BOOK_ID, "test-session", Direction.FIRST, 12,
GroupedMessageBatchToStore batch = batchOf(group, createMessage(12,
timestamp, "raw message".getBytes()));
persistor.persist(batch, callback);
pause(MESSAGE_PERSIST_TIMEOUT * (MAX_MESSAGE_PERSIST_RETRIES + 1));
verify(persistor).persist(same(batch), same(callback));

long timeout = MESSAGE_PERSIST_TIMEOUT * (MAX_MESSAGE_PERSIST_RETRIES + 1);

ArgumentCaptor<GroupedMessageBatchToStore> capture = ArgumentCaptor.forClass(GroupedMessageBatchToStore.class);
verify(persistor, times(MAX_MESSAGE_PERSIST_RETRIES + 1)).processTask(any());
verify(storageMock, times(MAX_MESSAGE_PERSIST_RETRIES + 1)).storeGroupedMessageBatchAsync(capture.capture());
verify(callback, times(0)).onSuccess(any());
verify(callback, times(1)).onFail(any());
verify(persistor, timeout(timeout).times(MAX_MESSAGE_PERSIST_RETRIES + 1)).processTask(any());
verify(storageMock, timeout(timeout).times(MAX_MESSAGE_PERSIST_RETRIES + 1)).storeGroupedMessageBatchAsync(capture.capture());
verify(callback, timeout(timeout).times(0)).onSuccess(any());
verify(callback, timeout(timeout).times(1)).onFail(any());

GroupedMessageBatchToStore value = capture.getValue();
assertNotNull(value, "Captured stored root event");
assertStoredGroupMessageBatch(batch, value);

verifyNoMoreInteractions(persistor);
verifyNoMoreInteractions(storageMock);
}


@Test
@DisplayName("Message persistence is queued by count")
public void testMessageCountQueueing() throws CradleStorageException, InterruptedException {
public void testMessageCountQueueing() throws CradleStorageException {

final long storeExecutionTime = MESSAGE_PERSIST_TIMEOUT * 3;
final long totalExecutionTime = MESSAGE_PERSIST_TIMEOUT * 5;
Expand All @@ -203,7 +218,7 @@ public void testMessageCountQueueing() throws CradleStorageException, Interrupte
String group = "test-group";
GroupedMessageBatchToStore[] batch = new GroupedMessageBatchToStore[totalMessages];
for (int i = 0; i < totalMessages; i++) {
batch[i] = batchOf(group, createMessage(BOOK_ID, "test-session", Direction.FIRST, i, timestamp,
batch[i] = batchOf(group, createMessage(i, timestamp,
"raw message".getBytes()));
}

Expand Down Expand Up @@ -232,14 +247,13 @@ public void testMessageCountQueueing() throws CradleStorageException, Interrupte
verify(callback, after(totalExecutionTime).times(totalMessages)).onSuccess(any());
verify(callback, after(totalExecutionTime).times(0)).onFail(any());

executor.shutdown();
executor.awaitTermination(0, TimeUnit.MILLISECONDS);
shutdownGracefully(executor,1, TimeUnit.SECONDS);
}


@Test
@DisplayName("Message persistence is queued by message sizes")
public void testMessageSizeQueueing() throws CradleStorageException, InterruptedException {
public void testMessageSizeQueueing() throws CradleStorageException {

final long storeExecutionTime = MESSAGE_PERSIST_TIMEOUT * 3;
final long totalExecutionTime = MESSAGE_PERSIST_TIMEOUT * 6;
Expand All @@ -257,7 +271,7 @@ public void testMessageSizeQueueing() throws CradleStorageException, Interrupted
String group = "test-group";
GroupedMessageBatchToStore[] batch = new GroupedMessageBatchToStore[totalMessages];
for (int i = 0; i < totalMessages; i++) {
batch[i] = batchOf(group, createMessage(BOOK_ID, "test-session", Direction.FIRST, i, timestamp, content));
batch[i] = batchOf(group, createMessage(i, timestamp, content));
}

when(storageMock.storeGroupedMessageBatchAsync(any()))
Expand Down Expand Up @@ -286,8 +300,7 @@ public void testMessageSizeQueueing() throws CradleStorageException, Interrupted
verify(callback, times(totalMessages)).onSuccess(any());
verify(callback, times(0)).onFail(any());

executor.shutdown();
executor.awaitTermination(0, TimeUnit.MILLISECONDS);
shutdownGracefully(executor,1, TimeUnit.SECONDS);
}


Expand All @@ -301,20 +314,17 @@ private void pause(long millis) {


private static MessageToStore createMessage(
BookId bookId,
String session,
Direction direction,
long sequence,
Instant timestamp,
byte[] content
) throws CradleStorageException {

return new MessageToStoreBuilder()
.bookId(bookId)
.sessionAlias(session)
.bookId(TestMessagePersistor.BOOK_ID)
.sessionAlias("test-session")
.content(content)
.timestamp(timestamp)
.direction(direction)
.direction(Direction.FIRST)
.sequence(sequence).build();
}

Expand Down