Search in sources :

Example 1 with KafkaStartpointToOffsetResolver

use of org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver in project samza by apache.

the class TestKafkaSystemAdminJava method testStartpointUpcomingVisitorShouldResolveToCorrectOffset.

@Test
public void testStartpointUpcomingVisitorShouldResolveToCorrectOffset() {
    // Define dummy variables for testing.
    final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class);
    final KafkaStartpointToOffsetResolver kafkaStartpointToOffsetResolver = new KafkaStartpointToOffsetResolver(consumer);
    final StartpointUpcoming testStartpointSpecific = new StartpointUpcoming();
    // Mock the consumer interactions.
    Mockito.when(consumer.endOffsets(ImmutableSet.of(TEST_TOPIC_PARTITION))).thenReturn(ImmutableMap.of(TEST_TOPIC_PARTITION, 10L));
    // Invoke the consumer with startpoint.
    String resolvedOffset = kafkaStartpointToOffsetResolver.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific);
    Assert.assertEquals(TEST_OFFSET, resolvedOffset);
}
Also used : KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) StartpointUpcoming(org.apache.samza.startpoint.StartpointUpcoming) KafkaStartpointToOffsetResolver(org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver) Test(org.junit.Test)

Example 2 with KafkaStartpointToOffsetResolver

use of org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver in project samza by apache.

the class TestKafkaSystemAdminJava method testStartpointOldestVisitorShouldResolveToCorrectOffset.

@Test
public void testStartpointOldestVisitorShouldResolveToCorrectOffset() {
    // Define dummy variables for testing.
    final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class);
    final KafkaStartpointToOffsetResolver kafkaStartpointToOffsetResolver = new KafkaStartpointToOffsetResolver(consumer);
    final StartpointOldest testStartpointSpecific = new StartpointOldest();
    // Mock the consumer interactions.
    Mockito.when(consumer.beginningOffsets(ImmutableSet.of(TEST_TOPIC_PARTITION))).thenReturn(ImmutableMap.of(TEST_TOPIC_PARTITION, 10L));
    // Invoke the consumer with startpoint.
    String resolvedOffset = kafkaStartpointToOffsetResolver.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific);
    Assert.assertEquals(TEST_OFFSET, resolvedOffset);
}
Also used : KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaStartpointToOffsetResolver(org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver) StartpointOldest(org.apache.samza.startpoint.StartpointOldest) Test(org.junit.Test)

Example 3 with KafkaStartpointToOffsetResolver

use of org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver in project samza by apache.

the class TestKafkaSystemAdminJava method testStartpointTimestampVisitorShouldResolveToCorrectOffsetWhenTimestampDoesNotExist.

@Test
public void testStartpointTimestampVisitorShouldResolveToCorrectOffsetWhenTimestampDoesNotExist() {
    final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class);
    final KafkaStartpointToOffsetResolver kafkaStartpointToOffsetResolver = new KafkaStartpointToOffsetResolver(consumer);
    final StartpointTimestamp startpointTimestamp = new StartpointTimestamp(0L);
    final Map<TopicPartition, OffsetAndTimestamp> offsetForTimesResult = new HashMap<>();
    offsetForTimesResult.put(TEST_TOPIC_PARTITION, null);
    // Mock the consumer interactions.
    Mockito.when(consumer.offsetsForTimes(ImmutableMap.of(TEST_TOPIC_PARTITION, 0L))).thenReturn(offsetForTimesResult);
    Mockito.when(consumer.endOffsets(ImmutableSet.of(TEST_TOPIC_PARTITION))).thenReturn(ImmutableMap.of(TEST_TOPIC_PARTITION, 10L));
    String resolvedOffset = kafkaStartpointToOffsetResolver.visit(TEST_SYSTEM_STREAM_PARTITION, startpointTimestamp);
    Assert.assertEquals(TEST_OFFSET, resolvedOffset);
    // Mock verifications.
    Mockito.verify(consumer).offsetsForTimes(ImmutableMap.of(TEST_TOPIC_PARTITION, 0L));
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaStartpointToOffsetResolver(org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver) StartpointTimestamp(org.apache.samza.startpoint.StartpointTimestamp) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Test(org.junit.Test)

Example 4 with KafkaStartpointToOffsetResolver

use of org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver in project samza by apache.

the class TestKafkaSystemAdminJava method testStartpointSpecificOffsetVisitorShouldResolveToCorrectOffset.

@Test
public void testStartpointSpecificOffsetVisitorShouldResolveToCorrectOffset() {
    final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class);
    final KafkaStartpointToOffsetResolver kafkaStartpointToOffsetResolver = new KafkaStartpointToOffsetResolver(consumer);
    final StartpointSpecific testStartpointSpecific = new StartpointSpecific(TEST_OFFSET);
    // Invoke the consumer with startpoint.
    String resolvedOffset = kafkaStartpointToOffsetResolver.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific);
    Assert.assertEquals(TEST_OFFSET, resolvedOffset);
}
Also used : KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaStartpointToOffsetResolver(org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver) StartpointSpecific(org.apache.samza.startpoint.StartpointSpecific) Test(org.junit.Test)

Example 5 with KafkaStartpointToOffsetResolver

use of org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver in project samza by apache.

the class TestKafkaSystemAdminJava method testStartpointTimestampVisitorShouldResolveToCorrectOffset.

@Test
public void testStartpointTimestampVisitorShouldResolveToCorrectOffset() {
    // Define dummy variables for testing.
    final Long testTimeStamp = 10L;
    final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class);
    final KafkaStartpointToOffsetResolver kafkaStartpointToOffsetResolver = new KafkaStartpointToOffsetResolver(consumer);
    final StartpointTimestamp startpointTimestamp = new StartpointTimestamp(testTimeStamp);
    final Map<TopicPartition, OffsetAndTimestamp> offsetForTimesResult = ImmutableMap.of(TEST_TOPIC_PARTITION, new OffsetAndTimestamp(Long.valueOf(TEST_OFFSET), testTimeStamp));
    // Mock the consumer interactions.
    Mockito.when(consumer.offsetsForTimes(ImmutableMap.of(TEST_TOPIC_PARTITION, testTimeStamp))).thenReturn(offsetForTimesResult);
    Mockito.when(consumer.position(TEST_TOPIC_PARTITION)).thenReturn(Long.valueOf(TEST_OFFSET));
    String resolvedOffset = kafkaStartpointToOffsetResolver.visit(TEST_SYSTEM_STREAM_PARTITION, startpointTimestamp);
    Assert.assertEquals(TEST_OFFSET, resolvedOffset);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaStartpointToOffsetResolver(org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver) StartpointTimestamp(org.apache.samza.startpoint.StartpointTimestamp) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Test(org.junit.Test)

Aggregations

KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)5 KafkaStartpointToOffsetResolver (org.apache.samza.system.kafka.KafkaSystemAdmin.KafkaStartpointToOffsetResolver)5 Test (org.junit.Test)5 OffsetAndTimestamp (org.apache.kafka.clients.consumer.OffsetAndTimestamp)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 StartpointTimestamp (org.apache.samza.startpoint.StartpointTimestamp)2 HashMap (java.util.HashMap)1 StartpointOldest (org.apache.samza.startpoint.StartpointOldest)1 StartpointSpecific (org.apache.samza.startpoint.StartpointSpecific)1 StartpointUpcoming (org.apache.samza.startpoint.StartpointUpcoming)1