Search in sources :

Example 31 with DataBase

use of com.ctrip.platform.dal.dao.configure.DataBase 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);
    SelectionContext context = getContext(new DalHints().setHA(ha), ms, ss, false, true);
    String dbName = selector.select(context);
    Assert.assertTrue(dbName.equals(S2));
    // masterOnly
    ha = new DalHA();
    ha.addDB(M1);
    context = getContext(new DalHints().setHA(ha), ms, ss, true, true);
    Assert.assertEquals(M2, selector.select(context));
    ha = new DalHA();
    ha.addDB(M1);
    context = getContext(new DalHints().setHA(ha), ms, ss, true, false);
    Assert.assertEquals(M2, selector.select(context));
    ha = new DalHA();
    ha.addDB(M1);
    context = getContext(new DalHints().setHA(ha), ms, ss, false, false);
    Assert.assertEquals(M2, selector.select(context));
    ha = new DalHA();
    ha.addDB(M1);
    context = getContext(new DalHints().setHA(ha).inDatabase(M1), ms, ss, false, false);
    assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
    ha = new DalHA();
    ha.addDB(M1);
    autoMarkdown(M2);
    context = getContext(new DalHints().setHA(ha), ms, ss, false, false);
    Assert.assertEquals(M1, selector.select(context));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalHA(com.ctrip.platform.dal.dao.client.DalHA) ArrayList(java.util.ArrayList) SelectionContext(com.ctrip.platform.dal.dao.configure.SelectionContext) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 32 with DataBase

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

the class DatabaseSelectorTest method notSelectTest.

@Test
public void notSelectTest() 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));
    SelectionContext context;
    context = getContext(new DalHints(), ms, ss, false, false);
    String dbName = selector.select(context);
    Assert.assertTrue(dbName.equals(M1) || dbName.equals(M2));
    context = getContext(new DalHints().inDatabase(M1), ms, ss, false, false);
    Assert.assertTrue(selector.select(context).equals(M1));
    context = getContext(new DalHints().inDatabase(M2), ms, ss, false, false);
    Assert.assertTrue(selector.select(context).equals(M2));
    context = getContext(new DalHints().inDatabase(S2), ms, ss, false, false);
    assertSelector(context, ErrorCode.InvalidDatabaseKeyName);
    // masterOnly
    context = getContext(null, ms, ss, true, false);
    dbName = selector.select(context);
    Assert.assertTrue(dbName.equals(M1) || dbName.equals(M2));
    context = getContext(new DalHints().inDatabase(M1), ms, ss, true, false);
    Assert.assertTrue(selector.select(context).equals(M1));
    context = getContext(new DalHints().inDatabase(M2), ms, ss, true, false);
    Assert.assertTrue(selector.select(context).equals(M2));
    context = getContext(new DalHints().inDatabase(S2), ms, ss, true, false);
    assertSelector(context, ErrorCode.InvalidDatabaseKeyName);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) SelectionContext(com.ctrip.platform.dal.dao.configure.SelectionContext) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Example 33 with DataBase

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

the class DatabaseSelectorTest method onlyHaveMasterHaTest.

@Test
public void onlyHaveMasterHaTest() 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 = null;
    SelectionContext context;
    Set<String> matched;
    matched = newHashSet(M1, M2);
    DalHints hints = new DalHints().setHA(new DalHA());
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, matched);
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, matched);
    // reset
    matched = newHashSet(M1, M2);
    hints = new DalHints().setHA(new DalHA());
    context = getContext(hints, ms, ss, false, false);
    assertSelector(context, matched);
    context = getContext(hints, ms, ss, false, false);
    assertSelector(context, matched);
    // Master only
    matched = newHashSet(M1, M2);
    hints = new DalHints().setHA(new DalHA());
    context = getContext(hints, ms, ss, true, true);
    assertSelector(context, M1, M2);
    context = getContext(hints, ms, ss, true, true);
    assertSelector(context, M1, M2);
    // reset
    matched = newHashSet(M1, M2);
    hints = new DalHints().setHA(new DalHA());
    context = getContext(hints, ms, ss, true, false);
    assertSelector(context, M1, M2);
    context = getContext(hints, ms, ss, true, false);
    assertSelector(context, M1, M2);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalHA(com.ctrip.platform.dal.dao.client.DalHA) ArrayList(java.util.ArrayList) SelectionContext(com.ctrip.platform.dal.dao.configure.SelectionContext) DataBase(com.ctrip.platform.dal.dao.configure.DataBase) Test(org.junit.Test)

Aggregations

DataBase (com.ctrip.platform.dal.dao.configure.DataBase)33 SelectionContext (com.ctrip.platform.dal.dao.configure.SelectionContext)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