Search in sources :

Example 1 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class FlinkKafkaConsumerBaseTest method getConsumer.

// ------------------------------------------------------------------------
private static <T> FlinkKafkaConsumerBase<T> getConsumer(AbstractFetcher<T, ?> fetcher, LinkedMap pendingOffsetsToCommit, boolean running) throws Exception {
    FlinkKafkaConsumerBase<T> consumer = new DummyFlinkKafkaConsumer<>();
    StreamingRuntimeContext mockRuntimeContext = mock(StreamingRuntimeContext.class);
    Mockito.when(mockRuntimeContext.isCheckpointingEnabled()).thenReturn(true);
    consumer.setRuntimeContext(mockRuntimeContext);
    Field fetcherField = FlinkKafkaConsumerBase.class.getDeclaredField("kafkaFetcher");
    fetcherField.setAccessible(true);
    fetcherField.set(consumer, fetcher);
    Field mapField = FlinkKafkaConsumerBase.class.getDeclaredField("pendingOffsetsToCommit");
    mapField.setAccessible(true);
    mapField.set(consumer, pendingOffsetsToCommit);
    Field runningField = FlinkKafkaConsumerBase.class.getDeclaredField("running");
    runningField.setAccessible(true);
    runningField.set(consumer, running);
    return consumer;
}
Also used : Field(java.lang.reflect.Field) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext)

Example 2 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class KafkaConsumer08Test method testCreateSourceWithoutCluster.

@Test
public void testCreateSourceWithoutCluster() {
    try {
        Properties props = new Properties();
        props.setProperty("zookeeper.connect", "localhost:56794");
        props.setProperty("bootstrap.servers", "localhost:11111, localhost:22222");
        props.setProperty("group.id", "non-existent-group");
        props.setProperty(FlinkKafkaConsumer08.GET_PARTITIONS_RETRIES_KEY, "1");
        FlinkKafkaConsumer08<String> consumer = new FlinkKafkaConsumer08<>(Collections.singletonList("no op topic"), new SimpleStringSchema(), props);
        StreamingRuntimeContext mockRuntimeContext = mock(StreamingRuntimeContext.class);
        Mockito.when(mockRuntimeContext.isCheckpointingEnabled()).thenReturn(true);
        consumer.setRuntimeContext(mockRuntimeContext);
        consumer.open(new Configuration());
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Unable to retrieve any partitions"));
    }
}
Also used : StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) SimpleStringSchema(org.apache.flink.streaming.util.serialization.SimpleStringSchema) Properties(java.util.Properties) UnknownHostException(java.net.UnknownHostException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class BoltWrapperTest method testOpenSink.

@SuppressWarnings("unchecked")
@Test
public void testOpenSink() throws Exception {
    final StormConfig stormConfig = new StormConfig();
    final Configuration flinkConfig = new Configuration();
    final ExecutionConfig taskConfig = mock(ExecutionConfig.class);
    when(taskConfig.getGlobalJobParameters()).thenReturn(null).thenReturn(stormConfig).thenReturn(flinkConfig);
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(taskConfig);
    when(taskContext.getTaskName()).thenReturn("name");
    when(taskContext.getMetricGroup()).thenReturn(new UnregisteredMetricsGroup());
    final IRichBolt bolt = mock(IRichBolt.class);
    BoltWrapper<Object, Object> wrapper = new BoltWrapper<Object, Object>(bolt);
    wrapper.setup(createMockStreamTask(), new StreamConfig(new Configuration()), mock(Output.class));
    wrapper.open();
    verify(bolt).prepare(any(Map.class), any(TopologyContext.class), isNotNull(OutputCollector.class));
}
Also used : StormConfig(org.apache.flink.storm.util.StormConfig) IRichBolt(org.apache.storm.topology.IRichBolt) OutputCollector(org.apache.storm.task.OutputCollector) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) UnmodifiableConfiguration(org.apache.flink.configuration.UnmodifiableConfiguration) Configuration(org.apache.flink.configuration.Configuration) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Output(org.apache.flink.streaming.api.operators.Output) TopologyContext(org.apache.storm.task.TopologyContext) Map(java.util.Map) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class BoltWrapperTest method testOpen.

