-
Notifications
You must be signed in to change notification settings - Fork 148
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
additional mailbox selector for typed props #1096
Merged
pjfanning
merged 13 commits into
apache:main
from
Roiocam:additional-typed-props-mailbox-selector
Aug 28, 2024
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3c2d712
additional mailbox selector for typed props
Roiocam 7144f06
add unit test
Roiocam 1fb6045
chore change of unit test
Roiocam ffd4af0
Revert "configuration typo"
Roiocam 40ad4f0
fix pekko imports
Roiocam 8e3f603
mention interoperability in doc
Roiocam d20e39a
share configuration in tests
Roiocam b9b046d
revert configuration change
Roiocam 160072c
fix new typo
Roiocam 9c1022f
fix jdocs tests
Roiocam d7cc377
optimized import
Roiocam 3490fc6
mention api version in doc
Roiocam 3084433
resolve import issue
Roiocam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,10 @@ package org.apache.pekko.actor.typed.scaladsl | |
import com.typesafe.config.Config | ||
import com.typesafe.config.ConfigFactory | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
import org.apache.pekko | ||
import pekko.actor.typed.DispatcherSelector | ||
import pekko.actor.typed.Props | ||
import pekko.actor.typed.scaladsl.AskPattern._ | ||
import pekko.actor.BootstrapSetup | ||
import pekko.actor.setup.ActorSystemSetup | ||
import pekko.actor.testkit.typed.scaladsl.ActorTestKit | ||
|
@@ -28,7 +30,6 @@ import pekko.actor.testkit.typed.scaladsl.TestProbe | |
import pekko.actor.typed.ActorRef | ||
import pekko.actor.typed.ActorSystem | ||
import pekko.actor.typed.Behavior | ||
import pekko.actor.typed.Props | ||
import pekko.actor.typed.SpawnProtocol | ||
|
||
object DispatcherSelectorSpec { | ||
|
@@ -64,7 +65,7 @@ class DispatcherSelectorSpec(config: Config) | |
|
||
"DispatcherSelector" must { | ||
|
||
"select dispatcher from config" in { | ||
"select dispatcher from empty Props" in { | ||
val probe = createTestProbe[Pong]() | ||
val pingPong = spawn(PingPong(), Props.empty.withDispatcherFromConfig("ping-pong-dispatcher")) | ||
pingPong ! Ping(probe.ref) | ||
|
@@ -73,18 +74,27 @@ class DispatcherSelectorSpec(config: Config) | |
response.threadName should startWith("DispatcherSelectorSpec-ping-pong-dispatcher") | ||
} | ||
|
||
"select dispatcher from DispatcherSelector" in { | ||
val probe = createTestProbe[Pong]() | ||
val pingPong = spawn(PingPong(), DispatcherSelector.fromConfig("ping-pong-dispatcher")) | ||
pingPong ! Ping(probe.ref) | ||
|
||
val response = probe.receiveMessage() | ||
response.threadName should startWith("DispatcherSelectorSpec-ping-pong-dispatcher") | ||
} | ||
|
||
"detect unknown dispatcher from config" in { | ||
val probe = createTestProbe[Pong]() | ||
LoggingTestKit.error("Spawn failed").expect { | ||
val ref = spawn(PingPong(), Props.empty.withDispatcherFromConfig("unknown")) | ||
val ref = spawn(PingPong(), DispatcherSelector.fromConfig("unknown")) | ||
probe.expectTerminated(ref) | ||
} | ||
} | ||
|
||
"select same dispatcher as parent" in { | ||
val parent = spawn(SpawnProtocol(), Props.empty.withDispatcherFromConfig("ping-pong-dispatcher")) | ||
val parent = spawn(SpawnProtocol(), DispatcherSelector.fromConfig("ping-pong-dispatcher")) | ||
val childProbe = createTestProbe[ActorRef[Ping]]() | ||
parent ! SpawnProtocol.Spawn(PingPong(), "child", Props.empty.withDispatcherSameAsParent, childProbe.ref) | ||
parent ! SpawnProtocol.Spawn(PingPong(), "child", DispatcherSelector.sameAsParent(), childProbe.ref) | ||
|
||
val probe = createTestProbe[Pong]() | ||
val child = childProbe.receiveMessage() | ||
|
@@ -95,19 +105,13 @@ class DispatcherSelectorSpec(config: Config) | |
} | ||
|
||
"select same dispatcher as parent, several levels" in { | ||
val grandParent = spawn(SpawnProtocol(), Props.empty.withDispatcherFromConfig("ping-pong-dispatcher")) | ||
val parentProbe = createTestProbe[ActorRef[SpawnProtocol.Spawn[Ping]]]() | ||
grandParent ! SpawnProtocol.Spawn( | ||
SpawnProtocol(), | ||
"parent", | ||
Props.empty.withDispatcherSameAsParent, | ||
parentProbe.ref) | ||
|
||
val childProbe = createTestProbe[ActorRef[Ping]]() | ||
grandParent ! SpawnProtocol.Spawn(PingPong(), "child", Props.empty.withDispatcherSameAsParent, childProbe.ref) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix children not spawn from the parent |
||
val guardian = spawn(SpawnProtocol(), DispatcherSelector.fromConfig("ping-pong-dispatcher")) | ||
val parent: ActorRef[SpawnProtocol.Command] = guardian.ask((replyTo: ActorRef[ActorRef[SpawnProtocol.Command]]) => | ||
SpawnProtocol.Spawn(SpawnProtocol(), "parent", DispatcherSelector.sameAsParent(), replyTo)).futureValue | ||
val child: ActorRef[Ping] = parent.ask((reply: ActorRef[ActorRef[Ping]]) => | ||
SpawnProtocol.Spawn(PingPong(), "child", DispatcherSelector.sameAsParent(), reply)).futureValue | ||
|
||
val probe = createTestProbe[Pong]() | ||
val child = childProbe.receiveMessage() | ||
child ! Ping(probe.ref) | ||
|
||
val response = probe.receiveMessage() | ||
|
@@ -119,7 +123,7 @@ class DispatcherSelectorSpec(config: Config) | |
PingPong(), | ||
"DispatcherSelectorSpec2", | ||
ActorSystemSetup.create(BootstrapSetup()), | ||
Props.empty.withDispatcherSameAsParent) | ||
DispatcherSelector.sameAsParent()) | ||
try { | ||
val probe = TestProbe[Pong]()(sys) | ||
sys ! Ping(probe.ref) | ||
|
131 changes: 131 additions & 0 deletions
131
...yped-tests/src/test/scala/org/apache/pekko/actor/typed/scaladsl/MailboxSelectorSpec.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,131 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.pekko.actor.typed.scaladsl | ||
|
||
import com.typesafe.config.Config | ||
import com.typesafe.config.ConfigFactory | ||
import org.apache.pekko | ||
import pekko.actor.ActorCell | ||
import pekko.actor.testkit.typed.scaladsl.LogCapturing | ||
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
import pekko.actor.typed.ActorRef | ||
import pekko.actor.typed.Behavior | ||
import pekko.actor.typed.DispatcherSelector | ||
import pekko.actor.typed.MailboxSelector | ||
import pekko.actor.typed.Props | ||
import pekko.actor.typed.internal.adapter.ActorContextAdapter | ||
import pekko.actor.typed.scaladsl.AskPattern._ | ||
import pekko.dispatch.BoundedMessageQueueSemantics | ||
import pekko.dispatch.BoundedNodeMessageQueue | ||
import pekko.dispatch.Dispatchers | ||
import pekko.dispatch.MessageQueue | ||
import pekko.dispatch.NodeMessageQueue | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
object MailboxSelectorSpec { | ||
val config = ConfigFactory.parseString( | ||
""" | ||
specific-mailbox { | ||
mailbox-type = "org.apache.pekko.dispatch.NonBlockingBoundedMailbox" | ||
mailbox-capacity = 4 | ||
} | ||
""") | ||
|
||
object PingPong { | ||
case class Ping(replyTo: ActorRef[Pong]) | ||
|
||
case class Pong(threadName: String) | ||
|
||
def apply(): Behavior[Ping] = | ||
Behaviors.receiveMessage[Ping] { message => | ||
message.replyTo ! Pong(Thread.currentThread().getName) | ||
Behaviors.same | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
class MailboxSelectorSpec(config: Config) | ||
extends ScalaTestWithActorTestKit(config) | ||
with AnyWordSpecLike | ||
with LogCapturing { | ||
|
||
def this() = this(MailboxSelectorSpec.config) | ||
|
||
sealed trait Command | ||
case class WhatsYourMailbox(replyTo: ActorRef[MessageQueue]) extends Command | ||
case class WhatsYourDispatcher(replyTo: ActorRef[String]) extends Command | ||
|
||
private def extract[R](context: ActorContext[_], f: ActorCell => R): R = { | ||
context match { | ||
case adapter: ActorContextAdapter[_] => | ||
adapter.classicActorContext match { | ||
case cell: ActorCell => f(cell) | ||
case unexpected => throw new RuntimeException(s"Unexpected: $unexpected") | ||
} | ||
case unexpected => throw new RuntimeException(s"Unexpected: $unexpected") | ||
} | ||
} | ||
|
||
private def behavior: Behavior[Command] = | ||
Behaviors.setup { context => | ||
Behaviors.receiveMessage[Command] { | ||
case WhatsYourMailbox(replyTo) => | ||
replyTo ! extract(context, cell => cell.mailbox.messageQueue) | ||
Behaviors.same | ||
case WhatsYourDispatcher(replyTo) => | ||
replyTo ! extract(context, cell => cell.dispatcher.id) | ||
Behaviors.same | ||
} | ||
} | ||
|
||
"MailboxSelectorSpec" must { | ||
|
||
"default is unbounded" in { | ||
val actor = spawn(behavior) | ||
val mailbox = actor.ask(WhatsYourMailbox(_)).futureValue | ||
mailbox shouldBe a[NodeMessageQueue] | ||
} | ||
|
||
"select an specific mailbox from MailboxSelector " in { | ||
val actor = spawn(behavior, MailboxSelector.fromConfig("specific-mailbox")) | ||
val mailbox = actor.ask(WhatsYourMailbox(_)).futureValue | ||
mailbox shouldBe a[BoundedMessageQueueSemantics] | ||
mailbox.asInstanceOf[BoundedNodeMessageQueue].capacity should ===(4) | ||
} | ||
|
||
"select an specific mailbox from empty Props " in { | ||
val actor = spawn(behavior, Props.empty.withMailboxFromConfig("specific-mailbox")) | ||
val mailbox = actor.ask(WhatsYourMailbox(_)).futureValue | ||
mailbox shouldBe a[BoundedMessageQueueSemantics] | ||
mailbox.asInstanceOf[BoundedNodeMessageQueue].capacity should ===(4) | ||
} | ||
|
||
"select an specific mailbox from DispatcherSelector " in { | ||
val actor = spawn(behavior, DispatcherSelector.blocking().withMailboxFromConfig("specific-mailbox")) | ||
val mailbox = actor.ask(WhatsYourMailbox(_)).futureValue | ||
mailbox shouldBe a[BoundedMessageQueueSemantics] | ||
mailbox.asInstanceOf[BoundedNodeMessageQueue].capacity should ===(4) | ||
val dispatcher = actor.ask(WhatsYourDispatcher(_)).futureValue | ||
dispatcher shouldBe Dispatchers.DefaultBlockingDispatcherId | ||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you reorder the imports and put back the empty line above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the delay, could you review this again and contain this in release 1.1.0?