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