use of com.hedera.mirror.grpc.domain.TopicMessageFilter in project hedera-mirror-node by hashgraph.
the class TopicMessageServiceTest method missingMessages.
@Test
void missingMessages() {
TopicListener topicListener = Mockito.mock(TopicListener.class);
EntityRepository entityRepository = Mockito.mock(EntityRepository.class);
TopicMessageRetriever topicMessageRetriever = Mockito.mock(TopicMessageRetriever.class);
topicMessageService = new TopicMessageServiceImpl(new GrpcProperties(), topicListener, entityRepository, topicMessageRetriever, new SimpleMeterRegistry());
TopicMessageFilter filter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
TopicMessage beforeMissing = topicMessage(1);
TopicMessage afterMissing = topicMessage(4);
Mockito.when(entityRepository.findById(filter.getTopicId().getId())).thenReturn(Optional.of(Entity.builder().type(EntityType.TOPIC).build()));
Mockito.when(topicMessageRetriever.retrieve(ArgumentMatchers.eq(filter), ArgumentMatchers.eq(true))).thenReturn(Flux.empty());
Mockito.when(topicListener.listen(filter)).thenReturn(Flux.just(beforeMissing, afterMissing));
Mockito.when(topicMessageRetriever.retrieve(ArgumentMatchers.argThat(t -> t.getLimit() == 2 && t.getStartTime().equals(beforeMissing.getConsensusTimestampInstant().plusNanos(1)) && t.getEndTime().equals(afterMissing.getConsensusTimestampInstant())), ArgumentMatchers.eq(false))).thenReturn(Flux.just(topicMessage(2), topicMessage(3)));
topicMessageService.subscribeTopic(filter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNext(1L, 2L, 3L, 4L).thenCancel().verify(Duration.ofMillis(700));
}
use of com.hedera.mirror.grpc.domain.TopicMessageFilter in project hedera-mirror-node by hashgraph.
the class TopicMessageServiceTest method missingMessagesFromRetrieverAndListener.
@Test
void missingMessagesFromRetrieverAndListener() {
TopicListener topicListener = Mockito.mock(TopicListener.class);
EntityRepository entityRepository = Mockito.mock(EntityRepository.class);
TopicMessageRetriever topicMessageRetriever = Mockito.mock(TopicMessageRetriever.class);
topicMessageService = new TopicMessageServiceImpl(new GrpcProperties(), topicListener, entityRepository, topicMessageRetriever, new SimpleMeterRegistry());
TopicMessageFilter retrieverFilter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
TopicMessage retrieved1 = topicMessage(1);
TopicMessage retrieved2 = topicMessage(2);
TopicMessage beforeMissing1 = topicMessage(3);
TopicMessage beforeMissing2 = topicMessage(4);
TopicMessage afterMissing1 = topicMessage(8);
TopicMessage afterMissing2 = topicMessage(9);
TopicMessage afterMissing3 = topicMessage(10);
Mockito.when(entityRepository.findById(retrieverFilter.getTopicId().getId())).thenReturn(Optional.of(Entity.builder().type(EntityType.TOPIC).build()));
TopicMessageFilter listenerFilter = TopicMessageFilter.builder().startTime(retrieved2.getConsensusTimestampInstant()).build();
Mockito.when(topicListener.listen(ArgumentMatchers.argThat(l -> l.getStartTime().equals(listenerFilter.getStartTime())))).thenReturn(Flux.just(beforeMissing1, beforeMissing2, afterMissing1, afterMissing2, afterMissing3));
Mockito.when(topicMessageRetriever.retrieve(ArgumentMatchers.isA(TopicMessageFilter.class), ArgumentMatchers.eq(true))).thenReturn(Flux.just(retrieved1));
Mockito.when(topicMessageRetriever.retrieve(ArgumentMatchers.isA(TopicMessageFilter.class), ArgumentMatchers.eq(false))).thenReturn(// missing historic
Flux.just(retrieved2), Flux.just(// missing incoming
topicMessage(5), topicMessage(6), topicMessage(7)));
topicMessageService.subscribeTopic(retrieverFilter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNext(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).expectComplete().verify(Duration.ofMillis(700));
}
use of com.hedera.mirror.grpc.domain.TopicMessageFilter in project hedera-mirror-node by hashgraph.
the class AbstractSharedTopicListenerTest method slowSubscriberOverflowException.
@Test
@DisplayName("slow subscriber receives overflow exception and normal subscriber is not affected")
void slowSubscriberOverflowException() {
int maxBufferSize = 16;
Duration interval = Duration.ofMillis(10L);
int prefetch = 4;
// step verifier requests 2 messages on subscription, and there are downstream buffers after the backpressure
// buffer, to ensure overflow, set the number of topic messages to send as follows
int numMessages = maxBufferSize + prefetch * 2 + 3;
listenerProperties.setInterval(interval);
listenerProperties.setMaxBufferSize(maxBufferSize);
listenerProperties.setPrefetch(prefetch);
TopicMessageFilter filterFast = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
// create a fast subscriber to keep the shared flux open. the fast subscriber should receive all messages
var stepVerifierFast = topicListener.listen(filterFast).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectNextSequence(LongStream.range(1, numMessages + 1).boxed().collect(Collectors.toList())).thenCancel().verifyLater();
TopicMessageFilter filterSlow = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
// send the messages in two batches and wait 2 * polling interval between. Limit the first batch to
// maxBufferSize messages so it definitely won't cause overflow with the SharedPollingTopicListener.
// The wait also gives the subscriber threads chance to consume messages in slow environment.
Flux<TopicMessage> firstBatch = domainBuilder.topicMessages(maxBufferSize, future);
Flux<TopicMessage> secondBatch = domainBuilder.topicMessages(numMessages - maxBufferSize, future.plusSeconds(1));
// the slow subscriber
topicListener.listen(filterSlow).map(TopicMessage::getSequenceNumber).as(// initial request amount of 1
p -> StepVerifier.create(p, 1)).thenRequest(// trigger subscription
1).thenAwait(Duration.ofMillis(10L)).then(() -> publish(firstBatch)).thenAwait(interval.multipliedBy(2)).then(() -> publish(secondBatch)).expectNext(1L, 2L).thenAwait(// stall to overrun backpressure buffer
Duration.ofMillis(500L)).thenRequest(Long.MAX_VALUE).thenConsumeWhile(n -> n < numMessages).expectErrorMatches(Exceptions::isOverflow).verify(Duration.ofMillis(1000L));
stepVerifierFast.verify(Duration.ofMillis(1000L));
}
use of com.hedera.mirror.grpc.domain.TopicMessageFilter in project hedera-mirror-node by hashgraph.
the class AbstractTopicListenerTest method noMessages.
@Test
void noMessages() {
TopicMessageFilter filter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
topicListener.listen(filter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).expectSubscription().expectTimeout(Duration.ofMillis(500L)).verify();
}
use of com.hedera.mirror.grpc.domain.TopicMessageFilter in project hedera-mirror-node by hashgraph.
the class AbstractTopicListenerTest method lessThanPageSize.
@Test
void lessThanPageSize() {
TopicMessageFilter filter = TopicMessageFilter.builder().startTime(Instant.EPOCH).topicId(topicId).build();
topicListener.listen(filter).map(TopicMessage::getSequenceNumber).as(StepVerifier::create).thenAwait(Duration.ofMillis(50)).then(() -> publish(domainBuilder.topicMessages(2, future))).expectNext(1L, 2L).thenCancel().verify(Duration.ofMillis(500));
}
Aggregations