[英]How should I test akka-streams RestartingSource usage
I'm working on an application that has a couple of long-running streams going, where it subscribes to data about a certain entity and processes that data.我正在开发一个应用程序,该应用程序有几个长时间运行的流,它订阅有关某个实体的数据并处理该数据。 These streams should be up 24/7, so we needed to handle failures (network issues etc).
这些流应该是 24/7,所以我们需要处理故障(网络问题等)。
For that purpose, we've wrapped our sources in RestartingSource
.为此,我们将源包装在
RestartingSource
中。
I'm now trying to verify this behaviour, and while it looks like it functions, I'm struggling to create a test where I push in some data, verify that it processes correctly, then send an error, and verify that it reconnects after that and continues processing.我现在正在尝试验证这种行为,虽然它看起来可以正常工作,但我正在努力创建一个测试,在其中我推送一些数据,验证它是否正确处理,然后发送一个错误,并验证它是否在之后重新连接并继续处理。
I've boiled that down to this minimal case:我把它归结为这个最小的情况:
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{RestartSource, Sink, Source}
import akka.stream.testkit.TestPublisher
import org.scalatest.concurrent.Eventually
import org.scalatest.{FlatSpec, Matchers}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
class MinimalSpec extends FlatSpec with Matchers with Eventually {
"restarting a failed source" should "be testable" in {
implicit val sys: ActorSystem = ActorSystem("akka-grpc-measurements-for-test")
implicit val mat: ActorMaterializer = ActorMaterializer()
implicit val ec: ExecutionContext = sys.dispatcher
val probe = TestPublisher.probe[Int]()
val restartingSource = RestartSource
.onFailuresWithBackoff(1 second, 1 minute, 0d) { () => Source.fromPublisher(probe) }
var last: Int = 0
val sink = Sink.foreach { l: Int => last = l }
restartingSource.runWith(sink)
probe.sendNext(1)
eventually {
last shouldBe 1
}
probe.sendNext(2)
eventually {
last shouldBe 2
}
probe.sendError(new RuntimeException("boom"))
probe.expectSubscription()
probe.sendNext(3)
eventually {
last shouldBe 3
}
}
}
This test consistently fails on the last eventually
block with Last failure message: 2 was not equal to 3
.该测试在最后一个
eventually
块上始终失败,并显示Last failure message: 2 was not equal to 3
。 What am I missing here?我在这里想念什么?
Edit: akka version is 2.5.31
编辑:akka 版本为
2.5.31
I figured it out after having had a look at the TestPublisher
code .在查看了
TestPublisher
代码后,我想通了。 Its subscription is a lazy val
.它的订阅是一个
lazy val
。 So when RestartSource
detects the error, and executes the factory method () => Source.fromPublisher(probe)
again, it gets a new Source
, but the subscription
of the probe
is still pointing to the old Source
.所以当
RestartSource
检测到错误,再次执行工厂方法() => Source.fromPublisher(probe)
时,会得到一个新的Source
,但是probe
的subscription
仍然指向旧的Source
。 Changing the code to initialize both a new Source
and TestPublisher
works.更改代码以初始化新的
Source
和TestPublisher
有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.