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

前端 未结 1 1727
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 15:52

I have a controller that asks an actor before answering and two test cases:

  • When I run play test the second test fails
  • When
相关标签:
1条回答
  • 2020-12-31 16:21

    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.

    0 讨论(0)
提交回复
热议问题