Search in sources :

Example 26 with DatabaseSelector

use of com.ctrip.platform.dal.dao.configure.DatabaseSelector in project dal by ctripcorp.

the class DatabaseSelectorTest method hasMixedMasterAndSlaveButSlaveMarkdownTest.

@Test
public void hasMixedMasterAndSlaveButSlaveMarkdownTest() throws DalException {
    List<DataBase> ms = new ArrayList<DataBase>();
    ms.add(new DataBase(M1, true, "", M1));
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    autoMarkdown(S1);
    autoMarkdown(S2);
    DatabaseSelector selector = new DatabaseSelector(null, ms, ss, false, true);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, false, true);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(new DalHints().inDatabase(S1), ms, ss, false, true);
    assertSelector(selector, ErrorCode.MarkdownConnection);
    // masterOnly
    selector = new DatabaseSelector(null, ms, ss, true, true);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, true, true);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(null, ms, ss, true, false);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, true, false);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(null, ms, ss, false, false);
    Assert.assertEquals(M1, selector.select());
    selector = new DatabaseSelector(new DalHints().inDatabase(M1), ms, ss, false, false);
    Assert.assertEquals(M1, selector.select());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DatabaseSelector(com.ctrip.platform.dal.dao.configure.DatabaseSelector) ArrayList(java.util.ArrayList) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 27 with DatabaseSelector

use of com.ctrip.platform.dal.dao.configure.DatabaseSelector in project dal by ctripcorp.

the class DatabaseSelectorTest method invalidDesigantedDbTest.

@Test
public void invalidDesigantedDbTest() throws DalException {
    List<DataBase> ms = new ArrayList<DataBase>();
    ms.add(new DataBase(M1, true, "", M1));
    ms.add(new DataBase(M2, true, "", M2));
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    ss.add(new DataBase(S3, false, "", S3));
    DatabaseSelector selector;
    selector = new DatabaseSelector(new DalHints().inDatabase(M1 + M2), ms, ss, false, false);
    assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
    selector = new DatabaseSelector(new DalHints().inDatabase(M1 + M2), ms, ss, true, true);
    assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
    selector = new DatabaseSelector(new DalHints().inDatabase(M1 + M2), ms, ss, true, false);
    assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
    selector = new DatabaseSelector(new DalHints().inDatabase(M1 + M2), ms, ss, false, true);
    assertSelector(selector, ErrorCode.InvalidDatabaseKeyName);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DatabaseSelector(com.ctrip.platform.dal.dao.configure.DatabaseSelector) ArrayList(java.util.ArrayList) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 28 with DatabaseSelector

use of com.ctrip.platform.dal.dao.configure.DatabaseSelector in project dal by ctripcorp.

the class DatabaseSelectorTest method masterOnlyHaTest.

@Test
public void masterOnlyHaTest() throws DalException {
    List<DataBase> ms = new ArrayList<DataBase>();
    ms.add(new DataBase(M1, true, "", M1));
    ms.add(new DataBase(M2, true, "", M2));
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    ss.add(new DataBase(S3, false, "", S3));
    DatabaseSelector selector;
    // make sure no slave is qualified
    autoMarkdown(S1);
    autoMarkdown(S2);
    autoMarkdown(S3);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
    assertSelector(selector, M2);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
    assertSelector(selector, M1);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, false);
    assertSelector(selector, M2);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, false);
    assertSelector(selector, M1);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1).addDB(M2)), ms, ss, true, false);
    assertSelector(selector, ErrorCode.NoMoreConnectionToFailOver);
    autoMarkdown(M1);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
    assertSelector(selector, M2);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
    assertSelector(selector, M2);
    autoMarkup(M1);
    autoMarkdown(M2);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
    assertSelector(selector, M1);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
    assertSelector(selector, M1);
    autoMarkdown(M1);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
    assertSelector(selector, ErrorCode.NoMoreConnectionToFailOver);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
    assertSelector(selector, ErrorCode.NoMoreConnectionToFailOver);
    selector = new DatabaseSelector(new DalHints().setHA(new DalHA().addDB(M1).addDB(M2)), ms, ss, true, false);
    assertSelector(selector, ErrorCode.NoMoreConnectionToFailOver);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalHA(com.ctrip.platform.dal.dao.client.DalHA) DatabaseSelector(com.ctrip.platform.dal.dao.configure.DatabaseSelector) ArrayList(java.util.ArrayList) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 29 with DatabaseSelector

