测试组与akka-stream-testkit

时间:2017-05-14 18:16:31

标签: scala unit-testing akka akka-stream

我无法使以下代码正常工作,它会编译,但我收到错误Expected OnNext(_), yet no element signaled during 3 seconds

  

为什么我收到此错误,我需要做些什么来测试类型   下面的流量?

class GeneralTests extends FunSuite {

  implicit val system = ActorSystem("Test-System")
  implicit val materializer = ActorMaterializer()

  case class Wid(id: Int, v: String)

  test("Substream with folds") {
    val (pub, sub) = TestSource.probe[Wid]
    .groupBy(Int.MaxValue, _.id)
      .fold("")((a: String, b: Wid) => a + b.v)
      .grouped(2)
    .mergeSubstreams
    .toMat(TestSink.probe[Seq[String]])(Keep.both)
    .run()

    sub.request(5)
    pub.sendNext(Wid(1,"1"))
    pub.sendNext(Wid(2,"2"))
    sub.expectNext()
    pub.sendNext(Wid(3,"3"))
    pub.sendNext(Wid(4,"4"))
    pub.sendNext(Wid(5,"5"))
    sub.expectNext()
    sub.expectNext()
  }
}

更新:解释我尝试构建的实际流: 我使用的用例是一个连续的(永无止境的)元素流,我需要将其分成子流,然后我可以在给定每个传入消息的情况下并行处理。这就是我现在所拥有的,但是groupBy很棘手。例如,有没有办法清理子流?由于这是一个永无止境的流,可能会积累大量需要清理的子流。

class GeneralTests extends UnitSpec {

  implicit val system = ActorSystem("Test-System")
  implicit val materializer = ActorMaterializer()

  case class Wid(id: Int, v: String)

  val flow = Flow[Wid]
    .map { s ⇒ println(Thread.currentThread().getName() + " ASYNC " + s); s }
    .scan("")((a: String, b: Wid) => a + b.v)
    .sliding(2, step = 1)

  test("Parallel group-by with state") {
    val (pub, sub) = TestSource.probe[Wid]
    .map { s ⇒ println(Thread.currentThread().getName() + " BEFORE " + s); s }
    .groupBy(Int.MaxValue, _.id)
      .via(flow).async
    .mergeSubstreams
    .map { s ⇒ println(Thread.currentThread().getName() + " AFTER " + s); s }
    .toMat(TestSink.probe[Seq[String]])(Keep.both)
    .run()

sub.request(n = 4)
pub.sendNext(Wid(1,"1"))
println(sub.requestNext())
pub.sendNext(Wid(2,"2"))
println(sub.requestNext())
pub.sendNext(Wid(1,"3"))
println(sub.requestNext())
pub.sendNext(Wid(2,"4"))
println(sub.requestNext())
  }
}

但是在某些地方,异步操作在输出中显示的同一个线程(6)上运行。

Test-System-akka.actor.default-dispatcher-3 BEFORE Wid(1,1)
Test-System-akka.actor.default-dispatcher-4 ASYNC Wid(1,1)
Test-System-akka.actor.default-dispatcher-3 AFTER Vector(, 1)
Vector(, 1)
Test-System-akka.actor.default-dispatcher-4 BEFORE Wid(2,2)
Test-System-akka.actor.default-dispatcher-2 ASYNC Wid(2,2)
Test-System-akka.actor.default-dispatcher-2 AFTER Vector(, 2)
Vector(, 2)
Test-System-akka.actor.default-dispatcher-6 BEFORE Wid(1,3)
Test-System-akka.actor.default-dispatcher-6 ASYNC Wid(1,3)
Test-System-akka.actor.default-dispatcher-6 AFTER Vector(1, 13)
Vector(1, 13)
Test-System-akka.actor.default-dispatcher-6 BEFORE Wid(2,4)
Test-System-akka.actor.default-dispatcher-2 ASYNC Wid(2,4)
Test-System-akka.actor.default-dispatcher-6 AFTER Vector(2, 24)
Vector(2, 24)

1 个答案:

答案 0 :(得分:1)

要使fold阶段发出最终值,发布者(即上游)需要完成。

有效序列如下:

sub.request(5)
pub.sendNext(Wid(1,"1"))
pub.sendNext(Wid(2,"2"))
pub.sendNext(Wid(3,"3"))
pub.sendNext(Wid(4,"4"))
pub.sendNext(Wid(5,"5"))
pub.sendComplete()
sub.expectNext()
sub.expectNext()
sub.expectNext()
sub.expectNext()
sub.expectNext()
sub.expectComplete()