flink的副输出sideoutput单元测试

背景

处理函数中处理输出主输出的数据流数据外,也可以输出多个其他的副输出的数据流数据,当我们的处理函数有副输出时,我们需要测试他们功能的正确性,本文就提供一个测试flink副输出单元测试的例子

测试flink副输出单元测试

首先看一下处理函数,其中包含副输出逻辑

java 复制代码
public class MySideOutputProcessFunction extends ProcessFunction<String, String> {
 
    public static final OutputTag<String> OUTPUT_TAG = new OutputTag<String>("sideoutput") {};
 
    @Override
    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
        out.collect("normal:" + value);
        ctx.output(OUTPUT_TAG, "side:" + value);
    }
}

其次,看下对应的单元测试

java 复制代码
/**
 * 测试sideOutput的输出功能
 */
@Test
public void testSideOutput() throws Exception {
    MySideOutputProcessFunction mySideOutputProcessFunction = new MySideOutputProcessFunction();
    OneInputStreamOperatorTestHarness<String, String> testHarness =
            ProcessFunctionTestHarnesses.forProcessFunction(mySideOutputProcessFunction);
    testHarness.open();
    testHarness.processElement("hello", 10);
 
    // 测试主输出
    Assert.assertEquals(Lists.newArrayList("normal:hello"), testHarness.extractOutputValues());
    ConcurrentLinkedQueue<StreamRecord<String>> sideOutPutQueue =
            testHarness.getSideOutput(MySideOutputProcessFunction.OUTPUT_TAG);
    // 测试副输出
    Assert.assertEquals(Lists.newArrayList("side:hello"),
            sideOutPutQueue.stream().map(StreamRecord::getValue).collect(Collectors.toList()));
    testHarness.close();
}
相关推荐
紫丁香15 小时前
unittest 完全指南:从入门到生产实践
自动化测试·单元测试·unittest
曲幽15 小时前
FastAPI单元测试实战:别等上线被喷才后悔,TestClient用对了真香!
python·单元测试·pytest·api·fastapi·web·httpx·testclient·依赖项覆盖
cccyi720 小时前
【C++ 脚手架】gtest 单元测试库的介绍与使用
c++·单元测试·gtest
不吃香菜学java2 天前
苍穹外卖-菜品分页查询
数据库·spring boot·tomcat·log4j·maven·mybatis
不吃香菜学java2 天前
苍穹外卖-新增菜品代码开发
spring boot·spring·servlet·log4j·maven·mybatis
D愿你归来仍是少年2 天前
Apache Flink Checkpoint 与 Chandy-Lamport 算法深度解析
算法·flink·apache
docsz2 天前
Flink-1.20集群部署
linux·服务器·flink
Java面试题总结2 天前
Junit到Springboot单元测试
spring boot·junit·单元测试
sxhcwgcy2 天前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j