use of com.ctrip.platform.dal.dao.configure.DatabaseSelector in project dal by ctripcorp.

the class DatabaseSelectorTest method hasHAMixedMasterAndSlaveButOneSlaveMarkdownTest.

@Test
public void hasHAMixedMasterAndSlaveButOneSlaveMarkdownTest() throws DalException {
    List<DataBase> ms = new ArrayList<DataBase>();
    ms.add(new DataBase(M1, true, "", M1));
    ms.add(new DataBase(M2, true, "", M2));
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    autoMarkdown(S1);
    DalHA ha = new DalHA();
    ha.addDB(M1);
    DatabaseSelector selector = new DatabaseSelector(new DalHints().setHA(ha), ms, ss, false, true);
    String dbName = selector.select();
    Assert.assertTrue(dbName.equals(S2));
    // masterOnly
    ha = new DalHA();
    ha.addDB(M1);
    selector = new DatabaseSelector(new DalHints().setHA(ha), ms, ss, true, true);
    Assert.assertEquals(M2, selector.select());
    ha = new DalHA();
    ha.addDB(M1);
    selector = new DatabaseSelector(new DalHints().setHA(ha), ms, ss, true, false);
    Assert.assertEquals(M2, selector.select());
    ha = new DalHA();
    ha.addDB(M1);
    selector = new DatabaseSelector(new DalHints().setHA(ha), ms, ss, false, false);
    Assert.assertEquals(M2, selector.select());
    ha = new DalHA();
    ha.addDB(M1);
    selector = new DatabaseSelector(new DalHints().setHA(ha).inDatabase(M1), ms, ss, false, false);
    assertSelector(selector, ErrorCode.NoMoreConnectionToFailOver);
    ha = new DalHA();
    ha.addDB(M1);
    autoMarkdown(M2);
    selector = new DatabaseSelector(new DalHints().setHA(ha), ms, ss, false, false);
    Assert.assertEquals(M1, selector.select());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalHA(com.ctrip.platform.dal.dao.client.DalHA) DatabaseSelector(com.ctrip.platform.dal.dao.configure.DatabaseSelector) ArrayList(java.util.ArrayList) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 30 with DatabaseSelector

use of com.ctrip.platform.dal.dao.configure.DatabaseSelector in project dal by ctripcorp.

the class DatabaseSelectorTest method notMasterOnlyMarkdownTest.

@Test
public void notMasterOnlyMarkdownTest() throws DalException {
    List<DataBase> ms = new ArrayList<DataBase>();
    ms.add(new DataBase(M1, true, "", M1));
    ms.add(new DataBase(M2, true, "", M2));
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    ss.add(new DataBase(S3, false, "", S3));
    DatabaseSelector selector;
    autoMarkdown(S1);
    autoMarkdown(S2);
    autoMarkdown(S3);
    selector = new DatabaseSelector(null, ms, ss, false, true);
    assertSelector(selector, M1, M2);
    selector = new DatabaseSelector(null, ms, ss, false, false);
    assertSelector(selector, M1, M2);
    setUp();
    autoMarkdown(M1);
    autoMarkdown(M2);
    selector = new DatabaseSelector(null, ms, ss, false, true);
    assertSelector(selector, S1, S2, S3);
    selector = new DatabaseSelector(null, ms, ss, false, false);
    assertSelector(selector, ErrorCode.MarkdownConnection);
    autoMarkdown(M1);
    autoMarkdown(M2);
    autoMarkdown(S1);
    autoMarkdown(S2);
    autoMarkdown(S3);
    selector = new DatabaseSelector(null, ms, ss, false, true);
    assertSelector(selector, ErrorCode.MarkdownConnection);
    selector = new DatabaseSelector(null, ms, ss, false, false);
    assertSelector(selector, ErrorCode.MarkdownConnection);
}
Also used : DatabaseSelector(com.ctrip.platform.dal.dao.configure.DatabaseSelector) ArrayList(java.util.ArrayList) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Aggregations

DataBase (com.ctrip.platform.dal.dao.configure.DataBase)33 DatabaseSelector (com.ctrip.platform.dal.dao.configure.DatabaseSelector)33 ArrayList (java.util.ArrayList)33 Test (org.junit.Test)33 DalHints (com.ctrip.platform.dal.dao.DalHints)26 DalHA (com.ctrip.platform.dal.dao.client.DalHA)15