✅Test: Redis 连接池测试
This commit is contained in:
+21
-6
@@ -4,20 +4,29 @@ import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* Redis连接管理类
|
||||
* TODO: 实现Redis连接池的配置和管理
|
||||
* TODO: 添加连接状态检查和重连机制
|
||||
* TODO: 实现连接参数的配置化
|
||||
* DONE: 实现Redis连接池的配置和管理
|
||||
* DONE: 添加连接状态检查和重连机制
|
||||
* DONE: 实现连接参数的配置化
|
||||
*/
|
||||
public class RedisConnection {
|
||||
public class RedisPoolConnection {
|
||||
private static JedisPool jedisPool;
|
||||
|
||||
static {
|
||||
// TODO: 配置连接池参数
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(20);
|
||||
config.setMaxIdle(10);
|
||||
config.setMinIdle(5);
|
||||
config.setMaxWait(Duration.ofMillis(1000));
|
||||
config.setTestOnBorrow(true);
|
||||
config.setTestWhileIdle(true);
|
||||
config.setBlockWhenExhausted(true);
|
||||
// TODO: 设置最大连接数、最小空闲连接数等参数
|
||||
jedisPool = new JedisPool(config, "localhost", 6379);
|
||||
jedisPool = new JedisPool(config, "47.121.181.112", 6379, 2000, "dn293830");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,7 +35,7 @@ public class RedisConnection {
|
||||
*/
|
||||
public static Jedis getJedis() {
|
||||
// TODO: 返回连接池中的连接
|
||||
return null;
|
||||
return jedisPool.getResource();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +44,9 @@ public class RedisConnection {
|
||||
*/
|
||||
public static void closeJedis(Jedis jedis) {
|
||||
// TODO: 关闭连接
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,5 +55,8 @@ public class RedisConnection {
|
||||
*/
|
||||
public static void closePool() {
|
||||
// TODO: 关闭连接池
|
||||
if (jedisPool != null) {
|
||||
jedisPool.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.hezhaohui.redis;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class RedisPoolConnectionTest {
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void getJedis() {
|
||||
System.out.println("开始Redis连接测试...");
|
||||
Jedis jedis = RedisPoolConnection.getJedis();
|
||||
assertNotNull(jedis);
|
||||
System.out.println("Redis连接测试通过!");
|
||||
System.out.println("Redis服务器状态:" + jedis.ping());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void closeJedis() throws InterruptedException {
|
||||
// Thread.sleep(10000);
|
||||
System.out.println("开始关闭Redis连接...");
|
||||
Jedis jedis = RedisPoolConnection.getJedis();
|
||||
assertNotNull(jedis);
|
||||
RedisPoolConnection.closeJedis(jedis);
|
||||
System.out.println("Redis连接已关闭!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void closePool() throws InterruptedException {
|
||||
// Thread.sleep(10000);
|
||||
System.out.println("开始关闭Redis连接池...");
|
||||
RedisPoolConnection.closePool();
|
||||
System.out.println("Redis连接池已关闭!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user