Skip to content

Commit

Permalink
[FLINK-11084]Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarkkkkk committed Dec 7, 2018
1 parent ec04af6 commit e36be96
Showing 1 changed file with 92 additions and 0 deletions.
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.streaming.api.datastream;

import org.apache.flink.streaming.api.collector.selector.OutputSelector;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.DiscardingSink;

import org.apache.commons.collections.IteratorUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* Test the {@link OutputSelector} for consecutive split and select.
*/
public class ConsecutiveSplitSelectTest {

@Test
public void testConsecutiveSplitSelect() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> dataStream = env.fromElements("a1", "a2");

SplitStream<String> splitStream1 = dataStream.split(new OutputSelector<String>() {
@Override public Iterable<String> select(String value) {
List<String> output = new ArrayList<>(1);
if (value.equals("a1")) {
output.add("name1");
} else {
output.add("name2");
}
return output;
}
});
SplitStream<String> splitStream2 = splitStream1.select("name1").split(new OutputSelector<String>() {
@Override public Iterable<String> select(String value) {
List<String> output = new ArrayList<>(1);
if (value.equals("a1")) {
output.add("name4");
} else {
output.add("name5");
}
return output;
}
});
splitStream1.addSink(new DiscardingSink<>());
splitStream2.addSink(new DiscardingSink<>());

List<OutputSelector<?>> outputSelectors = env.getStreamGraph().getStreamNode(1).getOutputSelectors();
OutputSelector<String> outputSelector1 = (OutputSelector<String>) outputSelectors.get(0);
OutputSelector<String> outputSelector2 = (OutputSelector<String>) outputSelectors.get(1);

List<String> actualResult = IteratorUtils.toList(outputSelector1.select("a1").iterator());
List<String> expectedResult = new ArrayList<>();
expectedResult.add("name1");
validateAndCleanUpTwoList(actualResult, expectedResult);

actualResult = IteratorUtils.toList(outputSelector2.select("a1").iterator());
expectedResult.add("name4");
validateAndCleanUpTwoList(actualResult, expectedResult);

actualResult = IteratorUtils.toList(outputSelector1.select("a2").iterator());
expectedResult.add("name2");
validateAndCleanUpTwoList(actualResult, expectedResult);

actualResult = IteratorUtils.toList(outputSelector2.select("a2").iterator());
validateAndCleanUpTwoList(actualResult, expectedResult);
}

private void validateAndCleanUpTwoList(List<String> actualResult, List<String> expectedResult) {
Assert.assertArrayEquals(actualResult.toArray(), expectedResult.toArray());
actualResult.clear();
expectedResult.clear();
}
}

0 comments on commit e36be96

Please sign in to comment.