issue#103 Close DB to clean up logs

This commit is contained in:
Gergely Hegedus 2022-07-20 15:16:06 +03:00
parent 732aa05a87
commit d057c357a3

View file

@ -1,10 +1,12 @@
package org.fnives.test.showcase.testutils package org.fnives.test.showcase.testutils
import android.util.Log
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import org.fnives.test.showcase.BuildConfig import org.fnives.test.showcase.BuildConfig
import org.fnives.test.showcase.TestShowcaseApplication import org.fnives.test.showcase.TestShowcaseApplication
import org.fnives.test.showcase.di.createAppModules import org.fnives.test.showcase.di.createAppModules
import org.fnives.test.showcase.model.network.BaseUrl import org.fnives.test.showcase.model.network.BaseUrl
import org.fnives.test.showcase.storage.LocalDatabase
import org.junit.rules.TestRule import org.junit.rules.TestRule
import org.junit.runner.Description import org.junit.runner.Description
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
@ -14,6 +16,7 @@ import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin import org.koin.core.context.stopKoin
import org.koin.mp.KoinPlatformTools import org.koin.mp.KoinPlatformTools
import org.koin.test.KoinTest import org.koin.test.KoinTest
import org.koin.test.get
/** /**
* Test rule to help reinitialize the whole Koin setup. * Test rule to help reinitialize the whole Koin setup.
@ -23,16 +26,17 @@ import org.koin.test.KoinTest
* *
* Note: Do not use if you want your test's to share Koin, and in such case do not stop your Koin. * Note: Do not use if you want your test's to share Koin, and in such case do not stop your Koin.
*/ */
class ReloadKoinModulesIfNecessaryTestRule : TestRule, KoinTest { class ReloadKoinModulesIfNecessaryTestRule : TestRule {
override fun apply(base: Statement, description: Description): Statement = override fun apply(base: Statement, description: Description): Statement =
ReinitKoinStatement(base) ReinitKoinStatement(base)
class ReinitKoinStatement(private val base: Statement) : Statement() { class ReinitKoinStatement(private val base: Statement) : Statement(), KoinTest {
override fun evaluate() { override fun evaluate() {
reinitKoinIfNeeded() reinitKoinIfNeeded()
try { try {
base.evaluate() base.evaluate()
} finally { } finally {
closeDB()
stopKoin() stopKoin()
} }
} }
@ -48,5 +52,13 @@ class ReloadKoinModulesIfNecessaryTestRule : TestRule, KoinTest {
modules(createAppModules(baseUrl)) modules(createAppModules(baseUrl))
} }
} }
private fun closeDB() {
try {
get<LocalDatabase>().close()
} catch (ignored: Throwable) {
Log.d("ReloadKoinModulesRule", "Could not close db: $ignored, stacktrace: ${ignored.stackTraceToString()}")
}
}
} }
} }