Skip to content

Commit

Permalink
chore: Fix scala constructor finding tests for scala 3. (#1220)
Browse files Browse the repository at this point in the history
* chore: Fix scala constructor finding tests for scala 3.

* reorder imports
  • Loading branch information
He-Pin authored Mar 23, 2024
1 parent 33a845a commit c818001
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 155 deletions.
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

0 comments on commit c818001

Please sign in to comment.