Skip to content

Instantly share code, notes, and snippets.

@taketora26
Last active January 27, 2022 02:37
Show Gist options
  • Save taketora26/733a7e47b65805acd3960fc9743837c0 to your computer and use it in GitHub Desktop.
Save taketora26/733a7e47b65805acd3960fc9743837c0 to your computer and use it in GitHub Desktop.
LeaderActorSynchronousSpec
package sample
import akka.actor.testkit.typed.CapturedLogEvent
import akka.actor.testkit.typed.Effect.TimerScheduled
import akka.actor.testkit.typed.scaladsl.{
BehaviorTestKit,
ScalaTestWithActorTestKit
}
import org.scalatest.wordspec.AnyWordSpecLike
import org.slf4j.event.Level
import sample.FollowerActor.ExecTask
import sample.LeaderActor.{Failed, Finish, Start}
class LeaderActorSynchronousSpec
extends ScalaTestWithActorTestKit
with AnyWordSpecLike {
"LeaderActor" must {
// 副作用の確認
"LeaderActorは起動すると、TimerScheduledが実行される" in {
// 同期的なBehaviorTestKitを使い対象のActorの振る舞いをテストする
val testKit = BehaviorTestKit(LeaderActor(Modules))
// 自分自身のメールボックスへ最初にメッセージを送り、Effectを記録する
testKit.runOne()
// LeaderActorにTimerScheduledのEffectが行われたことを確認。
testKit.expectEffectType[TimerScheduled[Start.type]]
}
// メッセージの確認
"LeaderActorにStartを渡すと、子アクターのFollowerActorへExecTaskを送る" in {
val testKit = BehaviorTestKit(LeaderActor(Modules))
// 名前と型を与えて子アクターのメールボックスを取得。
val childInbox =
testKit.childInbox[FollowerActor.Command]("FollowerActor")
// LeaderActorの振る舞いに、メッセージを送りEffectを記録する
testKit.run(Start)
// 子アクターの最も古いメッセージを取得する。メッセージは削除される。
childInbox.receiveMessage() shouldBe an[ExecTask]
}
// ログ出力
"Finishを受け取ると「finish taskId」をInfoログに出力する" in {
val testKit = BehaviorTestKit(LeaderActor(Modules))
testKit.run(Finish("taskId1"))
// Finishを受け取った振る舞いの全てのCapturedLogEventを取得して記録している。
testKit.logEntries() shouldBe Seq(
CapturedLogEvent(Level.INFO, "finish taskId taskId1")
)
}
// ログ出力
"Failedを受け取ると「failed taskId」をWarnログに出力する" in {
val testKit = BehaviorTestKit(LeaderActor(Modules))
testKit.run(Failed("taskId1", "IO Error"))
testKit.logEntries() shouldBe Seq(
CapturedLogEvent(Level.WARN, "failed taskId taskId1. IO Error")
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment