Search in sources :

Example 51 with TestProcessingTimeService

use of org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService in project flink by apache.

the class StreamSourceOperatorTest method testAutomaticWatermarkContext.

@Test
public void testAutomaticWatermarkContext() throws Exception {
    // regular stream source operator
    final StoppableStreamSource<String, InfiniteSource<String>> operator = new StoppableStreamSource<>(new InfiniteSource<String>());
    long watermarkInterval = 10;
    TestProcessingTimeService processingTimeService = new TestProcessingTimeService();
    processingTimeService.setCurrentTime(0);
    setupSourceOperator(operator, TimeCharacteristic.IngestionTime, watermarkInterval, 0, processingTimeService);
    final List<StreamElement> output = new ArrayList<>();
    StreamSourceContexts.getSourceContext(TimeCharacteristic.IngestionTime, operator.getContainingTask().getProcessingTimeService(), operator.getContainingTask().getCheckpointLock(), operator.getContainingTask().getStreamStatusMaintainer(), new CollectorOutput<String>(output), operator.getExecutionConfig().getAutoWatermarkInterval(), -1);
    for (long i = 1; i < 100; i += watermarkInterval) {
        processingTimeService.setCurrentTime(i);
    }
    assertTrue(output.size() == 9);
    long nextWatermark = 0;
    for (StreamElement el : output) {
        nextWatermark += watermarkInterval;
        Watermark wm = (Watermark) el;
        assertTrue(wm.getTimestamp() == nextWatermark);
    }
}
Also used : StoppableStreamSource(org.apache.flink.streaming.api.operators.StoppableStreamSource) ArrayList(java.util.ArrayList) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) StreamElement(org.apache.flink.streaming.runtime.streamrecord.StreamElement) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 52 with TestProcessingTimeService

use of org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService in project flink by apache.

the class AbstractFetcherTest method testIgnorePartitionStateSentinelInSnapshot.

@Test
public void testIgnorePartitionStateSentinelInSnapshot() throws Exception {
    final String testTopic = "test topic name";
    Map<KafkaTopicPartition, Long> originalPartitions = new HashMap<>();
    originalPartitions.put(new KafkaTopicPartition(testTopic, 1), KafkaTopicPartitionStateSentinel.LATEST_OFFSET);
    originalPartitions.put(new KafkaTopicPartition(testTopic, 2), KafkaTopicPartitionStateSentinel.GROUP_OFFSET);
    originalPartitions.put(new KafkaTopicPartition(testTopic, 3), KafkaTopicPartitionStateSentinel.EARLIEST_OFFSET);
    TestSourceContext<Long> sourceContext = new TestSourceContext<>();
    TestFetcher<Long> fetcher = new TestFetcher<>(sourceContext, originalPartitions, null, /* watermark strategy */
    new TestProcessingTimeService(), 0);
    synchronized (sourceContext.getCheckpointLock()) {
        HashMap<KafkaTopicPartition, Long> currentState = fetcher.snapshotCurrentState();
        fetcher.commitInternalOffsetsToKafka(currentState, new KafkaCommitCallback() {

            @Override
            public void onSuccess() {
            }

            @Override
            public void onException(Throwable cause) {
                throw new RuntimeException("Callback failed", cause);
            }
        });
        assertTrue(fetcher.getLastCommittedOffsets().isPresent());
        assertEquals(Collections.emptyMap(), fetcher.getLastCommittedOffsets().get());
    }
}
Also used : HashMap(java.util.HashMap) TestSourceContext(org.apache.flink.streaming.connectors.kafka.testutils.TestSourceContext) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Test(org.junit.Test)

Example 53 with TestProcessingTimeService

use of org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService in project flink by apache.

the class AbstractFetcherTest method testSkipCorruptedRecord.

