Desktop version

Home arrow Computer Science arrow Building Applications with Scala

Source

Chat room Actor test

The ChatRoonActorSpec.scala file should look something like this:

class ChatRoomActorSpec extends PlaySpec {

class Actors extends TestKit(ActorSystem("test")) "ChatRoomActor" should {

"accept joins the chat rooms" in new Actors { val probe1 = new TestProbe(system) val probe2 = new TestProbe(system) val actorRef = TestActorRef[ChatRoomActor] (Props[ChatRoomActor])

val roomActor = actorRef.underlyingActor assert(roomActor.users.size == 0) probe1.send(actorRef, JoinChatRoom) probe2.send(actorRef, JoinChatRoom) awaitCond(roomActor.users.size == 2, 100 millis)

assert(roomActor.users.contains(probe1.ref)) assert(roomActor.users.contains(probe2.ref))

}

"get stats from the chat room" in new Actors { val probe1 = new TestProbe(system) val actorRef = TestActorRef[ChatRoomActor] (Props[ChatRoomActor])

val roomActor = actorRef.underlyingActor assert(roomActor.users.size == 0) probe1.send(actorRef, JoinChatRoom) awaitCond(roomActor.users.size == 1, 100 millis)

assert(roomActor.users.contains(probe1.ref)) probe1.send(actorRef, GetStats) receiveOne(2000 millis)

}

"and broadcast messages" in new Actors { val probe1 = new TestProbe(system) val probe2 = new TestProbe(system) val actorRef = TestActorRef[ChatRoomActor] (Props[ChatRoomActor])

val roomActor = actorRef.underlyingActor probe1.send(actorRef, JoinChatRoom)

probe2.send(actorRef, JoinChatRoom)

awaitCond(roomActor.users.size == 2, 100 millis)

val msg = ChatMessage("sender", "test message")

actorRef.receive(msg)

probe1.expectMsg(msg)

probe2.expectMsg(msg)

}

"and track users ref and counts" in new Actors { val probe1 = new TestProbe(system) val probe2 = new TestProbe(system) val actorRef = TestActorRef[ChatRoomActor]

(Props[ChatRoomActor])

val roomActor = actorRef.underlyingActor probe1.send(actorRef, JoinChatRoom) probe2.send(actorRef, JoinChatRoom) awaitCond(roomActor.users.size == 2, 100 millis)

probe2.ref ! PoisonPill

awaitCond(roomActor.users.size == 1, 100 millis)

}

}

}

So, here we have the same concepts as the other test. However, we have more usage of the Akka testkit DSL. For instance, we are using expectMsg on the probe to check if an Actor received a specific message. We are also using awaitCond to check the Actor's internal state in an assertion.

Now is the time to test the last Actor method.

Chat Bot Admin Actor test

ChatBotAdminActorSpec.scala file should look something like this:

class ChatBotAdminActorSpec extends TestKit(ActorSystem("test")) with ImplicitSender

with WordSpecLike with Matchers with BeforeAndAfterAll { "ChatBotAdminActor" should {

"be able to create Bot Admin in the Chat Room and Tick" in { val probe1 = new TestProbe(system)

val actorRef = TestActorRef[ChatBotAdminActor](Props(new ChatBotAdminActor(system))) val botActor = actorRef.underlyingActor assert(botActor.context != null) awaitCond(botActor.room != null )

}

}

}

For this test, we will check if the actor context was not null, and also if the room was created and the scheduler was also not null. All good to go.

Alright, that's it! This is the last actor test. Now we are completely done. You can run this test with $ activator test, or, if you prefer the activator, then use "test-only TESTCLASSNAME" -Dsbt.task.forcegc=false to run a specific test case.

Summary

In this chapter, you learned how to work with Akka actors and created a web chat using Akka, the Play framework, and WebSockets. Akka is a really powerful solution that can be used with or without the Play framework. Additionally, you learned about the Actor model, mailboxes, routing, persistence, Akka configuration, message patterns, and how to write code with actors in Scala and Play.

In the next chapter, you will learn more about REST, JSON, and how to model a REST API, as well as how to create a Scala client for your REST services.

9

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >

Related topics