Search in sources :

Example 36 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class EventStreamControllerTest method whenInvalidCursorsThenPreconditionFailed.

@Test
public void whenInvalidCursorsThenPreconditionFailed() throws Exception {
    final NakadiCursor cursor = NakadiCursor.of(timeline, "0", "000000000000000000");
    when(eventTypeRepository.findByName(TEST_EVENT_TYPE_NAME)).thenReturn(EVENT_TYPE);
    when(timelineService.createEventConsumer(eq(KAFKA_CLIENT_ID), any())).thenThrow(new InvalidCursorException(CursorError.UNAVAILABLE, cursor));
    final StreamingResponseBody responseBody = createStreamingResponseBody(1, 0, 0, 0, 0, "[{\"partition\":\"0\",\"offset\":\"00000000000000000\"}]");
    final Problem expectedProblem = Problem.valueOf(PRECONDITION_FAILED, "offset 000000000000000000 for partition 0 is unavailable");
    assertThat(responseToString(responseBody), TestUtils.JSON_TEST_HELPER.matchesObject(expectedProblem));
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Problem(org.zalando.problem.Problem) Test(org.junit.Test)

Example 37 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class SubscriptionControllerTest method whenGetSubscriptionStatThenOk.

@Test
public void whenGetSubscriptionStatThenOk() throws Exception {
    final Subscription subscription = builder().withEventType(TIMELINE.getEventType()).build();
    final Collection<Partition> partitions = Collections.singleton(new Partition(TIMELINE.getEventType(), "0", "xz", null, Partition.State.ASSIGNED));
    final ZkSubscriptionNode zkSubscriptionNode = new ZkSubscriptionNode(partitions, Arrays.asList(new Session("xz", 0)));
    when(subscriptionRepository.getSubscription(subscription.getId())).thenReturn(subscription);
    when(zkSubscriptionClient.getZkSubscriptionNodeLocked()).thenReturn(Optional.of(zkSubscriptionNode));
    final SubscriptionCursorWithoutToken currentOffset = new SubscriptionCursorWithoutToken(TIMELINE.getEventType(), "0", "3");
    final EventTypePartition etp = new EventTypePartition(TIMELINE.getEventType(), "0");
    final Map<EventTypePartition, SubscriptionCursorWithoutToken> offsets = new HashMap<>();
    offsets.put(etp, currentOffset);
    when(zkSubscriptionClient.getOffsets(Collections.singleton(etp))).thenReturn(offsets);
    when(eventTypeRepository.findByName(TIMELINE.getEventType())).thenReturn(EventTypeTestBuilder.builder().name(TIMELINE.getEventType()).build());
    final List<PartitionEndStatistics> statistics = Collections.singletonList(new KafkaPartitionEndStatistics(TIMELINE, 0, 13));
    when(topicRepository.loadTopicEndStatistics(eq(Collections.singletonList(TIMELINE)))).thenReturn(statistics);
    final NakadiCursor currentCursor = mock(NakadiCursor.class);
    when(currentCursor.getEventTypePartition()).thenReturn(new EventTypePartition(TIMELINE.getEventType(), "0"));
    when(cursorConverter.convert((List<SubscriptionCursorWithoutToken>) any())).thenReturn(Collections.singletonList(currentCursor));
    when(cursorOperationsService.calculateDistance(eq(currentCursor), eq(statistics.get(0).getLast()))).thenReturn(10L);
    final List<SubscriptionEventTypeStats> expectedStats = Collections.singletonList(new SubscriptionEventTypeStats(TIMELINE.getEventType(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 10L, "xz", AUTO))));
    getSubscriptionStats(subscription.getId()).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new ItemsWrapper<>(expectedStats))));
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Subscription(org.zalando.nakadi.domain.Subscription) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Session(org.zalando.nakadi.service.subscription.model.Session) Test(org.junit.Test)

Example 38 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class CursorOperationsServiceTest method whenCursorsOffsetsAreInvertedThenNegativeDistance.

@Test
public void whenCursorsOffsetsAreInvertedThenNegativeDistance() throws Exception {
    final NakadiCursor initialCursor = NakadiCursor.of(timeline, "0", "0000000000000002");
    final NakadiCursor finalCursor = NakadiCursor.of(timeline, "0", "0000000000000001");
    final Long distance = service.calculateDistance(initialCursor, finalCursor);
    assertThat(distance, equalTo(-1L));
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 39 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class CursorOperationsServiceTest method shiftCursorBackToPreviousTimeline.

@Test
public void shiftCursorBackToPreviousTimeline() throws Exception {
    final Timeline initialTimeline = mockTimeline(0, 10L);
    final Timeline intermediaryTimeline = mockTimeline(1, 9L);
    final Timeline finalTimeline = mockTimeline(2, 9L);
    mockTimelines(initialTimeline, intermediaryTimeline, finalTimeline);
    final ShiftedNakadiCursor shiftedCursor = new ShiftedNakadiCursor(finalTimeline, "0", "000000000000000003", -15L);
    final NakadiCursor cursor = service.unshiftCursor(shiftedCursor);
    assertThat(cursor.getTimeline().getOrder(), equalTo(0));
    assertThat(cursor.getOffset(), equalTo("000000000000000009"));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Example 40 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class CursorOperationsServiceTest method shiftCursorForwardInTheSameTimelineOpen.

@Test
public void shiftCursorForwardInTheSameTimelineOpen() {
    final Timeline initialTimeline = mockTimeline(0, 10L);
    final ShiftedNakadiCursor shiftedCursor = new ShiftedNakadiCursor(initialTimeline, "0", "000000000000000003", 3L);
    final NakadiCursor cursor = service.unshiftCursor(shiftedCursor);
    assertThat(cursor.getOffset(), equalTo("000000000000000006"));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) Test(org.junit.Test)

Aggregations

NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)56 Timeline (org.zalando.nakadi.domain.Timeline)31 Test (org.junit.Test)27 ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)21 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)14 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)12 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)12 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)11 List (java.util.List)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 Storage (org.zalando.nakadi.domain.Storage)10 TopicRepository (org.zalando.nakadi.repository.TopicRepository)10 Optional (java.util.Optional)8 LoggerFactory (org.slf4j.LoggerFactory)8 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)8 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7