25 lines
1.1 KiB
Java
25 lines
1.1 KiB
Java
|
|
package com.njcn.middle.cache.integration;
|
||
|
|
|
||
|
|
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
||
|
|
import org.junit.jupiter.api.extension.ExecutionCondition;
|
||
|
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
||
|
|
|
||
|
|
import java.net.InetSocketAddress;
|
||
|
|
import java.net.Socket;
|
||
|
|
|
||
|
|
/** Redis 不可达时禁用整个测试类(skipped,非 failed),保证无 Redis 环境 mvn test 仍全绿。 */
|
||
|
|
public class RedisAvailableCondition implements ExecutionCondition {
|
||
|
|
@Override
|
||
|
|
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||
|
|
String host = System.getProperty("spring.redis.host", "127.0.0.1");
|
||
|
|
int port = Integer.parseInt(System.getProperty("spring.redis.port", "6379"));
|
||
|
|
try (Socket s = new Socket()) {
|
||
|
|
s.connect(new InetSocketAddress(host, port), 500);
|
||
|
|
return ConditionEvaluationResult.enabled("redis reachable at " + host + ":" + port);
|
||
|
|
} catch (Exception e) {
|
||
|
|
return ConditionEvaluationResult.disabled(
|
||
|
|
"no redis at " + host + ":" + port + ", skip integration test");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|