Add Actor.immutablePartial (closes #24015)
This commit is contained in:
parent
994d1d7e67
commit
14f4fe8928
2 changed files with 59 additions and 0 deletions
|
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* Copyright (C) 2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package akka.typed
|
||||
package scaladsl
|
||||
|
||||
import akka.typed.testkit.{ EffectfulActorContext, TestKitSettings }
|
||||
import akka.typed.testkit.scaladsl.TestProbe
|
||||
import org.junit.runner.RunWith
|
||||
import org.scalatest.junit.JUnitRunner
|
||||
import scala.concurrent.duration.DurationInt
|
||||
|
||||
@RunWith(classOf[JUnitRunner])
|
||||
final class ImmutablePartialSpec extends TypedSpec {
|
||||
|
||||
final object `An Actor.immutablePartial behavior (native)`
|
||||
extends Tests
|
||||
with NativeSystem
|
||||
|
||||
final object `An Actor.immutablePartial behavior (adapted)`
|
||||
extends Tests
|
||||
with AdaptedSystem
|
||||
|
||||
trait Tests extends StartSupport {
|
||||
|
||||
private implicit val testSettings = TestKitSettings(system)
|
||||
|
||||
override implicit def system: ActorSystem[TypedSpec.Command]
|
||||
|
||||
def `must correctly install the message handler`(): Unit = {
|
||||
val probe = TestProbe[Command]("probe")
|
||||
val behavior =
|
||||
Actor.immutablePartial[Command] {
|
||||
case (_, Command2) ⇒
|
||||
probe.ref ! Command2
|
||||
Actor.same
|
||||
}
|
||||
val context = new EffectfulActorContext("ctx", behavior, 42, null)
|
||||
|
||||
context.run(Command1)
|
||||
context.currentBehavior shouldBe behavior
|
||||
probe.expectNoMsg(100.milliseconds)
|
||||
|
||||
context.run(Command2)
|
||||
context.currentBehavior shouldBe behavior
|
||||
probe.expectMsg(Command2)
|
||||
}
|
||||
}
|
||||
|
||||
private sealed trait Command
|
||||
private case object Command1 extends Command
|
||||
private case object Command2 extends Command
|
||||
}
|
||||
|
|
@ -185,6 +185,12 @@ object Actor {
|
|||
new BehaviorImpl.ImmutableBehavior(onMessage, onSignal)
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an immutable actor behavior from a partial message handler which treats undefined messages as unhandled.
|
||||
*/
|
||||
def immutablePartial[T](onMessage: PartialFunction[(ActorContext[T], T), Behavior[T]]): Immutable[T] =
|
||||
Actor.immutable[T] { (ctx, t) ⇒ onMessage.applyOrElse((ctx, t), (_: (ActorContext[T], T)) ⇒ Actor.unhandled[T]) }
|
||||
|
||||
/**
|
||||
* Construct an actor behavior that can react to lifecycle signals only.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue