Search in sources :

Example 21 with SelectionContext

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

the class DatabaseSelectorTest method hasHASlavesTest.

@Test
public void hasHASlavesTest() throws DalException {
    List<DataBase> ss = new ArrayList<DataBase>();
    ss.add(new DataBase(S1, false, "", S1));
    ss.add(new DataBase(S2, false, "", S2));
    DalHA ha = new DalHA();
    ha.addDB(S1);
    SelectionContext context = getContext(new DalHints().setHA(ha), null, ss, false, true);
    Assert.assertEquals(S2, selector.select(context));
    // masterOnly
    ha = new DalHA();
    ha.addDB(S1);
    context = getContext(new DalHints().setHA(ha), null, ss, true, true);
    assertSelector(context, ErrorCode.NullLogicDbName);
    ha = new DalHA();
    ha.addDB(S1);
    context = getContext(new DalHints().setHA(ha), null, ss, true, false);
    assertSelector(context, ErrorCode.NullLogicDbName);
}
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 22 with SelectionContext

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

the class DatabaseSelectorTest method onlyOneMasterTest.

@Test
public void onlyOneMasterTest() throws DalException {
    DataBase db = new DataBase(M1, true, "", M1);
    List<DataBase> dbs = new ArrayList<DataBase>();
    dbs.add(db);
    SelectionContext context = getContext(null, dbs, null, false, false);
    Assert.assertEquals(M1, selector.select(context));
    context = getContext(null, dbs, null, false, true);
    Assert.assertEquals(M1, selector.select(context));
    // masterOnly
    context = getContext(null, dbs, null, true, false);
    Assert.assertEquals(M1, selector.select(context));
    context = getContext(null, dbs, null, true, true);
    Assert.assertEquals(M1, selector.select(context));
}
Also used : 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 23 with SelectionContext

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

the class DatabaseSelectorTest method onlyOneSlaveTest.

@Test
public void onlyOneSlaveTest() throws DalException {
    DataBase db = new DataBase(S1, false, "", S1);
    List<DataBase> dbs = new ArrayList<DataBase>();
    dbs.add(db);
    SelectionContext context = getContext(null, null, dbs, false, true);
    Assert.assertEquals(S1, selector.select(context));
    context = getContext(new DalHints().inDatabase(S1), null, dbs, false, true);
    Assert.assertEquals(S1, selector.select(context));
    context = getContext(new DalHints().inDatabase(M1), null, dbs, false, true);
    assertSelector(context, ErrorCode.InvalidDatabaseKeyName);
    // masterOnly
    context = getContext(null, null, dbs, true, true);
    assertSelector(context, ErrorCode.NullLogicDbName);
    context = getContext(null, null, dbs, true, false);
    assertSelector(context, ErrorCode.NullLogicDbName);
    // test pointed
    context = getContext(new DalHints().inDatabase(S1), null, dbs, true, true);
    assertSelector(context, ErrorCode.NullLogicDbName);
    context = getContext(new DalHints().inDatabase(S1), null, dbs, true, false);
    assertSelector(context, ErrorCode.NullLogicDbName);
}
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 24 with SelectionContext

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

the class DatabaseSelectorTest method notMasterOnlyHaTest.

@Test
public void notMasterOnlyHaTest() 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));
    SelectionContext context;
    Set<String> matched = newHashSet(S1, S2, S3);
    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);
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, matched);
    matched = newHashSet(M1, M2);
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, matched);
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, matched);
    context = getContext(hints, ms, ss, false, true);
    assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
}
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 25 with SelectionContext

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

the class DatabaseSelectorTest method onlyHaveMasterTest.

@Test
public void onlyHaveMasterTest() 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;
    context = getContext(null, ms, ss, false, true);
    assertSelector(context, M1, M2);
    context = getContext(null, ms, ss, false, false);
    assertSelector(context, M1, M2);
    // Master only
    context = getContext(null, ms, ss, true, true);
    assertSelector(context, M1, M2);
    context = getContext(null, ms, ss, true, false);
    assertSelector(context, M1, M2);
    autoMarkdown(M1);
    context = getContext(null, ms, ss, true, false);
    assertSelector(context, M2);
    autoMarkdown(M2);
    context = getContext(null, ms, ss, true, false);
    assertSelector(context, ErrorCode.MarkdownConnection);
}
Also used : 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

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