Search in sources :

Example 6 with MockBaseSource

use of org.apache.flink.connector.base.source.reader.mocks.MockBaseSource in project flink by apache.

the class HybridSourceReaderTest method testReader.

@Test
public void testReader() throws Exception {
    TestingReaderContext readerContext = new TestingReaderContext();
    TestingReaderOutput<Integer> readerOutput = new TestingReaderOutput<>();
    MockBaseSource source = new MockBaseSource(1, 1, Boundedness.BOUNDED);
    // 2 underlying readers to exercise switch
    SourceReader<Integer, MockSourceSplit> mockSplitReader1 = source.createReader(readerContext);
    SourceReader<Integer, MockSourceSplit> mockSplitReader2 = source.createReader(readerContext);
    HybridSourceReader<Integer> reader = new HybridSourceReader<>(readerContext);
    Assert.assertThat(readerContext.getSentEvents(), Matchers.emptyIterable());
    reader.start();
    assertAndClearSourceReaderFinishedEvent(readerContext, -1);
    Assert.assertNull(currentReader(reader));
    Assert.assertEquals(InputStatus.NOTHING_AVAILABLE, reader.pollNext(readerOutput));
    Source source1 = new MockSource(null, 0) {

        @Override
        public SourceReader<Integer, MockSourceSplit> createReader(SourceReaderContext readerContext) {
            return mockSplitReader1;
        }
    };
    reader.handleSourceEvents(new SwitchSourceEvent(0, source1, false));
    MockSourceSplit mockSplit = new MockSourceSplit(0, 0, 1);
    mockSplit.addRecord(0);
    SwitchedSources switchedSources = new SwitchedSources();
    switchedSources.put(0, source);
    HybridSourceSplit hybridSplit = HybridSourceSplit.wrapSplit(mockSplit, 0, switchedSources);
    reader.addSplits(Collections.singletonList(hybridSplit));
    // drain splits
    InputStatus status = reader.pollNext(readerOutput);
    while (readerOutput.getEmittedRecords().isEmpty() || status == InputStatus.MORE_AVAILABLE) {
        status = reader.pollNext(readerOutput);
        Thread.sleep(10);
    }
    Assert.assertThat(readerOutput.getEmittedRecords(), Matchers.contains(0));
    reader.pollNext(readerOutput);
    Assert.assertEquals("before notifyNoMoreSplits", InputStatus.NOTHING_AVAILABLE, reader.pollNext(readerOutput));
    reader.notifyNoMoreSplits();
    reader.pollNext(readerOutput);
    assertAndClearSourceReaderFinishedEvent(readerContext, 0);
    Assert.assertEquals("reader before switch source event", mockSplitReader1, currentReader(reader));
    Source source2 = new MockSource(null, 0) {

        @Override
        public SourceReader<Integer, MockSourceSplit> createReader(SourceReaderContext readerContext) {
            return mockSplitReader2;
        }
    };
    reader.handleSourceEvents(new SwitchSourceEvent(1, source2, true));
    Assert.assertEquals("reader after switch source event", mockSplitReader2, currentReader(reader));
    reader.notifyNoMoreSplits();
    Assert.assertEquals("reader 1 after notifyNoMoreSplits", InputStatus.END_OF_INPUT, reader.pollNext(readerOutput));
    reader.close();
}
Also used : MockSource(org.apache.flink.api.connector.source.mocks.MockSource) MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) MockSource(org.apache.flink.api.connector.source.mocks.MockSource) MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) Source(org.apache.flink.api.connector.source.Source) TestingReaderOutput(org.apache.flink.connector.testutils.source.reader.TestingReaderOutput) InputStatus(org.apache.flink.core.io.InputStatus) TestingReaderContext(org.apache.flink.connector.testutils.source.reader.TestingReaderContext) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) SourceReaderContext(org.apache.flink.api.connector.source.SourceReaderContext) Test(org.junit.Test)

Example 7 with MockBaseSource

use of org.apache.flink.connector.base.source.reader.mocks.MockBaseSource in project flink by apache.

the class HybridSourceReaderTest method testReaderRecovery.

@Test
public void testReaderRecovery() throws Exception {
    TestingReaderContext readerContext = new TestingReaderContext();
    TestingReaderOutput<Integer> readerOutput = new TestingReaderOutput<>();
    MockBaseSource source = new MockBaseSource(1, 1, Boundedness.BOUNDED);
    HybridSourceReader<Integer> reader = new HybridSourceReader<>(readerContext);
    reader.start();
    assertAndClearSourceReaderFinishedEvent(readerContext, -1);
    reader.handleSourceEvents(new SwitchSourceEvent(0, source, false));
    MockSourceSplit mockSplit = new MockSourceSplit(0, 0, 2147483647);
    SwitchedSources switchedSources = new SwitchedSources();
    switchedSources.put(0, source);
    HybridSourceSplit hybridSplit = HybridSourceSplit.wrapSplit(mockSplit, 0, switchedSources);
    reader.addSplits(Collections.singletonList(hybridSplit));
    List<HybridSourceSplit> snapshot = reader.snapshotState(0);
    Assert.assertThat(snapshot, Matchers.contains(hybridSplit));
    // reader recovery
    readerContext.clearSentEvents();
    reader = new HybridSourceReader<>(readerContext);
    reader.addSplits(snapshot);
    Assert.assertNull(currentReader(reader));
    reader.start();
    Assert.assertNull(currentReader(reader));
    assertAndClearSourceReaderFinishedEvent(readerContext, -1);
    reader.handleSourceEvents(new SwitchSourceEvent(0, source, false));
    Assert.assertNotNull(currentReader(reader));
    Assert.assertThat(reader.snapshotState(1), Matchers.contains(hybridSplit));
    reader.close();
}
Also used : MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) TestingReaderOutput(org.apache.flink.connector.testutils.source.reader.TestingReaderOutput) TestingReaderContext(org.apache.flink.connector.testutils.source.reader.TestingReaderContext) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) Test(org.junit.Test)

