Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actor 间的异步依赖如何单元测试 #15

Open
zhongl opened this issue Aug 30, 2013 · 0 comments
Open

Actor 间的异步依赖如何单元测试 #15

zhongl opened this issue Aug 30, 2013 · 0 comments

Comments

@zhongl
Copy link
Member

zhongl commented Aug 30, 2013

akka 提供了 TestActorRef 帮助我们同步测试 Actor.

def log(m: => String) = println(s"${Thread.currentThread} - $m") 

def logReceived(m: => Any)(implicit self: ActorRef) = log(s"$self received: $m")

class A extends Actor {
  def receive = {
    case x => logReceived(x)
  }
}

class B extends Actor {
  private val a = context.actorOf(Props(new A))

  def receive = {
    case x => logReceived(x); a ! x
  }
}

val a = TestActorRef(new A, "a")

log("Send a message to a")

a ! "message"

val b = TestActorRef(new B, "b")

log("Send a message to b")

b ! "message"

但运行完上面的完整代码, 打印输出结果会告诉你 :

  1. a发消息时, 收到消息的打印是同步执行的
  2. 当向b发消息时, 但a收到消息的打印却是异步的

实际开发中, 总会有如BA的依赖, 那么针对B的单元测试怎么做好呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant