Skip to content

Commit fcc8c17

Browse files
Cache HOCON config to avoid re-parsing on every test invocation
The config was being parsed ~170 times (once per testApplication call). Use a lazy singleton to parse it once and reuse across all tests. Co-Authored-By: doris.t.lam <dlamoris@gmail.com>
1 parent 408cc33 commit fcc8c17

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

src/test/kotlin/org/openmbee/flexo/mms/util/Environment.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@ package org.openmbee.flexo.mms.util
33
import com.typesafe.config.ConfigFactory
44
import io.ktor.server.config.*
55
import io.ktor.server.testing.*
6-
import java.io.InputStreamReader
6+
7+
private val cachedConfig: HoconApplicationConfig by lazy {
8+
HoconApplicationConfig(ConfigFactory.parseResources("application.conf.test").resolve())
9+
}
710

811
/**
912
* Load test environment from application.conf.test resource
1013
*/
11-
fun testEnv(): ApplicationConfig {
12-
return Thread.currentThread().contextClassLoader.getResourceAsStream("application.conf.test")?.let { stream ->
13-
InputStreamReader(stream).use { reader ->
14-
HoconApplicationConfig(ConfigFactory.parseReader(reader).resolve())
15-
}
16-
} ?: throw IllegalStateException("application.conf.test not found")
17-
}
14+
fun testEnv(): ApplicationConfig = cachedConfig
1815

1916
/**
2017
* Wrapper around Ktor's testApplication that automatically loads the
2118
* application.conf.test configuration file. In Ktor 3, testApplication
2219
* no longer auto-loads modules from config files.
2320
*/
2421
fun testApplication(block: suspend ApplicationTestBuilder.() -> Unit) {
25-
val hoconConfig = HoconApplicationConfig(
26-
ConfigFactory.parseResources("application.conf.test").resolve()
27-
)
2822
io.ktor.server.testing.testApplication {
2923
environment {
30-
config = hoconConfig
24+
config = cachedConfig
3125
}
3226
block()
3327
}

0 commit comments

Comments
 (0)