Search in sources :

Example 1 with MockConnection

use of com.ctrip.platform.dal.dao.base.MockConnection 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();
}
Also used : MockDefaultHostConnection(com.ctrip.platform.dal.dao.base.MockDefaultHostConnection) HashMap(java.util.HashMap) Connection(java.sql.Connection) MockDefaultHostConnection(com.ctrip.platform.dal.dao.base.MockDefaultHostConnection) MockConnection(com.ctrip.platform.dal.dao.base.MockConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) MockResultSet(com.ctrip.platform.dal.dao.base.MockResultSet) ResultSet(java.sql.ResultSet) MockResultSet(com.ctrip.platform.dal.dao.base.MockResultSet) MockConnection(com.ctrip.platform.dal.dao.base.MockConnection) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

MockConnection (com.ctrip.platform.dal.dao.base.MockConnection)1 MockDefaultHostConnection (com.ctrip.platform.dal.dao.base.MockDefaultHostConnection)1 MockResultSet (com.ctrip.platform.dal.dao.base.MockResultSet)1 DefaultHostConnection (com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1