繁体   English   中英

Scala ProcessAllWindowFunction 的单元测试

[英]Scala Unit testing for ProcessAllWindowFunction

After Reading the official flink testing documentation ( https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/testing.html ) I was able to develop tests for a ProcessFunction, using a测试线束,像这样:

pendingPartitionBuilder = new PendingPartitionBuilder(":::some_name", "")

testHarness =
  new OneInputStreamOperatorTestHarness[StaticAdequacyTilePublishedData, PendingPartition](
    new ProcessOperator[StaticAdequacyTilePublishedData, PendingPartition](pendingPartitionBuilder)
  )

testHarness.open()

现在,我正在尝试对 ProcessAllWindowFunction 执行相同的操作,如下所示:

class MapVersionValidationDistributor(batchSize: Int) extends
  ProcessAllWindowFunction[MapVersionValidation, Seq[StaticAdequacyTilePublishedData],TimeWindow] {

lazy val state: ValueState[Long] = getRuntimeContext .getState(new ValueStateDescriptor[Long]("latestMapVersion", classOf[Long]))
(...)

首先我意识到我不能对 ProcessAllWindowFunction 使用 TestHarness,因为它没有 processElement 方法。 在这种情况下,我应该遵循什么单元测试策略?

编辑:目前我的测试代码如下所示:

val collector = mock[Collector[Seq[StaticAdequacyTilePublishedData]]]
    val mvv = new MapVersionValidationDistributor(1)
    val input3 = Iterable(new MapVersionValidation("123",Seq(TileValidation(1,true,Seq(1,3,4)))))

    val ctx = mock[mvv.Context]
    val streamContext =  mock[RuntimeContext]
    mvv.setRuntimeContext(streamContext)

    mvv.open(mock[Configuration])
    mvv.process(ctx,input3,collector)

我收到了这个错误:

 Unexpected call: <mock-3> RuntimeContext.getState[T](ValueStateDescriptor{name=latestMapVersion, defaultValue=null, serializer=null}) Expected: inAnyOrder { }

您真的不需要测试工具来对ProcessAllWindowFunctionprocess方法进行单元测试。 process function 需要 3 个 arguments: ContextIterable[IN]Collector[OUT] 您可以根据用于模拟Context的语言使用一些库。 您还可以在此处根据您的偏好轻松实现或模拟Collector 而 Iterable[IN] 只是一个包含您的 window 元素的Iterable ,在触发 window 后将传递给 function。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM