Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pipeToSelf to typed ActorContext #26199
Implemented in terms of AdaptMessage, which makes sure to map the values on the actor's thread, in Scala.
- Loading branch information
1 parent
f8618b2
commit 1c370c1
Showing
7 changed files
with
192 additions
and
15 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextPipeToSelfTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.actor.typed.javadsl; | ||
|
||
import akka.actor.testkit.typed.javadsl.TestKitJunitResource; | ||
import akka.actor.testkit.typed.javadsl.TestProbe; | ||
import akka.actor.typed.Behavior; | ||
import akka.actor.typed.Props; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.scalatest.junit.JUnitSuite; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import static org.hamcrest.CoreMatchers.startsWith; | ||
import static org.junit.Assert.*; | ||
|
||
public final class ActorContextPipeToSelfTest extends JUnitSuite { | ||
|
||
@ClassRule | ||
public static final TestKitJunitResource testKit = | ||
new TestKitJunitResource(ConfigFactory.parseString( | ||
"pipe-to-self-spec-dispatcher.executor = thread-pool-executor\n" + | ||
"pipe-to-self-spec-dispatcher.type = PinnedDispatcher\n" | ||
)); | ||
|
||
static final class Msg { | ||
final String response; | ||
final String selfName; | ||
final String threadName; | ||
|
||
Msg(final String response, final String selfName, final String threadName) { | ||
this.response = response; | ||
this.selfName = selfName; | ||
this.threadName = threadName; | ||
} | ||
} | ||
|
||
@Test public void handlesSuccess() { | ||
assertEquals("ok: hi", responseFrom(CompletableFuture.completedFuture("hi"))); | ||
} | ||
|
||
@Test public void handlesFailure() { | ||
assertEquals("ko: boom", responseFrom(failedFuture(new RuntimeException("boom")))); | ||
} | ||
|
||
private CompletableFuture<String> failedFuture(final Throwable ex) { | ||
final CompletableFuture<String> future = new CompletableFuture<>(); | ||
future.completeExceptionally(ex); | ||
return future; | ||
} | ||
|
||
private String responseFrom(final CompletionStage<String> future) { | ||
final TestProbe<Msg> probe = testKit.createTestProbe(); | ||
final Behavior<Msg> behavior = Behaviors.setup(context -> { | ||
context.pipeToSelf(future, (string, exception) -> { | ||
final String response; | ||
if (string != null) response = String.format("ok: %s", string); | ||
else if (exception != null) response = String.format("ko: %s", exception.getMessage()); | ||
else response = "???"; | ||
return new Msg(response, context.getSelf().path().name(), Thread.currentThread().getName()); | ||
}); | ||
return Behaviors.receiveMessage(msg -> { | ||
probe.getRef().tell(msg); | ||
return Behaviors.stopped(); | ||
}); | ||
}); | ||
final String name = "pipe-to-self-spec"; | ||
final Props props = Props.empty().withDispatcherFromConfig("pipe-to-self-spec-dispatcher"); | ||
|
||
testKit.spawn(behavior, name, props); | ||
|
||
final Msg msg = probe.expectMessageClass(Msg.class); | ||
|
||
assertEquals("pipe-to-self-spec", msg.selfName); | ||
assertThat(msg.threadName, startsWith("ActorContextPipeToSelfTest-pipe-to-self-spec-dispatcher")); | ||
return msg.response; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...tor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextPipeToSelfSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.actor.typed.scaladsl | ||
|
||
import scala.concurrent.Future | ||
import scala.util.control.NoStackTrace | ||
import scala.util.{ Failure, Success } | ||
|
||
import akka.actor.testkit.typed.scaladsl.{ ScalaTestWithActorTestKit, TestProbe } | ||
import akka.actor.typed.Props | ||
import com.typesafe.config.ConfigFactory | ||
import org.scalatest.WordSpecLike | ||
|
||
object ActorContextPipeToSelfSpec { | ||
val config = ConfigFactory.parseString( | ||
""" | ||
|pipe-to-self-spec-dispatcher { | ||
| executor = thread-pool-executor | ||
| type = PinnedDispatcher | ||
|} | ||
""".stripMargin) | ||
} | ||
|
||
final class ActorContextPipeToSelfSpec extends ScalaTestWithActorTestKit(ActorContextPipeToSelfSpec.config) | ||
with WordSpecLike { | ||
|
||
"The Scala DSL ActorContext pipeToSelf" must { | ||
"handle success" in { responseFrom(Future.successful("hi")) should ===("ok: hi") } | ||
"handle failure" in { responseFrom(Future.failed(Fail)) should ===(s"ko: $Fail") } | ||
} | ||
|
||
object Fail extends NoStackTrace | ||
|
||
private def responseFrom(future: Future[String]) = { | ||
final case class Msg(response: String, selfName: String, threadName: String) | ||
|
||
val probe = TestProbe[Msg]() | ||
val behavior = Behaviors.setup[Msg] { context ⇒ | ||
context.pipeToSelf(future) { | ||
case Success(s) ⇒ Msg(s"ok: $s", context.self.path.name, Thread.currentThread().getName) | ||
case Failure(e) ⇒ Msg(s"ko: $e", context.self.path.name, Thread.currentThread().getName) | ||
} | ||
Behaviors.receiveMessage { msg ⇒ | ||
probe.ref ! msg | ||
Behaviors.stopped | ||
} | ||
} | ||
val name = "pipe-to-self-spec" | ||
val props = Props.empty.withDispatcherFromConfig("pipe-to-self-spec-dispatcher") | ||
|
||
spawn(behavior, name, props) | ||
|
||
val msg = probe.expectMessageType[Msg] | ||
|
||
msg.selfName should ===("pipe-to-self-spec") | ||
msg.threadName should startWith("ActorContextPipeToSelfSpec-pipe-to-self-spec-dispatcher") | ||
msg.response | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters