Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

3 years ago
  1. package wow.doge.mygame
  2. import akka.actor.typed.scaladsl.Behaviors
  3. import akka.actor.typed.scaladsl.ActorContext
  4. import org.scalatest.funsuite.AnyFunSuite
  5. import org.scalatest.BeforeAndAfterAll
  6. import akka.actor.typed.ActorSystem
  7. import scala.concurrent.duration._
  8. import akka.actor.typed.ActorRef
  9. import akka.actor.typed.scaladsl.AskPattern._
  10. import akka.util.Timeout
  11. import scala.concurrent.Await
  12. class ActorTimeoutTest extends AnyFunSuite with BeforeAndAfterAll {
  13. import ActorTimeoutTest._
  14. implicit val as = ActorSystem(new MyActor.Props().create, "system")
  15. implicit val timeout = Timeout(1.millis)
  16. test("timeoutTest") {
  17. val fut = as.ask(MyActor.GetInt(_))
  18. val res = Await.result(fut, 1.second)
  19. assert(res == 1)
  20. }
  21. override protected def afterAll(): Unit = {
  22. as.terminate()
  23. }
  24. }
  25. object ActorTimeoutTest {
  26. object MyActor {
  27. sealed trait Command
  28. case class GetInt(replyTo: ActorRef[Int]) extends Command
  29. class Props() {
  30. def create =
  31. Behaviors.setup[Command] { ctx =>
  32. new MyActor(ctx, this).receive
  33. }
  34. }
  35. }
  36. class MyActor(
  37. ctx: ActorContext[MyActor.Command],
  38. props: MyActor.Props
  39. ) {
  40. import MyActor._
  41. def receive =
  42. Behaviors.receiveMessage[Command] {
  43. case GetInt(replyTo) =>
  44. // Thread.sleep(1000)
  45. replyTo ! 1
  46. Behaviors.same
  47. }
  48. }
  49. }