[英]How should I test akka-streams RestartingSource usage
我正在開發一個應用程序,該應用程序有幾個長時間運行的流,它訂閱有關某個實體的數據並處理該數據。 這些流應該是 24/7,所以我們需要處理故障(網絡問題等)。
為此,我們將源包裝在RestartingSource
中。
我現在正在嘗試驗證這種行為,雖然它看起來可以正常工作,但我正在努力創建一個測試,在其中我推送一些數據,驗證它是否正確處理,然后發送一個錯誤,並驗證它是否在之后重新連接並繼續處理。
我把它歸結為這個最小的情況:
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
}
}
}
該測試在最后一個eventually
塊上始終失敗,並顯示Last failure message: 2 was not equal to 3
。 我在這里想念什么?
編輯:akka 版本為2.5.31
在查看了TestPublisher
代碼后,我想通了。 它的訂閱是一個lazy val
。 所以當RestartSource
檢測到錯誤,再次執行工廠方法() => Source.fromPublisher(probe)
時,會得到一個新的Source
,但是probe
的subscription
仍然指向舊的Source
。 更改代碼以初始化新的Source
和TestPublisher
有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.