我是 Akka 和 Scala 的新手,我来自一个非并发的世界。可能我做错了很多事情,即使与问题无关,我也会感谢反馈。
我正在用 Akka 和 Scala 做一个简单的聊天应用程序。我从“输入功能”开始(bc 业务需求)......这是whatsapp 或tellegram 中的典型功能“约翰正在输入消息”。
我使用两种演员类型对其进行了建模:Talkers 和 Conversation,并且我想对我的 Conversation 演员进行单元测试。我的对话演员如下所示:
object Conversation {
def props(conversationId: UUID, talkers: List[ActorRef])(out: ActorRef) = Props(new Conversation(conversationId, talkers))
case class Typing(talkerId: TalkerId)
}
class Conversation(conversationId: UUID, talkers: List[ActorRef]) extends Actor with ActorLogging {
def receive = LoggingReceive {
case Typing(talkerId) =>
// notify all talkers that a talker is typing
// @TODO don't notify user which is typing
talkers foreach {talker: ActorRef => talker ! InterlocutorTyping(talkerId)}
}
}
我想,到现在很简单。因此,在开始使用 Scala 和 Akka 进行编码之前,我进行了如下测试:
- 我得到了我的对话演员
- 我嘲笑谈话者
- 我向我的演员发送消息打字
- 我希望应该通知谈话者
我真的不知道这是否是 Scala 和 Akka 中的正确方法。我的测试(使用 scalatest)如下所示:
"Conversation" should {
"Notify interlocutors when a talker is typing" in {
val talkerRef1 = system.actorOf(Props())
val talkerRef2 = system.actorOf(Props())
val talkerRef1Id = TalkerIdStub.random
val conversationId = UUID.randomUUID()
val conversationRef = system.actorOf(Props(classOf[Conversation], conversationId, List(talkerRef1, talkerRef2)))
// should I use TestActorRef ?
conversationRef ! InterlocutorTyping(talkerRef1Id)
// assert that talker2 is notified when talker1 is typing
}
}
我应该使用 TestActorRef 吗?我应该使用 TestProbe() 吗(我读到这是用于集成测试)
如何创建 Talker 模拟?这种方法正确吗?
将谈话者列表注入我的谈话演员是正确的吗?
我搜索了文档,但我认为有很多太旧了,我不确定代码示例是否仍然有效。
谢谢你们的时间,对这个菜鸟问题感到抱歉:=)