【问题标题】:Why does the Akka Streams TestSource not receive every pull as a request?为什么 Akka Streams TestSource 没有收到每个拉取请求?
【发布时间】:2016-12-09 23:42:39
【问题描述】:

我正在尝试跟踪自定义流程中的错误,其中上游有额外的请求。我写了一个测试用例,考虑到我看到的行为和日志记录,它应该失败,但它没有。

然后我做了一个带有明显额外拉力的微不足道的 Flow,但测试仍然没有失败。

这是一个例子:

class EagerFlow extends GraphStage[FlowShape[String, String]] {

  val in: Inlet[String] = Inlet("EagerFlow.in")
  val out: Outlet[String] = Outlet("EagerFlow.out")

  override def shape: FlowShape[String, String] = FlowShape(in, out)

  override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
    setHandler(in, new InHandler {
      override def onPush() = {
        println("EagerFlow.in.onPush")
        println("EagerFlow.out.push")
        push(out, grab(in))
        println("EagerFlow.in.pull")
        pull(in)
      }
    })
    setHandler(out, new OutHandler {
      override def onPull() = {
        println("EagerFlow.out.onPull")
        println("EagerFlow.in.pull")
        pull(in)
      }
    })
  }
}

这是一个应该通过的测试,即确认有额外的请求。

"Eager Flow" should "pull on every push" in {
  val sourceProbe = TestSource.probe[String]
  val sinkProbe = TestSink.probe[String]

  val eagerFlow = Flow.fromGraph(new EagerFlow)

  val (source, sink) = sourceProbe.
    via(eagerFlow).
    toMat(sinkProbe)(Keep.both).
    run

  sink.request(1)
  source.expectRequest()
  source.expectNoMsg()

  source.sendNext("hello")
  sink.expectNext()
  source.expectRequest()
}

在标准输出中可以看到:

0C3E08A8 PULL DownstreamBoundary -> TestSpec$EagerFlow@1d154180 (TestSpec$EagerFlow$$anon$4$$anon$6@7e62ecbd) [TestSpec$EagerFlow$$anon$4@36904dd4]
EagerFlow.out.onPull
EagerFlow.in.pull
0C3E08A8 PULL TestSpec$EagerFlow@1d154180 -> UpstreamBoundary (BatchingActorInputBoundary(id=0, fill=0/16, completed=false, canceled=false)) [UpstreamBoundary]
EagerFlow.in.onPush
EagerFlow.out.push
EagerFlow.in.pull
0C3E08A8 PULL TestSpec$EagerFlow@1d154180 -> UpstreamBoundary (BatchingActorInputBoundary(id=0, fill=0/16, completed=false, canceled=false)) [UpstreamBoundary]

assertion failed: timeout (3 seconds) during expectMsg:
  java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg:
  at scala.Predef$.assert(Predef.scala:170)
at akka.testkit.TestKitBase$class.expectMsgPF(TestKit.scala:368)
at akka.testkit.TestKit.expectMsgPF(TestKit.scala:737)
at akka.stream.testkit.StreamTestKit$PublisherProbeSubscription.expectRequest(StreamTestKit.scala:665)
at akka.stream.testkit.TestPublisher$Probe.expectRequest(StreamTestKit.scala:172)

(这是akka.stream.impl.fusing.GraphInterpreter#processEvent 中的调试日志,实际上使用打印点但实际上是相同的东西)

为什么这个拉动在UpstreamBoundary 中停止并且永远不会回到TestSource.probe?

我有很多测试使用expectNoMsg() 来确保正确的背压,但似乎这些只会给出误报。如果这不起作用,我应该如何测试?

【问题讨论】:

    标签: scala scalatest akka-stream


    【解决方案1】:

    要使您的测试适用,您需要将 Flow 缓冲区的大小设置为 1。

    val settings = ActorMaterializerSettings(system)
      .withInputBuffer(initialSize = 1, maxSize = 1)
    implicit val mat = ActorMaterializer(settings)
    

    默认尺寸为:

      # Initial size of buffers used in stream elements
      initial-input-buffer-size = 4
      # Maximum size of buffers used in stream elements
      max-input-buffer-size = 16
    

    这意味着 Akka 将在您的第一次请求无论如何时急切地请求和摄取多达 16 个元素。有关内部缓冲区的更多信息,请参阅here。

    【讨论】:

    • 是的,你是对的。我可以看到 BatchingActorInputBoundary 正在为 16 个元素调用 tryRequest。首先如何创建异步边界似乎仍然有点倒退。我原以为 TestKit 提供了一些东西,使测试单个阶段更加直观。我没有意识到的另一件事是Probe.expectRequest() 允许多个并返回数字。如果它有一个复数名称会更明显。
    • 2021 年仍然是一个陷阱。你拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2017-06-12
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多