Skip to content

Commit

Permalink
fix: add try catch block on health check unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambert-Rao committed Apr 19, 2024
1 parent 985c710 commit 4ec7f6f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import com.alibaba.nacos.api.exception.NacosException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -38,35 +40,43 @@ class NacosConfigCheckTest {

@BeforeEach
public void init() {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.instanceId(1L)
.healthCheckResourceType("meta")
.healthCheckResourceSubType("nacos")
.clusterId(1L)
.connectUrl("127.0.0.1:8848")
.requestTimeoutMillis(1000L)
.build();
nacosCheck = new NacosConfigCheck(config);
try {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.instanceId(1L)
.healthCheckResourceType("meta")
.healthCheckResourceSubType("nacos")
.clusterId(1L)
.connectUrl("127.0.0.1:8848")
.requestTimeoutMillis(1000L)
.build();
nacosCheck = new NacosConfigCheck(config);
} catch (Exception e) {
log.error("NacosConfigCheck failed.", e);
}
}

@Test
public void testDoCheck() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Thread.sleep(100);
nacosCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
try {
CountDownLatch latch = new CountDownLatch(1);
Thread.sleep(100);
nacosCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await(2, TimeUnit.SECONDS);
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await(2, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("NacosConfigCheck failed.", e);
}
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,43 @@ class NacosNamingServiceCheckTest {

@BeforeEach
public void init() {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.instanceId(1L)
.healthCheckResourceType("meta")
.healthCheckResourceSubType("nacos")
.clusterId(1L)
.connectUrl("127.0.0.1:8848")
.requestTimeoutMillis(1000L)
.build();
nacosRegisterCheck = new NacosNamingServiceCheck(config);
try {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.instanceId(1L)
.healthCheckResourceType("meta")
.healthCheckResourceSubType("nacos")
.clusterId(1L)
.connectUrl("127.0.0.1:8848")
.requestTimeoutMillis(1000L)
.build();
nacosRegisterCheck = new NacosNamingServiceCheck(config);
} catch (Exception e) {
log.error("NacosNamingServiceCheck failed.", e);
}
}

@Test
public void testDoCheck() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Thread.sleep(100);
nacosRegisterCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
try {
CountDownLatch latch = new CountDownLatch(1);
Thread.sleep(100);
nacosRegisterCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e.getMessage());
}
});
latch.await(2, TimeUnit.SECONDS);
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e.getMessage());
}
});
latch.await(2, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("NacosNamingServiceCheck failed.", e);
}
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,38 @@ class Rocketmq4BrokerCheckTest {

@BeforeEach
public void init() {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.host("127.0.0.1")
.port(10911)
.requestTimeoutMillis(1000L)
.build();
rocketmqCheck = new Rocketmq4BrokerCheck(config);
try {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
.host("127.0.0.1")
.port(10911)
.requestTimeoutMillis(1000L)
.build();
rocketmqCheck = new Rocketmq4BrokerCheck(config);
} catch (Exception e) {
log.error("Rocketmq4BrokerCheck failed.", e);
}
}

@Test
public void testDoCheck() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
try {
CountDownLatch latch = new CountDownLatch(1);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
} catch (Exception e) {
log.error("Rocketmq4BrokerCheck failed.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,38 @@ class Rocketmq4NameserverCheckTest {

@BeforeEach
public void init() {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()
try {
HealthCheckObjectConfig config = HealthCheckObjectConfig.builder()

.connectUrl("127.0.0.1:9876")
.requestTimeoutMillis(1000L)
.build();
rocketmqCheck = new Rocketmq4NameServerCheck(config);
.connectUrl("127.0.0.1:9876")
.requestTimeoutMillis(1000L)
.build();
rocketmqCheck = new Rocketmq4NameServerCheck(config);
} catch (Exception e) {
log.error("Rocketmq4NameserverCheck failed.", e);
}
}

@Test
public void testDoCheck() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
try {
CountDownLatch latch = new CountDownLatch(1);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
} catch (Exception e) {
log.error("Rocketmq4NameserverCheck failed.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,50 @@ class Rocketmq4TopicCheckTest {

@BeforeEach
public void init() throws InterruptedException {
HealthCheckObjectConfig config = new HealthCheckObjectConfig();
config.getRocketmqConfig().setBrokerUrl("127.0.0.1:10911");
config.getRocketmqConfig().setNameServerUrl("127.0.0.1:9876");
config.setRequestTimeoutMillis(1000L);
rocketmqCheck = new Rocketmq4TopicCheck(config);
try {
HealthCheckObjectConfig config = new HealthCheckObjectConfig();
config.getRocketmqConfig().setBrokerUrl("127.0.0.1:10911");
config.getRocketmqConfig().setNameServerUrl("127.0.0.1:9876");
config.setRequestTimeoutMillis(1000L);
rocketmqCheck = new Rocketmq4TopicCheck(config);
} catch (Exception e) {
log.error("Rocketmq4TopicCheck failed.", e);
}
}

@Test
public void testDoCheck() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(2);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
try {
CountDownLatch latch = new CountDownLatch(2);
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
rocketmqCheck.doCheck(new HealthCheckCallback() {
@Override
public void onSuccess() {
latch.countDown();
log.info("{} success", this.getClass().getSimpleName());
}

@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
@Override
public void onFail(Exception e) {
latch.countDown();
log.error("{}, failed for reason {}", this.getClass().getSimpleName(), e);
}
});
latch.await();
} catch (Exception e) {
log.error("Rocketmq4TopicCheck failed.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Timeout(value = 5)
class NacosNamingSDKOperationTest {

private NacosNamingSDKOperation nacosNamingSDKOperation = new NacosNamingSDKOperation();

@Test
public void testCreateNamingService() throws NacosException, InterruptedException {
try {
CreateNacosConfig createClientConfig = new CreateNacosConfig();
createClientConfig.setServerAddress("127.0.0.1:8848");
NamingService namingService = (NamingService) SDKManager.getInstance().createClient(SDKTypeEnum.META_NACOS_NAMING, createClientConfig)
.getValue();
namingService.registerInstance("eventmesh-dashboard-sdk-nacos-test", "11.11.11.11", 8888, "eventmesh-dashboard-sdk-nacos-test-cluster-name");

namingService.deregisterInstance("eventmesh-dashboard-sdk-nacos-test", "11.11.11.11", 8888,
"eventmesh-dashboard-sdk-nacos-test-cluster-name");
"eventmesh-dashboard-sdk-nacos-test-cluster-name");}
catch (Exception e){
log.error("create nacos naming service failed", e);
}
}
}

0 comments on commit 4ec7f6f

Please sign in to comment.