Example 8 with MockBaseSource

use of org.apache.flink.connector.base.source.reader.mocks.MockBaseSource in project flink by apache.

the class HybridSourceReaderTest method testDefaultMethodDelegation.

@Test
public void testDefaultMethodDelegation() throws Exception {
    TestingReaderContext readerContext = new TestingReaderContext();
    TestingReaderOutput<Integer> readerOutput = new TestingReaderOutput<>();
    MockBaseSource source = new MockBaseSource(1, 1, Boundedness.BOUNDED) {

        @Override
        public SourceReader<Integer, MockSourceSplit> createReader(SourceReaderContext readerContext) {
            return Mockito.spy(super.createReader(readerContext));
        }
    };
    HybridSourceReader<Integer> reader = new HybridSourceReader<>(readerContext);
    reader.start();
    assertAndClearSourceReaderFinishedEvent(readerContext, -1);
    reader.handleSourceEvents(new SwitchSourceEvent(0, source, false));
    SourceReader<Integer, MockSourceSplit> underlyingReader = currentReader(reader);
    reader.notifyCheckpointComplete(1);
    Mockito.verify(underlyingReader).notifyCheckpointComplete(1);
    reader.notifyCheckpointAborted(1);
    Mockito.verify(underlyingReader).notifyCheckpointAborted(1);
    reader.close();
}
Also used : TestingReaderOutput(org.apache.flink.connector.testutils.source.reader.TestingReaderOutput) MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) TestingReaderContext(org.apache.flink.connector.testutils.source.reader.TestingReaderContext) SourceReaderContext(org.apache.flink.api.connector.source.SourceReaderContext) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) Test(org.junit.Test)

Example 9 with MockBaseSource

use of org.apache.flink.connector.base.source.reader.mocks.MockBaseSource in project flink by apache.

the class HybridSourceTest method testBuilderWithSourceFactory.

@Test
public void testBuilderWithSourceFactory() {
    HybridSource.SourceFactory<Integer, Source<Integer, ?, ?>, MockSplitEnumerator> sourceFactory = new HybridSource.SourceFactory<Integer, Source<Integer, ?, ?>, MockSplitEnumerator>() {

        @Override
        public Source<Integer, ?, ?> create(HybridSource.SourceSwitchContext<MockSplitEnumerator> context) {
            MockSplitEnumerator enumerator = context.getPreviousEnumerator();
            return new MockBaseSource(1, 1, Boundedness.BOUNDED);
        }
    };
    HybridSource<Integer> source = new HybridSource.HybridSourceBuilder<Integer, MockSplitEnumerator>().<MockSplitEnumerator, Source<Integer, ?, ?>>addSource(new MockBaseSource(1, 1, Boundedness.BOUNDED)).addSource(sourceFactory, Boundedness.BOUNDED).build();
    assertNotNull(source);
}
Also used : MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) Source(org.apache.flink.api.connector.source.Source) MockSplitEnumerator(org.apache.flink.connector.base.source.reader.mocks.MockSplitEnumerator) Test(org.junit.Test)

Example 10 with MockBaseSource

use of org.apache.flink.connector.base.source.reader.mocks.MockBaseSource in project flink by apache.

the class CoordinatedSourceITCase method testEnumeratorReaderCommunication.

@Test
public void testEnumeratorReaderCommunication() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    MockBaseSource source = new MockBaseSource(2, 10, Boundedness.BOUNDED);
    DataStream<Integer> stream = env.fromSource(source, WatermarkStrategy.noWatermarks(), "TestingSource");
    executeAndVerify(env, stream, 20);
}
Also used : MockBaseSource(org.apache.flink.connector.base.source.reader.mocks.MockBaseSource) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Test(org.junit.Test)

Aggregations

MockBaseSource (org.apache.flink.connector.base.source.reader.mocks.MockBaseSource)10 Test (org.junit.Test)8 Source (org.apache.flink.api.connector.source.Source)3 MockSourceSplit (org.apache.flink.api.connector.source.mocks.MockSourceSplit)3 TestingReaderContext (org.apache.flink.connector.testutils.source.reader.TestingReaderContext)3 TestingReaderOutput (org.apache.flink.connector.testutils.source.reader.TestingReaderOutput)3 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)3 SourceReaderContext (org.apache.flink.api.connector.source.SourceReaderContext)2 MockSplitEnumerator (org.apache.flink.connector.base.source.reader.mocks.MockSplitEnumerator)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 JobID (org.apache.flink.api.common.JobID)1 MockSource (org.apache.flink.api.connector.source.mocks.MockSource)1 JobClient (org.apache.flink.core.execution.JobClient)1 InputStatus (org.apache.flink.core.io.InputStatus)1