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();
}
相关推荐
紫丁香11 小时前
pytest_自动化测试3
开发语言·python·功能测试·单元测试·集成测试·pytest
念陌曦13 小时前
Flink总结
大数据·flink
兰.lan15 小时前
【黑马ai测试】Day01课堂笔记+课后作业
软件测试·笔记·python·ai·单元测试
紫丁香17 小时前
pytest_自动化测试1
开发语言·python·功能测试·单元测试·pytest
紫丁香20 小时前
pytest_自动化测试4
python·功能测试·单元测试·集成测试·pytest
大黄说说2 天前
测试金字塔的实战演进:单元测试、集成测试与系统测试的深度解析与高效落地
log4j
Code_LT2 天前
【AIGC】Claude Code 模型配置详解
log4j·aigc
岁岁种桃花儿2 天前
Flink从入门到上天系列第二十五篇:Flink和Kafka连接时的精准一次性
大数据·flink·kafka
Lyyaoo.2 天前
Spring Boot日志
spring boot·缓存·单元测试
Welcome_Back2 天前
SpringBoot后端开发测试全指南
spring boot·后端·log4j