写入单元测试以断言flink函数实例是可序列化的

时间:2019-06-18 07:28:06

标签: unit-testing apache-flink serializable

我在运行时遇到此异常:

org.apache.flink.api.common.InvalidProgramException: The implementation of the RichFlatMapFunction is not serializable. The object probably contains or references non serializable fields. 

虽然我了解发生了什么并知道如何解决它,但我想确保它不会再次发生。当有人向该RichFlatMapFunction类添加不可序列化的字段时,我希望单元测试失败,而不是在运行时失败。

是否可以使用与flink相同的函数序列化代码编写可断言该函数可序列化的单元测试?

1 个答案:

答案 0 :(得分:0)

在这种情况下,请改用集成测试

在以下代码中,行env.execute();将运行管道并序列化运算符MultiplyByTwoCollectSink

您可以使用相同的方法来测试RichFlatMapFunction是否可序列化。

public class ExampleIntegrationTest extends AbstractTestBase {

    @Test
    public void testMultiply() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // configure your test environment
        env.setParallelism(1);

        // values are collected in a static variable
        CollectSink.values.clear();

        // create a stream of custom elements and apply transformations
        env.fromElements(1L, 21L, 22L)
                .map(new MultiplyByTwo())
                .addSink(new CollectSink());

        // execute
        env.execute();

        // verify your results
        assertEquals(Lists.newArrayList(2L, 42L, 44L), CollectSink.values);
    }

    // create a testing sink
    private static class CollectSink implements SinkFunction<Long> {

        // must be static
        public static final List<Long> values = new ArrayList<>();

        @Override
        public synchronized void invoke(Long value) throws Exception {
            values.add(value);
        }
    }
}

参考:https://ci.apache.org/projects/flink/flink-docs-stable/dev/stream/testing.html#integration-testing