Apache flink 如何对处理时间计时器进行单元测试?
我正在为Apache flink 如何对处理时间计时器进行单元测试?,apache-flink,flink-streaming,Apache Flink,Flink Streaming,我正在为协处理函数编写单元测试。是否有一种方法可以在测试中手动向前推动处理时间,以触发onTimer调用?Flink提供了测试工具,用于测试带有计时器和状态的功能。 允许您“控制”时间,还可以验证状态的属性 以下代码块是从 让您了解如何使用线束 public class StatefulFlatMapTest { private OneInputStreamOperatorTestHarness<Long, Long> testHarness; private Sta
协处理函数编写单元测试。是否有一种方法可以在测试中手动向前推动处理时间,以触发onTimer
调用?Flink提供了测试工具,用于测试带有计时器和状态的功能。
允许您“控制”时间,还可以验证状态的属性
以下代码块是从
让您了解如何使用线束
public class StatefulFlatMapTest {
private OneInputStreamOperatorTestHarness<Long, Long> testHarness;
private StatefulFlatMap statefulFlatMapFunction;
@Before
public void setupTestHarness() throws Exception {
//instantiate user-defined function
statefulFlatMapFunction = new StatefulFlatMapFunction();
// wrap user defined function into a the corresponding operator
testHarness = new OneInputStreamOperatorTestHarness<>(new StreamFlatMap<>(statefulFlatMapFunction));
// optionally configured the execution environment
testHarness.getExecutionConfig().setAutoWatermarkInterval(50);
// open the test harness (will also call open() on RichFunctions)
testHarness.open();
}
@Test
public void testingStatefulFlatMapFunction() throws Exception {
//push (timestamped) elements into the operator (and hence user defined function)
testHarness.processElement(2L, 100L);
//trigger event time timers by advancing the event time of the operator with a watermark
testHarness.processWatermark(100L);
//trigger processing time timers by advancing the processing time of the operator directly
testHarness.setProcessingTime(100L);
//retrieve list of emitted records for assertions
assertThat(testHarness.getOutput(), containsInExactlyThisOrder(3L));
//retrieve list of records emitted to a specific side output for assertions (ProcessFunction only)
//assertThat(testHarness.getSideOutput(new OutputTag<>("invalidRecords")), hasSize(0))
}
}
公共类StatefulFlatMapTest{
private OneInputStreamOperator测试线束测试线束;
私有StatefulFlatMap StatefulFlatMap函数;
@以前
public void setupTestHarness()引发异常{
//实例化用户定义的函数
statefulFlatMapFunction=新的statefulFlatMapFunction();
//将用户定义的函数包装到相应的运算符中
testHarness=新的OneInputStreamOperatorTestHarness(新的StreamFlatMap(statefulFlatMapFunction));
//可选地配置执行环境
testHarness.getExecutionConfig().setAutoWatermarkInterval(50);
//打开测试线束(也将在函数上调用open())
testHarness.open();
}
@试验
public void testingStatefulFlatMapFunction()引发异常{
//将(带时间戳的)元素推入操作符(因此也是用户定义的函数)
testHarness.processElement(2L,100L);
//通过使用水印提前操作员的事件时间来触发事件时间计时器
testHarness.processWatermark(100L);
//通过直接提前操作员的处理时间触发处理时间计时器
testHarness.setProcessingTime(100L);
//检索断言的已发出记录列表
断言(testHarness.getOutput(),包含sinexactlythisorder(3L));
//检索为断言发送到特定端输出的记录列表(仅限ProcessFunction)
//资产(testHarness.getSideOutput(新的OutputTag(“invalidRecords”)),hasSize(0))
}
}
请查看文档,了解有关包含哪些依赖项、存在哪些线束以及如何使用它们的详细信息。Flink提供了测试线束,用于测试带有计时器和状态的功能。
允许您“控制”时间,还可以验证状态的属性
以下代码块是从
让您了解如何使用线束
public class StatefulFlatMapTest {
private OneInputStreamOperatorTestHarness<Long, Long> testHarness;
private StatefulFlatMap statefulFlatMapFunction;
@Before
public void setupTestHarness() throws Exception {
//instantiate user-defined function
statefulFlatMapFunction = new StatefulFlatMapFunction();
// wrap user defined function into a the corresponding operator
testHarness = new OneInputStreamOperatorTestHarness<>(new StreamFlatMap<>(statefulFlatMapFunction));
// optionally configured the execution environment
testHarness.getExecutionConfig().setAutoWatermarkInterval(50);
// open the test harness (will also call open() on RichFunctions)
testHarness.open();
}
@Test
public void testingStatefulFlatMapFunction() throws Exception {
//push (timestamped) elements into the operator (and hence user defined function)
testHarness.processElement(2L, 100L);
//trigger event time timers by advancing the event time of the operator with a watermark
testHarness.processWatermark(100L);
//trigger processing time timers by advancing the processing time of the operator directly
testHarness.setProcessingTime(100L);
//retrieve list of emitted records for assertions
assertThat(testHarness.getOutput(), containsInExactlyThisOrder(3L));
//retrieve list of records emitted to a specific side output for assertions (ProcessFunction only)
//assertThat(testHarness.getSideOutput(new OutputTag<>("invalidRecords")), hasSize(0))
}
}
公共类StatefulFlatMapTest{
private OneInputStreamOperator测试线束测试线束;
私有StatefulFlatMap StatefulFlatMap函数;
@以前
public void setupTestHarness()引发异常{
//实例化用户定义的函数
statefulFlatMapFunction=新的statefulFlatMapFunction();
//将用户定义的函数包装到相应的运算符中
testHarness=新的OneInputStreamOperatorTestHarness(新的StreamFlatMap(statefulFlatMapFunction));
//可选地配置执行环境
testHarness.getExecutionConfig().setAutoWatermarkInterval(50);
//打开测试线束(也将在函数上调用open())
testHarness.open();
}
@试验
public void testingStatefulFlatMapFunction()引发异常{
//将(带时间戳的)元素推入操作符(因此也是用户定义的函数)
testHarness.processElement(2L,100L);
//通过使用水印提前操作员的事件时间来触发事件时间计时器
testHarness.processWatermark(100L);
//通过直接提前操作员的处理时间触发处理时间计时器
testHarness.setProcessingTime(100L);
//检索断言的已发出记录列表
断言(testHarness.getOutput(),包含sinexactlythisorder(3L));
//检索为断言发送到特定端输出的记录列表(仅限ProcessFunction)
//资产(testHarness.getSideOutput(新的OutputTag(“invalidRecords”)),hasSize(0))
}
}
请查看文档,了解要包含哪些依赖项、存在哪些线束以及如何使用这些线束的详细信息。我已经在使用线束了。我本应该认为,当然,马具有:)。我已经在使用马具了。当然,我本应该想到的是,马具有它:)。