简体   繁体   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 Test Harness, something like this: 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()

now, I'm trying to do the same for a ProcessAllWindowFunction, that looks like this:现在,我正在尝试对 ProcessAllWindowFunction 执行相同的操作,如下所示:

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

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

First I realized I can't use TestHarness for ProcessAllWindowFunction, because it doesn't have a processElement method.首先我意识到我不能对 ProcessAllWindowFunction 使用 TestHarness,因为它没有 processElement 方法。 In this case, what unit test strategy should I follow?在这种情况下,我应该遵循什么单元测试策略?

EDIT: At the moment my test code looks like this:编辑:目前我的测试代码如下所示:

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)

and I'm getting this error:我收到了这个错误:

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

You don't really need test harness to unit test the process method of the ProcessAllWindowFunction .您真的不需要测试工具来对ProcessAllWindowFunctionprocess方法进行单元测试。 The process function takes 3 arguments: Context , Iterable[IN] , Collector[OUT] . process function 需要 3 个 arguments: ContextIterable[IN]Collector[OUT] You can use some library depending on the language used to mock the Context .您可以根据用于模拟Context的语言使用一些库。 You can also easily implement or mock the Collector depending on your prerefences here.您还可以在此处根据您的偏好轻松实现或模拟Collector And the Iterable[IN] is just an Iterable containing the elements of Your window, that would be passed to the function after the window is triggered.而 Iterable[IN] 只是一个包含您的 window 元素的Iterable ,在触发 window 后将传递给 function。

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

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