rewrite FlatSpec tests using WordSpec #24186

This commit is contained in:
Łukasz Drygała 2018-01-03 12:51:27 +01:00 committed by Johan Andrén
parent 6d6e96179d
commit 0c0bf91661
4 changed files with 145 additions and 135 deletions

View file

@ -3,12 +3,9 @@
*/
package akka.util
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.{ Matchers, WordSpec }
class PrettyDurationSpec extends FlatSpec with Matchers {
behavior of "PrettyDuration"
class PrettyDurationSpec extends WordSpec with Matchers {
import akka.util.PrettyDuration._
@ -27,22 +24,25 @@ class PrettyDurationSpec extends FlatSpec with Matchers {
95.hours "3.958 d" ::
Nil
cases foreach {
case (d, expectedValue)
it should s"print $d nanos as $expectedValue" in {
d.pretty should ===(expectedValue)
}
}
"PrettyDuration" should {
it should "work with infinity" in {
Duration.Inf.pretty should include("infinity")
}
cases foreach {
case (d, expectedValue)
s"print $d nanos as $expectedValue" in {
d.pretty should ===(expectedValue)
}
}
it should "work with -infinity" in {
Duration.MinusInf.pretty should include("minus infinity")
}
"work with infinity" in {
Duration.Inf.pretty should include("infinity")
}
it should "work with undefined" in {
Duration.Undefined.pretty should include("undefined")
"work with -infinity" in {
Duration.MinusInf.pretty should include("minus infinity")
}
"work with undefined" in {
Duration.Undefined.pretty should include("undefined")
}
}
}

View file

@ -4,12 +4,13 @@
package docs.actor
import language.postfixOps
import akka.actor.{ ActorSystem, ActorRef, Props, Terminated }
import akka.actor.{ ActorRef, ActorSystem, Props, Terminated }
import FaultHandlingDocSpec._
import org.scalatest.{ WordSpec, WordSpecLike }
//#testkit
import com.typesafe.config.{ Config, ConfigFactory }
import org.scalatest.{ FlatSpecLike, Matchers, BeforeAndAfterAll }
import org.scalatest.{ Matchers, BeforeAndAfterAll }
import akka.testkit.{ TestActors, TestKit, ImplicitSender, EventFilter }
//#testkit
@ -100,7 +101,7 @@ object FaultHandlingDocSpec {
}
//#testkit
class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system)
with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfterAll {
with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll {
def this() = this(ActorSystem(
"FaultHandlingDocSpec",
@ -115,70 +116,72 @@ class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system)
TestKit.shutdownActorSystem(system)
}
"A supervisor" must "apply the chosen strategy for its child" in {
//#testkit
"A supervisor" must {
"apply the chosen strategy for its child" in {
//#testkit
//#create
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
//#create
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
supervisor ! Props[Child]
val child = expectMsgType[ActorRef] // retrieve answer from TestKits testActor
//#create
EventFilter.warning(occurrences = 1) intercept {
//#resume
child ! 42 // set state to 42
child ! "get"
expectMsg(42)
supervisor ! Props[Child]
val child = expectMsgType[ActorRef] // retrieve answer from TestKits testActor
//#create
EventFilter.warning(occurrences = 1) intercept {
//#resume
child ! 42 // set state to 42
child ! "get"
expectMsg(42)
child ! new ArithmeticException // crash it
child ! "get"
expectMsg(42)
//#resume
}
EventFilter[NullPointerException](occurrences = 1) intercept {
//#restart
child ! new NullPointerException // crash it harder
child ! "get"
expectMsg(0)
//#restart
}
EventFilter[IllegalArgumentException](occurrences = 1) intercept {
//#stop
watch(child) // have testActor watch child
child ! new IllegalArgumentException // break it
expectMsgPF() { case Terminated(`child`) () }
//#stop
}
EventFilter[Exception]("CRASH", occurrences = 2) intercept {
//#escalate-kill
supervisor ! Props[Child] // create new child
val child2 = expectMsgType[ActorRef]
watch(child2)
child2 ! "get" // verify it is alive
expectMsg(0)
child2 ! new Exception("CRASH") // escalate failure
expectMsgPF() {
case t @ Terminated(`child2`) if t.existenceConfirmed ()
child ! new ArithmeticException // crash it
child ! "get"
expectMsg(42)
//#resume
}
//#escalate-kill
//#escalate-restart
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
EventFilter[NullPointerException](occurrences = 1) intercept {
//#restart
child ! new NullPointerException // crash it harder
child ! "get"
expectMsg(0)
//#restart
}
EventFilter[IllegalArgumentException](occurrences = 1) intercept {
//#stop
watch(child) // have testActor watch child
child ! new IllegalArgumentException // break it
expectMsgPF() { case Terminated(`child`) () }
//#stop
}
EventFilter[Exception]("CRASH", occurrences = 2) intercept {
//#escalate-kill
supervisor ! Props[Child] // create new child
val child2 = expectMsgType[ActorRef]
watch(child2)
child2 ! "get" // verify it is alive
expectMsg(0)
supervisor2 ! Props[Child]
val child3 = expectMsgType[ActorRef]
child2 ! new Exception("CRASH") // escalate failure
expectMsgPF() {
case t @ Terminated(`child2`) if t.existenceConfirmed ()
}
//#escalate-kill
//#escalate-restart
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
child3 ! 23
child3 ! "get"
expectMsg(23)
supervisor2 ! Props[Child]
val child3 = expectMsgType[ActorRef]
child3 ! new Exception("CRASH")
child3 ! "get"
expectMsg(0)
//#escalate-restart
child3 ! 23
child3 ! "get"
expectMsg(23)
child3 ! new Exception("CRASH")
child3 ! "get"
expectMsg(0)
//#escalate-restart
}
//#testkit
// code here
}
//#testkit
// code here
}
}
//#testkit

View file

@ -5,9 +5,9 @@ package akka.remote
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.concurrent.Eventually._
import org.scalatest.{ Matchers, WordSpec }
import scala.collection.JavaConverters._
import scala.collection.mutable.Set
import scala.concurrent.duration._
@ -19,7 +19,7 @@ import scala.util.control.NonFatal
* by any network node. Therefore we assume here that the initialization of
* the ActorSystem with the use of remoting will intentionally fail.
*/
class RemoteInitErrorSpec extends FlatSpec with Matchers {
class RemoteInitErrorSpec extends WordSpec with Matchers {
val conf = ConfigFactory.parseString(
"""
akka {
@ -41,17 +41,19 @@ class RemoteInitErrorSpec extends FlatSpec with Matchers {
threads.asScala.collect({ case t: Thread if (!t.isDaemon()) t.getId() })
}
"Remoting" must "shut down properly on RemoteActorRefProvider initialization failure" in {
val start = currentThreadIds()
try {
ActorSystem("duplicate", ConfigFactory.parseString("akka.loglevel=OFF").withFallback(conf))
fail("initialization should fail due to invalid IP address")
} catch {
case NonFatal(e) {
eventually(timeout(30 seconds), interval(800 milliseconds)) {
val current = currentThreadIds()
// no new threads should remain compared to the start state
(current diff start) should be(empty)
"Remoting" must {
"shut down properly on RemoteActorRefProvider initialization failure" in {
val start = currentThreadIds()
try {
ActorSystem("duplicate", ConfigFactory.parseString("akka.loglevel=OFF").withFallback(conf))
fail("initialization should fail due to invalid IP address")
} catch {
case NonFatal(e) {
eventually(timeout(30 seconds), interval(800 milliseconds)) {
val current = currentThreadIds()
// no new threads should remain compared to the start state
(current diff start) should be(empty)
}
}
}
}

View file

@ -5,11 +5,11 @@
package akka.testkit.typed
import akka.actor.typed.scaladsl.Actor
import akka.testkit.typed.Effect.{ Spawned, SpawnedAdapter, SpawnedAnonymous }
import akka.testkit.typed.BehaviorTestkitSpec.{ Child, Father }
import akka.testkit.typed.BehaviorTestkitSpec.Father._
import akka.actor.typed.{ Behavior, Props }
import org.scalatest.{ FlatSpec, Matchers }
import akka.testkit.typed.BehaviorTestkitSpec.Father._
import akka.testkit.typed.BehaviorTestkitSpec.{ Child, Father }
import akka.testkit.typed.Effect.{ Spawned, SpawnedAdapter, SpawnedAnonymous }
import org.scalatest.{ Matchers, WordSpec }
object BehaviorTestkitSpec {
object Father {
@ -78,56 +78,61 @@ object BehaviorTestkitSpec {
}
//TODO WordSpec
class BehaviorTestkitSpec extends FlatSpec with Matchers {
class BehaviorTestkitSpec extends WordSpec with Matchers {
private val props = Props.empty
"BehaviourTestkit's spawn" should "create children when no props specified" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
"BehaviourTestkit's spawn" should {
"create children when no props specified" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnChildren(2))
val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0"), Spawned(Child.initial, "child1", Props.empty))
ctx.run(SpawnChildren(2))
val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0"), Spawned(Child.initial, "child1", Props.empty))
}
"create children when props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnChildrenWithProps(2, props))
val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0", props), Spawned(Child.initial, "child1", props))
}
}
it should "create children when props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
"BehaviourTestkit's spawnAnonymous" should {
"create children when no props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnChildrenWithProps(2, props))
val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0", props), Spawned(Child.initial, "child1", props))
ctx.run(SpawnAnonymous(2))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, Props.empty), SpawnedAnonymous(Child.initial, Props.empty))
}
"create children when props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAnonymousWithProps(2, props))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, props), SpawnedAnonymous(Child.initial, props))
}
}
"BehaviourTestkit's spawnAnonymous" should "create children when no props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
"BehaviourTestkit's spawnAdapter" should {
"create adapters without name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAnonymous(2))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, Props.empty), SpawnedAnonymous(Child.initial, Props.empty))
}
ctx.run(SpawnAdapter)
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAdapter)
}
it should "create children when props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
"create adapters with name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAnonymousWithProps(2, props))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, props), SpawnedAnonymous(Child.initial, props))
}
"BehaviourTestkit's spawnAdapter" should "create adapters without name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAdapter)
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAdapter)
}
it should "create adapters with name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAdapterWithName("adapter"))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAdapter)
ctx.run(SpawnAdapterWithName("adapter"))
val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAdapter)
}
}
}