@SuppressWarnings("unchecked")
@Test
public void testOpen() throws Exception {
    // utility mocks
    final StormConfig stormConfig = new StormConfig();
    final Configuration flinkConfig = new Configuration();
    final ExecutionConfig taskConfig = mock(ExecutionConfig.class);
    when(taskConfig.getGlobalJobParameters()).thenReturn(null).thenReturn(stormConfig).thenReturn(flinkConfig);
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(taskConfig);
    when(taskContext.getTaskName()).thenReturn("name");
    when(taskContext.getMetricGroup()).thenReturn(new UnregisteredMetricsGroup());
    final SetupOutputFieldsDeclarer declarer = new SetupOutputFieldsDeclarer();
    declarer.declare(new Fields("dummy"));
    PowerMockito.whenNew(SetupOutputFieldsDeclarer.class).withNoArguments().thenReturn(declarer);
    // (1) open with no configuration
    {
        ExecutionConfig execConfig = mock(ExecutionConfig.class);
        when(execConfig.getGlobalJobParameters()).thenReturn(null);
        final IRichBolt bolt = mock(IRichBolt.class);
        BoltWrapper<Object, Object> wrapper = new BoltWrapper<Object, Object>(bolt);
        wrapper.setup(createMockStreamTask(execConfig), new StreamConfig(new Configuration()), mock(Output.class));
        wrapper.open();
        verify(bolt).prepare(any(Map.class), any(TopologyContext.class), any(OutputCollector.class));
    }
    // (2) open with a storm specific configuration
    {
        ExecutionConfig execConfig = mock(ExecutionConfig.class);
        when(execConfig.getGlobalJobParameters()).thenReturn(stormConfig);
        final IRichBolt bolt = mock(IRichBolt.class);
        BoltWrapper<Object, Object> wrapper = new BoltWrapper<Object, Object>(bolt);
        wrapper.setup(createMockStreamTask(execConfig), new StreamConfig(new Configuration()), mock(Output.class));
        wrapper.open();
        verify(bolt).prepare(same(stormConfig), any(TopologyContext.class), any(OutputCollector.class));
    }
    // (3) open with a flink config
    {
        final Configuration cfg = new Configuration();
        cfg.setString("foo", "bar");
        cfg.setInteger("the end (the int)", Integer.MAX_VALUE);
        ExecutionConfig execConfig = mock(ExecutionConfig.class);
        when(execConfig.getGlobalJobParameters()).thenReturn(new UnmodifiableConfiguration(cfg));
        TestDummyBolt testBolt = new TestDummyBolt();
        BoltWrapper<Object, Object> wrapper = new BoltWrapper<Object, Object>(testBolt);
        wrapper.setup(createMockStreamTask(execConfig), new StreamConfig(new Configuration()), mock(Output.class));
        wrapper.open();
        for (Entry<String, String> entry : cfg.toMap().entrySet()) {
            Assert.assertEquals(entry.getValue(), testBolt.config.get(entry.getKey()));
        }
    }
}
Also used : StormConfig(org.apache.flink.storm.util.StormConfig) IRichBolt(org.apache.storm.topology.IRichBolt) UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) UnmodifiableConfiguration(org.apache.flink.configuration.UnmodifiableConfiguration) Configuration(org.apache.flink.configuration.Configuration) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Entry(java.util.Map.Entry) Fields(org.apache.storm.tuple.Fields) TestDummyBolt(org.apache.flink.storm.util.TestDummyBolt) UnmodifiableConfiguration(org.apache.flink.configuration.UnmodifiableConfiguration) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class SpoutWrapperTest method testRunExecuteFixedNumber.

@SuppressWarnings("unchecked")
@Test
public void testRunExecuteFixedNumber() throws Exception {
    final SetupOutputFieldsDeclarer declarer = new SetupOutputFieldsDeclarer();
    declarer.declare(new Fields("dummy"));
    PowerMockito.whenNew(SetupOutputFieldsDeclarer.class).withNoArguments().thenReturn(declarer);
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(mock(ExecutionConfig.class));
    when(taskContext.getTaskName()).thenReturn("name");
    final IRichSpout spout = mock(IRichSpout.class);
    final int numberOfCalls = this.r.nextInt(50);
    final SpoutWrapper<?> spoutWrapper = new SpoutWrapper<Object>(spout, numberOfCalls);
    spoutWrapper.setRuntimeContext(taskContext);
    spoutWrapper.run(mock(SourceContext.class));
    verify(spout, times(numberOfCalls)).nextTuple();
}
Also used : Fields(org.apache.storm.tuple.Fields) IRichSpout(org.apache.storm.topology.IRichSpout) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) SourceContext(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StreamingRuntimeContext (org.apache.flink.streaming.api.operators.StreamingRuntimeContext)28 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)13 Configuration (org.apache.flink.configuration.Configuration)11 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 AbstractTest (org.apache.flink.storm.util.AbstractTest)9 Map (java.util.Map)5 Properties (java.util.Properties)5 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)5 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)4 SourceContext (org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext)4 IRichBolt (org.apache.storm.topology.IRichBolt)4 HashMap (java.util.HashMap)3 Internal (org.apache.flink.annotation.Internal)3 RuntimeContextInitializationContextAdapters (org.apache.flink.api.common.serialization.RuntimeContextInitializationContextAdapters)3 StormConfig (org.apache.flink.storm.util.StormConfig)3 IRichSpout (org.apache.storm.topology.IRichSpout)3