package wow.doge.mygame

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.scaladsl.ActorContext
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.BeforeAndAfterAll
import akka.actor.typed.ActorSystem
import scala.concurrent.duration._
import akka.actor.typed.ActorRef
import akka.actor.typed.scaladsl.AskPattern._
import akka.util.Timeout
import scala.concurrent.Await

class ActorTimeoutTest extends AnyFunSuite with BeforeAndAfterAll {
  import ActorTimeoutTest._
  implicit val as = ActorSystem(new MyActor.Props().create, "system")
  implicit val timeout = Timeout(1.millis)

  test("timeoutTest") {
    val fut = as.ask(MyActor.GetInt)
    val res = Await.result(fut, 1.second)
    assert(res == 1)
  }

  override protected def afterAll(): Unit =
    as.terminate()

}

object ActorTimeoutTest {
  object MyActor {
    sealed trait Command
    case class GetInt(replyTo: ActorRef[Int]) extends Command

    class Props() {
      def create =
        Behaviors.setup[Command] { ctx =>
          new MyActor(ctx, this).receive
        }
    }
  }
  class MyActor(
      ctx: ActorContext[MyActor.Command],
      props: MyActor.Props
  ) {
    import MyActor._
    def receive =
      Behaviors.receiveMessage[Command] {
        case GetInt(replyTo) =>
          // Thread.sleep(1000)
          replyTo ! 1
          Behaviors.same
      }
  }

}
