From 99ec4a90dc85891a0d4d8c10b2377f401727b23f Mon Sep 17 00:00:00 2001 From: hmmini Date: Mon, 1 Jul 2024 23:22:55 +0900 Subject: [PATCH] =?UTF-8?q?[Test]=20=EB=82=B4=EC=9E=A5=20Redis=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20test=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EB=A1=9C=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit issue: 125 --- build.gradle | 1 + .../commons/config/EmbeddedRedisConfig.java | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/com/mini/joymall/commons/config/EmbeddedRedisConfig.java diff --git a/build.gradle b/build.gradle index 8981491..82d91a8 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'mysql:mysql-connector-java:8.0.33' implementation 'org.redisson:redisson-spring-boot-starter:3.31.0' + implementation 'com.github.codemonstur:embedded-redis:1.4.3' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' diff --git a/src/main/java/com/mini/joymall/commons/config/EmbeddedRedisConfig.java b/src/main/java/com/mini/joymall/commons/config/EmbeddedRedisConfig.java new file mode 100644 index 0000000..de1114a --- /dev/null +++ b/src/main/java/com/mini/joymall/commons/config/EmbeddedRedisConfig.java @@ -0,0 +1,74 @@ +package com.mini.joymall.commons.config; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.util.StringUtils; +import redis.embedded.RedisServer; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +@Profile("test") +@Configuration +public class EmbeddedRedisConfig { + @Value("${spring.data.redis.port}") + private int redisPort; + + private RedisServer redisServer; + + @PostConstruct + public void redisServer() throws IOException { + int port = isRedisRunning()? findAvailablePort() : redisPort; + redisServer = new RedisServer(port); + redisServer.start(); + } + + @PreDestroy + public void stopRedis() throws IOException { + if (redisServer != null) { + redisServer.stop(); + } + } + + private boolean isRedisRunning() throws IOException { + return isRunning(executeGrepProcessCommand(redisPort)); + } + + public int findAvailablePort() throws IOException { + + for (int port = 10000; port <= 65535; port++) { + Process process = executeGrepProcessCommand(port); + if (!isRunning(process)) { + return port; + } + } + + throw new IllegalArgumentException("Not Found Available port: 10000 ~ 65535"); + } + + private Process executeGrepProcessCommand(int port) throws IOException { + String command = String.format("netstat -nat | grep LISTEN|grep %d", port); + String[] shell = {"/bin/sh", "-c", command}; + return Runtime.getRuntime().exec(shell); + } + + private boolean isRunning(Process process) { + String line; + StringBuilder pidInfo = new StringBuilder(); + + try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + + while ((line = input.readLine()) != null) { + pidInfo.append(line); + } + + } catch (Exception e) { + } + + return !StringUtils.hasText(pidInfo.toString()); + } +}