// ------------------------------------------------------------------------
// Record emitting tests
// ------------------------------------------------------------------------
@Test
public void testSkipCorruptedRecord() throws Exception {
    final String testTopic = "test topic name";
    Map<KafkaTopicPartition, Long> originalPartitions = new HashMap<>();
    originalPartitions.put(new KafkaTopicPartition(testTopic, 1), KafkaTopicPartitionStateSentinel.LATEST_OFFSET);
    TestSourceContext<Long> sourceContext = new TestSourceContext<>();
    TestFetcher<Long> fetcher = new TestFetcher<>(sourceContext, originalPartitions, null, /* watermark strategy */
    new TestProcessingTimeService(), 0);
    final KafkaTopicPartitionState<Long, Object> partitionStateHolder = fetcher.subscribedPartitionStates().get(0);
    emitRecord(fetcher, 1L, partitionStateHolder, 1L);
    emitRecord(fetcher, 2L, partitionStateHolder, 2L);
    assertEquals(2L, sourceContext.getLatestElement().getValue().longValue());
    assertEquals(2L, partitionStateHolder.getOffset());
    // emit no records
    fetcher.emitRecordsWithTimestamps(emptyQueue(), partitionStateHolder, 3L, Long.MIN_VALUE);
    assertEquals(2L, sourceContext.getLatestElement().getValue().longValue());
    assertEquals(3L, // the offset in state still should have advanced
    partitionStateHolder.getOffset());
}
Also used : TestSourceContext(org.apache.flink.streaming.connectors.kafka.testutils.TestSourceContext) HashMap(java.util.HashMap) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Test(org.junit.Test)

Example 54 with TestProcessingTimeService

use of org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService in project flink by apache.

the class TopicMetadataListenerTest method listenEmptyTopics.

@Test
void listenEmptyTopics() {
    TopicMetadataListener listener = new TopicMetadataListener();
    SinkConfiguration configuration = sinkConfiguration(Duration.ofMinutes(5).toMillis());
    TestProcessingTimeService timeService = new TestProcessingTimeService();
    List<String> topics = listener.availableTopics();
    assertThat(topics).isEmpty();
    listener.open(configuration, timeService);
    topics = listener.availableTopics();
    assertThat(topics).isEmpty();
}
Also used : SinkConfiguration(org.apache.flink.connector.pulsar.sink.config.SinkConfiguration) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Test(org.junit.jupiter.api.Test)

Example 55 with TestProcessingTimeService

use of org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService in project flink by apache.

the class BatchExecutionInternalTimeServiceTest method testForEachProcessingTimeTimerUnsupported.

@Test
public void testForEachProcessingTimeTimerUnsupported() {
    expectedException.expect(UnsupportedOperationException.class);
    expectedException.expectMessage("The BatchExecutionInternalTimeService should not be used in State Processor API");
    BatchExecutionInternalTimeService<Object, Object> timeService = new BatchExecutionInternalTimeService<>(new TestProcessingTimeService(), LambdaTrigger.eventTimeTrigger(timer -> {
    }));
    timeService.forEachEventTimeTimer((o, aLong) -> fail("The forEachProcessingTimeTimer() should not be supported"));
}
Also used : VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) InternalTimeServiceManager(org.apache.flink.streaming.api.operators.InternalTimeServiceManager) Arrays(java.util.Arrays) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Watermark(org.apache.flink.streaming.api.watermark.Watermark) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) VoidNamespaceSerializer(org.apache.flink.runtime.state.VoidNamespaceSerializer) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) InternalTimerService(org.apache.flink.streaming.api.operators.InternalTimerService) TestLogger(org.apache.flink.util.TestLogger) BiConsumer(java.util.function.BiConsumer) Assert.fail(org.junit.Assert.fail) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) ExpectedException(org.junit.rules.ExpectedException) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) KeyContext(org.apache.flink.streaming.api.operators.KeyContext) Triggerable(org.apache.flink.streaming.api.operators.Triggerable) InternalTimer(org.apache.flink.streaming.api.operators.InternalTimer) Test(org.junit.Test) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Consumer(java.util.function.Consumer) TtlTimeProvider(org.apache.flink.runtime.state.ttl.TtlTimeProvider) List(java.util.List) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) AbstractKeyedStateBackend(org.apache.flink.runtime.state.AbstractKeyedStateBackend) Assert(org.junit.Assert) Collections(java.util.Collections) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) Test(org.junit.Test)

Aggregations

TestProcessingTimeService (org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService)78 Test (org.junit.Test)66 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)17 HashMap (java.util.HashMap)16 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)15 ArrayList (java.util.ArrayList)14 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)11 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 Watermark (org.apache.flink.streaming.api.watermark.Watermark)9 List (java.util.List)8 JobID (org.apache.flink.api.common.JobID)8 MockEnvironment (org.apache.flink.runtime.operators.testutils.MockEnvironment)8 VoidNamespace (org.apache.flink.runtime.state.VoidNamespace)8 VoidNamespaceSerializer (org.apache.flink.runtime.state.VoidNamespaceSerializer)8 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 Properties (java.util.Properties)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BiConsumer (java.util.function.BiConsumer)5