Play ! 2.2.4 / Akka : tests failed when run together, but ok separately

吃可爱长大的小学妹 提交于 2019-11-30 09:08:46

The problem is that you are holding a reference to games manager actor ref in a val in an object. The first test that runs will initialise this object, which will get the currently running actor system and lookup the the actor. Then the actor system will be shutdown, and that actor ref will become invalid. But the application controller still holds the invalid ref in the val, so when the next test runs, it uses the invalid actor ref from the shut down actor system.

Here are the ways you can solve it:

  • Change gamesManagerRef to a def, use actorFor to look it up, and create it (using actorOf) in Global.onStart. So the actor is created once in Global.onStart, and the lookup is done every time it's used.
  • Make gamesManagerRef a @volatile var in some other object, and have your Global.onStart method create the actor and then assign it to that var. In this way, the lookup doesn't have to be done each time it's used, but it's kind of ugly because you have this global shared mutable state.
  • My preferred solution is to write a plugin (http://developer.vz.net/2012/03/16/writing-a-play-2-0-module/) that looks up the actor ref when the plugin starts, or puts it in a lazy val, and then have consumers access the actor ref through doing a plugin lookup.

Update 23/07/2018: The above suggestions are completely out of date. The recommended approaches are well documented in https://www.playframework.com/documentation/2.6.x/ScalaAkka.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!