Hi! I have a test problem. I'm not sure what chann...
# coroutines
h
Hi! I have a test problem. I'm not sure what channel is most relevant, but I think my problem is mainly coroutine related, so I'll try here. I'm running a Ktor application with Exposed, Flyway and Coroutines. And testcontainers to run a database in tests. After upgrading to Ktor 3.1.x there is a couple of tests that consistently fails in CI jobs, but succeeds locally. I suspect that I just do testing in a not indended way. Examples and questions in 🧵
Copy code
class TestExample {
    val database: Database = initTestContainer()
    
    @Test
    fun exampleTest() = runTest {
        val repository = Repository(db = database, context = backgroundScope.coroutineContext)
        val jobRunner = AppJobRunner(repository, context = testScheduler)
        
        backgroundScope.launch { jobRunner.startJobs() }
        testScheduler.runCurrent()
        assertEquals(0, repository.getObjectCount())
        testScheduler.advanceTimeBy(10000)
        testScheduler.runCurrent()
        assertEquals(10, repository.getObjectCount())
    }
}
Questions: 1. Is it ever possible to do this kind of test using an actual database as backend, or will the test result be inconsistent? 2. Should I use Dispatchers.IO in db connection in test as well? I assume so, but that does not help with my issue. 3. If using H2 as database, should I still use Dispatchers.IO? * Using H2 in my case is not that easy because of Flyway, which expect a certain database in the migration. If I cannot use an actual database in a test with coroutines, I will have to mock the repository class, and I think that makes the test less valuable
I am also unsure about Ktor testApplication tests. I this case the application itself will not run on test coroutines, but it is possible to inject test coroutines and use them for jobs that the app is starting. I suspect that these tests are working because the main app is not running on test coroutines. 🤔