test #426
@@ -1,36 +1,42 @@
|
||||
package kr.co.vividnext.sodalive.support
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues
|
||||
import org.springframework.context.ApplicationContextInitializer
|
||||
import org.springframework.context.ConfigurableApplicationContext
|
||||
import redis.embedded.RedisServer
|
||||
import redis.embedded.core.PortProvider
|
||||
|
||||
class EmbeddedRedisInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
companion object {
|
||||
const val PORT = 16379
|
||||
}
|
||||
|
||||
override fun initialize(applicationContext: ConfigurableApplicationContext) {
|
||||
EmbeddedRedisHolder.start()
|
||||
val port = EmbeddedRedisHolder.start()
|
||||
TestPropertyValues.of(
|
||||
"spring.redis.host=127.0.0.1",
|
||||
"spring.redis.port=$port",
|
||||
"spring.redis.ssl-enabled=false"
|
||||
).applyTo(applicationContext.environment)
|
||||
}
|
||||
}
|
||||
|
||||
private object EmbeddedRedisHolder {
|
||||
private var redisServer: RedisServer? = null
|
||||
private var port: Int? = null
|
||||
private var shutdownHookRegistered = false
|
||||
|
||||
@Synchronized
|
||||
fun start() {
|
||||
if (redisServer != null) {
|
||||
return
|
||||
fun start(): Int {
|
||||
port?.let {
|
||||
return it
|
||||
}
|
||||
|
||||
val selectedPort = PortProvider.newEphemeralPortProvider().get()
|
||||
redisServer = RedisServer.newRedisServer()
|
||||
.port(EmbeddedRedisInitializer.PORT)
|
||||
.port(selectedPort)
|
||||
.setting("bind 127.0.0.1")
|
||||
.setting("daemonize no")
|
||||
.setting("appendonly no")
|
||||
.build()
|
||||
.also { it.start() }
|
||||
port = selectedPort
|
||||
|
||||
if (!shutdownHookRegistered) {
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
@@ -40,11 +46,14 @@ private object EmbeddedRedisHolder {
|
||||
)
|
||||
shutdownHookRegistered = true
|
||||
}
|
||||
|
||||
return selectedPort
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stop() {
|
||||
redisServer?.stop()
|
||||
redisServer = null
|
||||
port = null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory
|
||||
@@ -15,8 +16,11 @@ import org.springframework.data.redis.listener.RedisMessageListenerContainer
|
||||
@TestConfiguration
|
||||
class EmbeddedRedisTestConfiguration {
|
||||
@Bean
|
||||
fun redisConnectionFactory(): RedisConnectionFactory {
|
||||
return LettuceConnectionFactory(RedisStandaloneConfiguration("127.0.0.1", EmbeddedRedisInitializer.PORT))
|
||||
fun redisConnectionFactory(
|
||||
@Value("\${spring.redis.host}") host: String,
|
||||
@Value("\${spring.redis.port}") port: Int
|
||||
): RedisConnectionFactory {
|
||||
return LettuceConnectionFactory(RedisStandaloneConfiguration(host, port))
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user