使用 ScalaTest 测试时,为什么 Akka 会失败并显示“IllegalStateException:在终止或终止时无法创建子项”?

2024-02-19

这是我从 Akka 收到的错误:

[debug] Running TaskDef(com.suredbits.core.util.time.TimeUtilUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.address.AddressDAOSystemTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.policy.PolicyHolderDAOUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(rpc.ModesTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.policy.PolicyDAOSystemTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
java.lang.IllegalStateException: cannot create children while terminating or terminated
    at akka.actor.dungeon.Children$class.makeChild(Children.scala:199)
    at akka.actor.dungeon.Children$class.attachChild(Children.scala:41)
    at akka.actor.ActorCell.attachChild(ActorCell.scala:369)
    at akka.actor.ActorSystemImpl.systemActorOf(ActorSystem.scala:551)
    at akka.testkit.TestKitBase$class.$init$(TestKit.scala:125)
    at akka.testkit.TestKit.<init>(TestKit.scala:718)
    at com.suredbits.core.policy.PolicyDAOSystemTest.<init>(PolicyDAOSystemTest.scala:34)
    at com.suredbits.core.policy.PolicyDAOSystemTest.<init>(PolicyDAOSystemTest.scala:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:379)
    at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:641)
    at sbt.TestRunner.runTest$1(TestFramework.scala:84)
    at sbt.TestRunner.run(TestFramework.scala:94)
    at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:219)
    at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:219)
    at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:207)
    at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:219)
    at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:219)
    at sbt.TestFunction.apply(TestFramework.scala:224)
    at sbt.Tests$.sbt$Tests$$processRunnable$1(Tests.scala:211)
    at sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
    at sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
    at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
    at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
    at sbt.std.Transform$$anon$4.work(System.scala:64)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:244)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
[error] Could not run test com.suredbits.core.policy.PolicyDAOSystemTest: java.lang.IllegalStateException: cannot create children while terminating or terminated
[debug] Running TaskDef(com.suredbits.core.policy.PolicyDAOUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.CurrencyUnitsTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.AddressMonitorUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.blockexplorer.BlockCypherSvcUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.address.AddressDAOUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.protocol.BitcoinAddressTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])
[debug] Running TaskDef(com.suredbits.core.blockexplorer.BlockchainInfoSvcUnitTest, org.scalatest.tools.Framework$$anon$1@7e1522e, false, [SuiteSelector])

这是我尝试运行的测试用例:

class PolicyDAOSystemTest(actorSystemConfig: ActorSystemConfig) extends TestKit(actorSystemConfig.actorSystem) with ImplicitSender with WordSpecLike
  with MustMatchers with BeforeAndAfter with ScalaFutures with PolicyDAOComponent with PolicyHolderDAOComponent with DbConfigTest with ActorSystemConfig {
  import actorSystemConfig.actorSystem._

  private val dbManagement = new DbManagement with DbConfigTest

  // 1 month
  private val policyDuration = new RichDuration(new Duration(1000 * 60 * 60 * 24 * 30))
  private val policyCreation = new RichDateTime(new DateTime)



  def this() = this(ActorSystemConfig)
}

这是scala代码ActorSystemConfig

trait ActorSystemConfig {
  val actorSystem: ActorSystem = ActorSystem("My-Actor-System")
  implicit val timeout = Timeout(2 seconds)
}


object ActorSystemConfig extends ActorSystemConfig

最后,这是我的源代码PolicyDAO.

    trait PolicyDAOComponent extends CRUDActorComponent { this: PolicyHolderDAOComponent with DbConfig with ActorSystemConfig =>
      import actorSystem._
      def policyDAOActor: ActorRef = actorSystem.actorOf(Props(new PolicyDAO))
      class PolicyDAO extends CRUDActor[Policy, Long] { 
    ... 
      } 
    }

我认为我遇到了共享问题ActorSystemConfig跨多个 scala 测试用例。例如,ActorSystemConfig共享于PolicyDAOSystemTest and PolicyHolderDAOSystemTest。我想这就是导致 Akka 抛出的原因IllegalStateException。这场冲突究竟发生在哪里?我每次打电话都应该接到一个新演员policyDAOActor因为它正在经历system.actorOf, 正确的?

感谢您的见解。


此异常通常是由于actorSystem.shutdown:

scala> val actorSystem: ActorSystem = ActorSystem("My-Actor-System")
actorSystem: akka.actor.ActorSystem = akka://My-Actor-System

scala> class Aaa extends Actor{ def receive = { case _ => }}
defined class Aaa

scala> actorSystem.actorOf(Props(new Aaa))
res178: akka.actor.ActorRef = Actor[akka://My-Actor-System/user/$a#-842131493]

scala> actorSystem.shutdown

scala> actorSystem.actorOf(Props(new Aaa))
java.lang.IllegalStateException: cannot create children while terminating or terminated
  at akka.actor.dungeon.Children$class.makeChild(Children.scala:200)
  at akka.actor.dungeon.Children$class.attachChild(Children.scala:40)
  at akka.actor.ActorCell.attachChild(ActorCell.scala:369)
  at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:554)
  ... 43 elided

由于默认情况下您在许多测试之间共享 ActorSystem 的一个实例 - 其中一个测试可能显式或隐式关闭(例如在内部)after甚至afterAll)。此外,您还在这里创建了两个同名的 actor 系统 - 一个来自(actorSystemConfig: ActorSystemConfig),第二个从 -with ActorSystemConfig (see PolicyDAOSystemTest)。默认情况下,第一个在应用程序中的所有测试之间共享。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 ScalaTest 测试时,为什么 Akka 会失败并显示“IllegalStateException:在终止或终止时无法创建子项”? 的相关文章

随机推荐