fix(#11498): fix CheckDbHealthTask. (#11500)

This commit is contained in:
blake.qiu 2023-12-28 11:37:29 +08:00 committed by GitHub
parent 6587029851
commit 526227c77b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -29,6 +29,7 @@ import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@ -281,7 +282,11 @@ public class ExternalDataSourceServiceImpl implements DataSourceService {
for (int i = 0; i < testJtList.size(); i++) {
JdbcTemplate jdbcTemplate = testJtList.get(i);
try {
jdbcTemplate.queryForMap(sql);
try {
jdbcTemplate.queryForMap(sql);
} catch (EmptyResultDataAccessException e) {
// do nothing.
}
isHealthList.set(i, Boolean.TRUE);
} catch (DataAccessException e) {
if (i == masterIndex) {

View File

@ -24,14 +24,18 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.support.TransactionTemplate;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@ -64,6 +68,9 @@ public class ExternalDataSourceServiceImplTest {
ReflectionTestUtils.setField(service, "tjt", tjt);
ReflectionTestUtils.setField(service, "testMasterJT", testMasterJT);
ReflectionTestUtils.setField(service, "testMasterWritableJT", testMasterWritableJT);
List<HikariDataSource> dataSourceList = new ArrayList<>();
dataSourceList.add(new HikariDataSource());
ReflectionTestUtils.setField(service, "dataSourceList", dataSourceList);
}
@Test
@ -107,4 +114,37 @@ public class ExternalDataSourceServiceImplTest {
Assert.assertTrue(isHealthList.get(0));
}
@Test
public void testCheckDbHealthTaskRunWhenEmptyResult() {
List<JdbcTemplate> testJtList = new ArrayList<>();
testJtList.add(jt);
ReflectionTestUtils.setField(service, "testJtList", testJtList);
List<Boolean> isHealthList = new ArrayList<>();
isHealthList.add(Boolean.FALSE);
ReflectionTestUtils.setField(service, "isHealthList", isHealthList);
when(jt.queryForMap(anyString())).thenThrow(new EmptyResultDataAccessException("Expected exception", 1));
service.new CheckDbHealthTask().run();
Assert.assertEquals(1, isHealthList.size());
Assert.assertTrue(isHealthList.get(0));
}
@Test
public void testCheckDbHealthTaskRunWhenSqlException() {
List<JdbcTemplate> testJtList = new ArrayList<>();
testJtList.add(jt);
ReflectionTestUtils.setField(service, "testJtList", testJtList);
List<Boolean> isHealthList = new ArrayList<>();
isHealthList.add(Boolean.FALSE);
ReflectionTestUtils.setField(service, "isHealthList", isHealthList);
when(jt.queryForMap(anyString())).thenThrow(
new UncategorizedSQLException("Expected exception", "", new SQLException()));
service.new CheckDbHealthTask().run();
Assert.assertEquals(1, isHealthList.size());
Assert.assertFalse(isHealthList.get(0));
}
}