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

chore: Fix scala constructor finding tests for scala 3. #1220

Merged
merged 2 commits into from
Mar 23, 2024
Merged
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
129 changes: 61 additions & 68 deletions docs/src/test/scala/docs/actor/ActorDocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package docs.actor

import org.apache.pekko.actor.Kill
import jdocs.actor.ImmutableMessage

import language.postfixOps
Expand All @@ -27,12 +26,16 @@ import pekko.event.Logging
//#imports1

import scala.concurrent.Future
import pekko.actor.{ ActorLogging, ActorRef, ActorSystem, PoisonPill, Terminated }
// #watch
// #identify
import pekko.actor.{ ActorIdentity, ActorLogging, ActorRef, ActorSystem, Identify, Kill, PoisonPill, Terminated }

// #watch
// #identify
import pekko.testkit._
import pekko.util._
import scala.concurrent.duration._
import scala.concurrent.Await
import pekko.Done
import pekko.actor.CoordinatedShutdown

//#my-actor
Expand All @@ -46,6 +49,51 @@ class MyActor extends Actor {
}
//#my-actor

// #import-context
class ContextActor extends Actor {
import context._
val myActor = actorOf(Props[MyActor](), name = "myactor")
def receive: Receive = {
case x => myActor ! x
}
}
// #import-context

// #watch
class WatchActor extends Actor {
val child = context.actorOf(Props.empty, "child")
context.watch(child) // <-- this is the only call needed for registration
var lastSender = context.system.deadLetters

def receive: Receive = {
case "kill" =>
context.stop(child)
lastSender = sender()
case Terminated(`child`) =>
lastSender ! "finished"
}
}
// #watch

// #identify
class Follower extends Actor {
val identifyId = 1
context.actorSelection("/user/another") ! Identify(identifyId)

def receive = {
case ActorIdentity(`identifyId`, Some(ref)) =>
context.watch(ref)
context.become(active(ref))
case ActorIdentity(`identifyId`, None) => context.stop(self)

}

def active(another: ActorRef): Actor.Receive = {
case Terminated(`another`) => context.stop(self)
}
}
// #identify

final case class DoIt(msg: ImmutableMessage)
final case class Message(s: String)

Expand Down Expand Up @@ -334,20 +382,8 @@ class ActorDocSpec extends PekkoSpec("""
""") {

"import context" in {
new AnyRef {
// #import-context
class FirstActor extends Actor {
import context._
val myActor = actorOf(Props[MyActor](), name = "myactor")
def receive = {
case x => myActor ! x
}
}
// #import-context

val first = system.actorOf(Props(classOf[FirstActor], this), name = "first")
system.stop(first)
}
val first = system.actorOf(Props(classOf[ContextActor]), name = "first")
system.stop(first)
}

"creating actor with system.actorOf" in {
Expand Down Expand Up @@ -580,29 +616,9 @@ class ActorDocSpec extends PekkoSpec("""
}

"using watch" in {
new AnyRef {
// #watch
import org.apache.pekko.actor.{ Actor, Props, Terminated }

class WatchActor extends Actor {
val child = context.actorOf(Props.empty, "child")
context.watch(child) // <-- this is the only call needed for registration
var lastSender = context.system.deadLetters

def receive = {
case "kill" =>
context.stop(child)
lastSender = sender()
case Terminated(`child`) =>
lastSender ! "finished"
}
}
// #watch

val victim = system.actorOf(Props(classOf[WatchActor], this))
victim.tell("kill", testActor)
expectMsg("finished")
}
val victim = system.actorOf(Props(classOf[WatchActor]))
victim.tell("kill", testActor)
expectMsg("finished")
}

"using Kill" in {
Expand Down Expand Up @@ -641,34 +657,11 @@ class ActorDocSpec extends PekkoSpec("""
}

"using Identify" in {
new AnyRef {
// #identify
import org.apache.pekko.actor.{ Actor, ActorIdentity, Identify, Props, Terminated }

class Follower extends Actor {
val identifyId = 1
context.actorSelection("/user/another") ! Identify(identifyId)

def receive = {
case ActorIdentity(`identifyId`, Some(ref)) =>
context.watch(ref)
context.become(active(ref))
case ActorIdentity(`identifyId`, None) => context.stop(self)

}

def active(another: ActorRef): Actor.Receive = {
case Terminated(`another`) => context.stop(self)
}
}
// #identify

val a = system.actorOf(Props.empty)
val b = system.actorOf(Props(classOf[Follower], this))
watch(b)
system.stop(a)
expectMsgType[pekko.actor.Terminated].actor should be(b)
}
val a = system.actorOf(Props.empty)
val b = system.actorOf(Props(classOf[Follower]))
watch(b)
system.stop(a)
expectMsgType[pekko.actor.Terminated].actor should be(b)
}

"using pattern gracefulStop" in {
Expand Down
49 changes: 27 additions & 22 deletions docs/src/test/scala/docs/actor/SchedulerDocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package docs.actor

import docs.actor.SchedulerDocSpec.TickActor

import language.postfixOps

//#imports1
Expand All @@ -25,6 +27,16 @@ import scala.concurrent.duration._

import pekko.testkit._

object SchedulerDocSpec {
// #schedule-recurring
class TickActor extends Actor {
def receive: Receive = {
case "tick" => // Do something
}
}
// #schedule-recurring
}

class SchedulerDocSpec extends PekkoSpec(Map("pekko.loglevel" -> "INFO")) {
"schedule a one-off task" in {
// #schedule-one-off-message
Expand All @@ -47,27 +59,20 @@ class SchedulerDocSpec extends PekkoSpec(Map("pekko.loglevel" -> "INFO")) {
}

"schedule a recurring task" in {
new AnyRef {
// #schedule-recurring
val Tick = "tick"
class TickActor extends Actor {
def receive = {
case Tick => // Do something
}
}
val tickActor = system.actorOf(Props(classOf[TickActor], this))
// Use system's dispatcher as ExecutionContext
import system.dispatcher

// This will schedule to send the Tick-message
// to the tickActor after 0ms repeating every 50ms
val cancellable =
system.scheduler.scheduleWithFixedDelay(Duration.Zero, 50.milliseconds, tickActor, Tick)

// This cancels further Ticks to be sent
cancellable.cancel()
// #schedule-recurring
system.stop(tickActor)
}
// #schedule-recurring

val tickActor = system.actorOf(Props(classOf[TickActor]))
// Use system's dispatcher as ExecutionContext
import system.dispatcher

// This will schedule to send the Tick-message
// to the tickActor after 0ms repeating every 50ms
val cancellable =
system.scheduler.scheduleWithFixedDelay(Duration.Zero, 50.milliseconds, tickActor, "tick")

// This cancels further Ticks to be sent
cancellable.cancel()
// #schedule-recurring
system.stop(tickActor)
}
}
Loading
Loading