Skip to content

Commit

Permalink
[UPDATE] EmbeddedRedisCOnfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jhhong0509 committed May 2, 2021
1 parent 5c14520 commit 52a5f46
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
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 javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Configuration
@Profile("test")
public class EmbeddedRedisConfig {

private final RedisServer redisServer;
private RedisServer redisServer;

@Value("${spring.redis.port}")
int redisPort;

public EmbeddedRedisConfig() {
this.redisServer = RedisServer.builder()
.setting("maxheap 128M")
.build();
}

@PostConstruct
public void runRedis() {
public void runRedis() throws IOException{
int port = isRedisRunning() ? findAvailablePort() : redisPort;
redisServer = new RedisServer(port);
redisServer.start();
}

Expand All @@ -34,4 +34,41 @@ public void stopRedis() {
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 ignored) {
}

return !StringUtils.isEmpty(pidInfo.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dsmpear.main.user_backend_v2;

import com.dsmpear.main.user_backend_v2.config.EmbeddedRedisConfig;
import com.dsmpear.main.user_backend_v2.entity.user.UserRepository;
import com.dsmpear.main.user_backend_v2.entity.verifynumber.VerifyNumber;
import com.dsmpear.main.user_backend_v2.entity.verifynumber.VerifyNumberRepository;
import com.dsmpear.main.user_backend_v2.payload.request.EmailVerifyRequest;
Expand All @@ -14,9 +12,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spring.mail.password=""

spring.redis.host=localhost
spring.redis.database=0
spring.redis.port=6379
spring.redis.port=6380

spring.config.activate.on-profile=test

Expand Down

0 comments on commit 52a5f46

Please sign in to comment.