vivri
vivri

Reputation: 905

Scala Play Slick RejectedExecutionException on ScalaTest runs

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!

Upvotes: 3

Views: 933

Answers (2)

Sumit Paliwal
Sumit Paliwal

Reputation: 395

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.

Upvotes: 0

healsjnr
healsjnr

Reputation: 405

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.

Upvotes: 1

Related Questions