Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3206 - Adding expectTerminated #1314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion akka-actor-tests/src/test/scala/akka/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object TestUtils {
def verifyActorTermination(actor: ActorRef)(implicit system: ActorSystem): Unit = {
val watcher = TestProbe()
watcher.watch(actor)
assert(watcher.expectMsgType[Terminated].actor == actor)
watcher.expectTerminated(actor)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ActorLookupSpec extends AkkaSpec with DefaultTimeout {
val a1 = system.actorOf(p, name)
watch(a1)
a1 ! PoisonPill
expectMsgType[Terminated].actor must be === a1
expectTerminated(a1)

// not equal because it's terminated
system.actorFor(a1.path.toString) must not be (a1)
Expand All @@ -94,7 +94,7 @@ class ActorLookupSpec extends AkkaSpec with DefaultTimeout {

watch(a2)
a2 ! PoisonPill
expectMsgType[Terminated].actor must be === a2
expectTerminated(a2)
}

"find actors by looking up their root-anchored relative path" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LocalActorRefProviderSpec extends AkkaSpec(LocalActorRefProviderSpec.confi
val childProps1 = child.asInstanceOf[LocalActorRef].underlying.props
childProps1 must be(Props.empty)
system stop a
expectMsgType[Terminated]
expectTerminated(a)
val childProps2 = child.asInstanceOf[LocalActorRef].underlying.props
childProps2 must not be theSameInstanceAs(childProps1)
childProps2 must be theSameInstanceAs ActorCell.terminatedProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TcpListenerSpec extends AkkaSpec("akka.io.tcp.batch-accept-limit = 2") {
unbindCommander.send(listener, Unbind)

unbindCommander.expectMsg(Unbound)
parent.expectMsgType[Terminated].actor must be(listener)
parent.expectTerminated(listener)
}

"drop an incoming connection if it cannot be registered with a selector" in new TestSetup {
Expand Down
10 changes: 3 additions & 7 deletions akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with
watch(router)
watch(c2)
system.stop(c2)
expectMsgPF() {
case t @ Terminated(`c2`) if t.existenceConfirmed == true ⇒ t
}
expectTerminated(c2).existenceConfirmed must be === true
// it might take a while until the Router has actually processed the Terminated message
awaitCond {
router ! ""
Expand All @@ -95,9 +93,7 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with
res == Seq(c1, c1)
}
system.stop(c1)
expectMsgPF() {
case t @ Terminated(`router`) if t.existenceConfirmed == true ⇒ t
}
expectTerminated(router).existenceConfirmed must be === true
}

"not terminate when resizer is used" in {
Expand Down Expand Up @@ -152,7 +148,7 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with
expectMsgType[RouterRoutees].routees.size must be(3)
watch(router)
system.stop(router)
expectMsgType[Terminated]
expectTerminated(router)
}

"use configured nr-of-instances when router is specified" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ abstract class ClusterDeathWatchSpec
// removed
awaitAssert(clusterView.unreachableMembers.map(_.address) must not contain (address(first)))

val t = expectMsgType[Terminated]
t.actor must be(hello)
expectTerminated(hello)

enterBarrier("first-unavailable")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class ClusterSingletonManagerSpec extends MultiNodeSpec(ClusterSingletonManagerS
case ActorIdentity("singleton", None) ⇒ // already terminated
case ActorIdentity("singleton", Some(singleton)) ⇒
watch(singleton)
expectMsgType[Terminated].actor must be(singleton)
expectTerminated(singleton)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PeekMailboxSpec extends AkkaSpec("""
}
expectMsg("DIE")
expectMsgType[DeadLetter].message must be("DIE")
expectMsgType[Terminated].actor must be(a)
expectTerminated(a)
}

}
Expand Down
2 changes: 1 addition & 1 deletion akka-docs/rst/scala/code/docs/testkit/TestkitDocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
val probe = TestProbe()
probe watch target
target ! PoisonPill
probe.expectMsgType[Terminated].actor must be(target)
probe.expectTerminated(target)
//#test-probe-watch
}

Expand Down
2 changes: 1 addition & 1 deletion akka-remote/src/test/scala/akka/remote/RemotingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class RemotingSpec extends AkkaSpec(RemotingSpec.cfg) with ImplicitSender with D
watch(child)
child ! PoisonPill
expectMsg("postStop")
expectMsgType[Terminated].actor must be === child
expectTerminated(child)
l ! ((Props[Echo1], "child"))
val child2 = expectMsgType[ActorRef]
child2 ! 45
Expand Down
21 changes: 21 additions & 0 deletions akka-testkit/src/main/java/akka/testkit/JavaTestKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package akka.testkit;

import akka.actor.Terminated;
import akka.japi.Option;
import scala.runtime.AbstractFunction0;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
Expand Down Expand Up @@ -456,10 +458,29 @@ public void expectNoMsg(FiniteDuration max) {
p.expectNoMsg(max);
}


/**
* Assert that the given ActorRef is Terminated within the specified time.
* Don't forget to 'watch' it first!
*/
public Terminated expectTerminated(Duration max, ActorRef target) {
return p.expectTerminated(target, max);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice with a default timeout version as well expectTerminated(ActorRef target) just like expectMsgEquals et.c.


/**
* Same as <code>expectTerminated(remaining(), target)</code>,
* but correctly treating the timeFactor.
* Don't forget to 'watch' it first!
*/
public Terminated expectTerminated(ActorRef target) {
return expectTerminated(Duration.Undefined(), target);
}

/**
* Same as <code>receiveN(n, remaining())</code>, but correctly treating the
* timeFactor.
*/

public Object[] receiveN(int n) {
return (Object[]) p.receiveN(n).toArray(Util.classTag(Object.class));
}
Expand Down
11 changes: 11 additions & 0 deletions akka-testkit/src/main/scala/akka/testkit/TestKit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ trait TestKitBase {
f(o)
}

/**
* Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef.
* Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.
*
* @return the received Terminated message
*/
def expectTerminated(target: ActorRef, max: Duration = Duration.Undefined): Terminated =
expectMsgPF(max, "Terminated " + target) {
case t @ Terminated(`target`) ⇒ t
}

/**
* Hybrid of expectMsgPF and receiveWhile: receive messages while the
* partial function matches and returns false. Use it to ignore certain
Expand Down
18 changes: 16 additions & 2 deletions akka-testkit/src/test/scala/akka/testkit/JavaTestKitSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JavaTestKitSpec extends AkkaSpec with DefaultTimeout {
new JavaTestKit(system) {
val sent = List(1, 2, 3, 4, 5)
for (m ← sent) { getRef() ! m }
val received = receiveN(sent.size, 5 seconds);
val received = receiveN(sent.size, 5 seconds)
sent.toSet must be(received.toSet)
}
}
Expand All @@ -28,11 +28,25 @@ class JavaTestKitSpec extends AkkaSpec with DefaultTimeout {
new JavaTestKit(system) {
val sent = List(1, 2, 3)
for (m ← sent) { getRef() ! m }
val received = receiveN(sent.size);
val received = receiveN(sent.size)
sent.toSet must be(received.toSet)
}
}

"be able to expectTerminated" in {
new JavaTestKit(system) {
val actor = system.actorOf(Props(new Actor { def receive = { case _ ⇒ } }))

system stop actor

watch(actor)
expectTerminated(actor).existenceConfirmed must be === true

watch(actor)
expectTerminated(5 seconds, actor).actor must be === actor
}
}

}

}