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 package akka.util
import org.scalatest.FlatSpec import org.scalatest.{ Matchers, WordSpec }
import org.scalatest.Matchers
class PrettyDurationSpec extends FlatSpec with Matchers { class PrettyDurationSpec extends WordSpec with Matchers {
behavior of "PrettyDuration"
import akka.util.PrettyDuration._ import akka.util.PrettyDuration._
@ -27,22 +24,25 @@ class PrettyDurationSpec extends FlatSpec with Matchers {
95.hours "3.958 d" :: 95.hours "3.958 d" ::
Nil Nil
cases foreach { "PrettyDuration" should {
case (d, expectedValue)
it should s"print $d nanos as $expectedValue" in {
d.pretty should ===(expectedValue)
}
}
it should "work with infinity" in { cases foreach {
Duration.Inf.pretty should include("infinity") case (d, expectedValue)
} s"print $d nanos as $expectedValue" in {
d.pretty should ===(expectedValue)
}
}
it should "work with -infinity" in { "work with infinity" in {
Duration.MinusInf.pretty should include("minus infinity") Duration.Inf.pretty should include("infinity")
} }
it should "work with undefined" in { "work with -infinity" in {
Duration.Undefined.pretty should include("undefined") 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 package docs.actor
import language.postfixOps import language.postfixOps
import akka.actor.{ ActorSystem, ActorRef, Props, Terminated } import akka.actor.{ ActorRef, ActorSystem, Props, Terminated }
import FaultHandlingDocSpec._ import FaultHandlingDocSpec._
import org.scalatest.{ WordSpec, WordSpecLike }
//#testkit //#testkit
import com.typesafe.config.{ Config, ConfigFactory } import com.typesafe.config.{ Config, ConfigFactory }
import org.scalatest.{ FlatSpecLike, Matchers, BeforeAndAfterAll } import org.scalatest.{ Matchers, BeforeAndAfterAll }
import akka.testkit.{ TestActors, TestKit, ImplicitSender, EventFilter } import akka.testkit.{ TestActors, TestKit, ImplicitSender, EventFilter }
//#testkit //#testkit
@ -100,7 +101,7 @@ object FaultHandlingDocSpec {
} }
//#testkit //#testkit
class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system) 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( def this() = this(ActorSystem(
"FaultHandlingDocSpec", "FaultHandlingDocSpec",
@ -115,70 +116,72 @@ class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system)
TestKit.shutdownActorSystem(system) TestKit.shutdownActorSystem(system)
} }
"A supervisor" must "apply the chosen strategy for its child" in { "A supervisor" must {
//#testkit "apply the chosen strategy for its child" in {
//#testkit
//#create //#create
val supervisor = system.actorOf(Props[Supervisor], "supervisor") val supervisor = system.actorOf(Props[Supervisor], "supervisor")
supervisor ! Props[Child] supervisor ! Props[Child]
val child = expectMsgType[ActorRef] // retrieve answer from TestKits testActor val child = expectMsgType[ActorRef] // retrieve answer from TestKits testActor
//#create //#create
EventFilter.warning(occurrences = 1) intercept { EventFilter.warning(occurrences = 1) intercept {
//#resume //#resume
child ! 42 // set state to 42 child ! 42 // set state to 42
child ! "get" child ! "get"
expectMsg(42) expectMsg(42)
child ! new ArithmeticException // crash it child ! new ArithmeticException // crash it
child ! "get" child ! "get"
expectMsg(42) expectMsg(42)
//#resume //#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 ()
} }
//#escalate-kill EventFilter[NullPointerException](occurrences = 1) intercept {
//#escalate-restart //#restart
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2") 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] child2 ! new Exception("CRASH") // escalate failure
val child3 = expectMsgType[ActorRef] expectMsgPF() {
case t @ Terminated(`child2`) if t.existenceConfirmed ()
}
//#escalate-kill
//#escalate-restart
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
child3 ! 23 supervisor2 ! Props[Child]
child3 ! "get" val child3 = expectMsgType[ActorRef]
expectMsg(23)
child3 ! new Exception("CRASH") child3 ! 23
child3 ! "get" child3 ! "get"
expectMsg(0) expectMsg(23)
//#escalate-restart
child3 ! new Exception("CRASH")
child3 ! "get"
expectMsg(0)
//#escalate-restart
}
//#testkit
// code here
} }
//#testkit
// code here
} }
} }
//#testkit //#testkit

