Skip to content

Commit

Permalink
fix(alibaba#11498): fix CheckDbHealthTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo-Qiu committed Dec 12, 2023
1 parent 5880368 commit c4b60d6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -281,7 +282,11 @@ public void run() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
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;

Expand Down Expand Up @@ -64,6 +68,9 @@ public void setUp() {
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
Expand Down Expand Up @@ -107,4 +114,37 @@ public void testCheckDbHealthTaskRun() {
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 testCheckDbHealthTaskRunWhenUncategorizedSQLException() {
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));
}

}

0 comments on commit c4b60d6

Please sign in to comment.