use of com.ctrip.platform.dal.dao.base.MockResultSet in project dal by ctripcorp.
the class MajorityHostValidatorTest method doubleCheckOnlineStatusTest.
@Test
public void doubleCheckOnlineStatusTest() throws SQLException {
// currentMemberId is empty
MockMajorityHostValidator validator = new MockMajorityHostValidator(configuredHost, orderedHosts, failOverTime, blackListTimeOut, fixedValidatePeriod);
assertEquals(false, validator.doubleCheckOnlineStatus("", hostSpec1));
// host not in pre and black list
assertEquals(false, validator.doubleCheckOnlineStatus("1234", hostSpec1));
// hostSpec2 is need to remove from pre list
validator.mysqlServer.put(hostSpec2, MockMajorityHostValidator.MysqlStatus.unknown);
MockDefaultHostConnection mockDefaultHostConnection2 = new MockDefaultHostConnection(hostSpec2);
try {
validator.validateAndUpdate(mockDefaultHostConnection2, hostSpec2, 3);
} catch (Exception e) {
}
Connection mockConnectionHost1 = new MockConnection();
ResultSet resultSet1 = new MockResultSet();
HashMap<String, Object> host1Map = new HashMap<>();
host1Map.put("CURRENT_MEMBER_ID", "host1");
host1Map.put("MEMBER_STATE", "online");
host1Map.put("MEMBER_ID", "host2");
((MockResultSet) resultSet1).result.add(host1Map);
((MockStatement) ((MockConnection) mockConnectionHost1).statement).result.put("default", resultSet1);
Connection mockConnectionHost3 = new MockConnection();
ResultSet resultSet3 = new MockResultSet();
HashMap<String, Object> host3Map = new HashMap<>();
host3Map.put("CURRENT_MEMBER_ID", "host3");
host3Map.put("MEMBER_STATE", "online");
host3Map.put("MEMBER_ID", "host2");
((MockResultSet) resultSet3).result.add(host3Map);
((MockStatement) ((MockConnection) mockConnectionHost3).statement).result.put("default", resultSet3);
((MockResultSet) resultSet1).resetIndex();
((MockResultSet) resultSet3).resetIndex();
validator.connectionMap.put(hostSpec1, mockConnectionHost1);
validator.connectionMap.put(hostSpec3, mockConnectionHost3);
assertEquals(true, validator.doubleCheckOnlineStatus("host2", hostSpec2));
((MockResultSet) resultSet1).resetIndex();
((MockResultSet) resultSet3).resetIndex();
host1Map.put("MEMBER_STATE", "error");
host3Map.put("MEMBER_STATE", "error");
assertEquals(false, validator.doubleCheckOnlineStatus("host2", hostSpec2));
((MockResultSet) resultSet1).resetIndex();
((MockResultSet) resultSet3).resetIndex();
host1Map.put("MEMBER_STATE", "online");
assertEquals(true, validator.doubleCheckOnlineStatus("host2", hostSpec2));
((MockResultSet) resultSet1).resetIndex();
((MockResultSet) resultSet3).resetIndex();
host3Map.put("default_timeout", "5");
assertEquals(true, validator.doubleCheckOnlineStatus("host2", hostSpec2));
validator.destroy();
}
Aggregations