Scala Play Slick RejectedExecutionException on ScalaTest runs

与世无争的帅哥 提交于 2019-12-10 21:10:37

问题


My FlatSpec tests are throwing:

 java.util.concurrent.RejectedExecutionException: Task slick.backend.DatabaseComponent$DatabaseDef$$anon$2@dda460e rejected from java.util.concurrent.ThreadPoolExecutor@4f489ebd[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]

But only when I run more than one suite, on the second suite forward; it seems there's something that isn't reset between tests. I'm using OneAppPerSuite to provide the app context. Whenever I use OneAppPerTest, it fails again after the first test/Suite.

I have a override def beforeEach = tables.foreach ( _.truncate ) set up to clear the tables, where truncate just deletes all from a table: Await.result (db.run (q.delete), Timeout.Inf)

I have the following setup for my DAO layer:

SomeMappedDaoClass extends SomeCrudBase with HasDatabaseConfig

where

trait SomeCrudBase { self: HasDatabaseConfig => 
  override lazy val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
  implicit lazy val context = Akka.system.dispatchers.lookup("db-context")
}

And in application.conf

db-context {
  fork-join-executor {
    parallelism-factor = 5
    parallelism-max = 100
  }
}

I was refactoring the code to move away from Play's Guice DI. Before, when it had @Inject() (val dbConfigProvider: DatabaseConfigProvider) and extended HasDatabaseConfigProvider instead on the DAO classes, everything worked perfectly. Now it doesn't, and I don't know why.

Thank you in advance!


回答1:


Just out of interest is SomeMappedDaoClass an object? (I know it says class but...).

When testing the Play framework I have run into this kind of issue when dealing with objects that setup connections to parts of the Play Framework.

Between tests and between test files the Play app is killed and restarted, however, the objects created persist (because they are objects, they are initialised once within a JVM context--I think).

This can result in an object with a connection (be it for slick, an actor, anything...) that is referencing the first instance of the app used in a test. When the app is terminated and a new test starts a new app, that connection is now pointing to nothing.




回答2:


I came across the same issue and in my case, the above answers did not work out. My Solution -

implicit val app = new FakeApplication(additionalConfiguration = inMemoryDatabase())
Play.start(app)

Add above code in your first test case and don't add Play.stop(app). As all the test cases are already refering the first application, it should not be terminated. This worked for me.



来源:https://stackoverflow.com/questions/33223303/scala-play-slick-rejectedexecutionexception-on-scalatest-runs

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