View file

@ -5,9 +5,9 @@ package akka.remote
import akka.actor.ActorSystem import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.concurrent.Eventually._ import org.scalatest.concurrent.Eventually._
import org.scalatest.{ Matchers, WordSpec }
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
import scala.collection.mutable.Set import scala.collection.mutable.Set
import scala.concurrent.duration._ 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 * by any network node. Therefore we assume here that the initialization of
* the ActorSystem with the use of remoting will intentionally fail. * 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( val conf = ConfigFactory.parseString(
""" """
akka { akka {
@ -41,17 +41,19 @@ class RemoteInitErrorSpec extends FlatSpec with Matchers {
threads.asScala.collect({ case t: Thread if (!t.isDaemon()) t.getId() }) threads.asScala.collect({ case t: Thread if (!t.isDaemon()) t.getId() })
} }
"Remoting" must "shut down properly on RemoteActorRefProvider initialization failure" in { "Remoting" must {
val start = currentThreadIds() "shut down properly on RemoteActorRefProvider initialization failure" in {
try { val start = currentThreadIds()
ActorSystem("duplicate", ConfigFactory.parseString("akka.loglevel=OFF").withFallback(conf)) try {
fail("initialization should fail due to invalid IP address") ActorSystem("duplicate", ConfigFactory.parseString("akka.loglevel=OFF").withFallback(conf))
} catch { fail("initialization should fail due to invalid IP address")
case NonFatal(e) { } catch {
eventually(timeout(30 seconds), interval(800 milliseconds)) { case NonFatal(e) {
val current = currentThreadIds() eventually(timeout(30 seconds), interval(800 milliseconds)) {
// no new threads should remain compared to the start state val current = currentThreadIds()
(current diff start) should be(empty) // 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 package akka.testkit.typed
import akka.actor.typed.scaladsl.Actor 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 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 BehaviorTestkitSpec {
object Father { object Father {
@ -78,56 +78,61 @@ object BehaviorTestkitSpec {
} }
//TODO WordSpec class BehaviorTestkitSpec extends WordSpec with Matchers {
class BehaviorTestkitSpec extends FlatSpec with Matchers {
private val props = Props.empty private val props = Props.empty
"BehaviourTestkit's spawn" should "create children when no props specified" in { "BehaviourTestkit's spawn" should {
val ctx = BehaviorTestkit[Father.Command](Father.init()) "create children when no props specified" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnChildren(2)) ctx.run(SpawnChildren(2))
val effects = ctx.retrieveAllEffects() val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0"), Spawned(Child.initial, "child1", Props.empty)) 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 { "BehaviourTestkit's spawnAnonymous" should {
val ctx = BehaviorTestkit[Father.Command](Father.init()) "create children when no props specified and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnChildrenWithProps(2, props)) ctx.run(SpawnAnonymous(2))
val effects = ctx.retrieveAllEffects() val effects = ctx.retrieveAllEffects()
effects should contain only (Spawned(Child.initial, "child0", props), Spawned(Child.initial, "child1", props)) 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 { "BehaviourTestkit's spawnAdapter" should {
val ctx = BehaviorTestkit[Father.Command](Father.init()) "create adapters without name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAnonymous(2)) ctx.run(SpawnAdapter)
val effects = ctx.retrieveAllEffects() val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, Props.empty), SpawnedAnonymous(Child.initial, Props.empty)) effects shouldBe Seq(SpawnedAdapter)
} }
it should "create children when props specified and record effects" in { "create adapters with name and record effects" in {
val ctx = BehaviorTestkit[Father.Command](Father.init()) val ctx = BehaviorTestkit[Father.Command](Father.init())
ctx.run(SpawnAnonymousWithProps(2, props)) ctx.run(SpawnAdapterWithName("adapter"))
val effects = ctx.retrieveAllEffects() val effects = ctx.retrieveAllEffects()
effects shouldBe Seq(SpawnedAnonymous(Child.initial, props), SpawnedAnonymous(Child.initial, props)) effects shouldBe Seq(SpawnedAdapter)
} }
"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)
} }
} }