Java Code Examples for com.taobao.diamond.mockserver.MockServer#setConfigInfo()

The following examples show how to use com.taobao.diamond.mockserver.MockServer#setConfigInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Test
public void dynamicChangeGlobalIpTest() throws InterruptedException {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));

    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.13.40.25\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.13.40.25\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail("");
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
}
 
Example 2
Source File: DataSourceIndexTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreadLocalDataSourceIndex() throws Exception {
    try {
        TGroupDataSource ds = new TGroupDataSource(GROUP0, APPNAME);
        MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
        ds.init();

        ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, 0);
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
        ResultSet rs = stmt.executeQuery("select id,name from tddl_test_0000 where id=100");
        assertTrue(rs.next());
        // 如果指定了index,忽略rw限制
        ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, 1);
        assertEquals(stmt.executeUpdate("insert into tddl_test_0000(id,name) values(100,'str')"), 1);
        rs = stmt.executeQuery("select count(*) from tddl_test_0000 where id=100");
        assertTrue(rs.next());
        assertEquals(rs.getInt(1), 1);

        stmt.close();
        conn.close();
    } finally {
        ThreadLocalMap.put(ThreadLocalString.DATASOURCE_INDEX, null);
    }
}
 
Example 3
Source File: AtomDynamicChangeGlobalTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Test
public void dynamicChangeGlobalPortTest() throws InterruptedException {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3300\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");

    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3300\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
}
 
Example 4
Source File: AtomDynamicChangeGlobalTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Ignore("oracle驱动暂时没依赖")
@Test
public void dynamicChangeGlobalDbTypeTest() throws InterruptedException {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=oracle\r\ndbStatus=RW\r\n");

    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=oracle\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
}
 
Example 5
Source File: AtomDynamicChangePswTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Ignore("druid对passwd变更不会关闭老的正常的链接")
@Test
public void dynamicChangePswTest() throws Exception {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getPasswdDataId(DBKEY_0, DBTYPE_MYSQL, "tddl"),
        "encPasswd=dddddddd\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getPasswdDataId(DBKEY_0, DBTYPE_MYSQL, "tddl"),
            "encPasswd=dddddddd\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
}
 
Example 6
Source File: AtomDynamicChangeGlobalTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Test
public void dynamicChangeGlobalDbNameTest() throws InterruptedException {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal\r\ndbType=mysql\r\ndbStatus=RW\r\n");

    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=\r\nport=3306\r\ndbName=qatest_normal\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
}
 
Example 7
Source File: AtomDynamicChangeAppTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Test
public void dynamicChangeAppnameTest() throws Exception {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));

    MockServer.setConfigInfo(TAtomConstants.getAppDataId(APPNAME, DBKEY_0),
        "maxPoolSize=100\r\nuserName=xxxx\r\nminPoolSize=1\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);

    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getAppDataId(APPNAME, DBKEY_0),
            "maxPoolSize=100\r\nuserName=xxxx\r\nminPoolSize=1\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
}
 
Example 8
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Test
public void dynamicChangeGlobalPortTest() throws InterruptedException {
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3300\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");

    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    try {
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3300\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
}
 
Example 9
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
private void restore() throws Exception {
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    Map re = null;
    try {
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    } catch (Exception ex) {
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    }
    Assert.assertEquals("manhong", re.get("name"));
}
 
Example 10
Source File: GroupSelectDbUseRwTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void wireteDataWithRWTest_add() throws InterruptedException {
    // 将2个库全部设置为只读,1个为读写(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:r,qatest_normal_0_bac:wr,qatest_normal_1_bac:r");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    // 插入数据
    String sql = "insert into normaltbl_0001 (pk,gmt_create) values (?,?)";
    int rs = tddlJT.update(sql, new Object[] { RANDOM_ID, time });
    Assert.assertTrue(rs > 0);

    sql = "select * from normaltbl_0001 where pk=?";
    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
    Map re = tddlJT.queryForMap(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));

    // 更新数据
    sql = "update normaltbl_0001 set gmt_create=? where pk=?";
    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
    rs = tddlJT.update(sql, new Object[] { nextDay, RANDOM_ID });
    Assert.assertTrue(rs > 0);

    sql = "select * from normaltbl_0001 where pk=?";
    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
    re = tddlJT.queryForMap(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(nextDay, String.valueOf(re.get("gmt_create")));
}
 
Example 11
Source File: GroupSelectDbUseWeightTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void someOfDssWithoutRWAndWeightWhoDbstatusAreNATest() throws InterruptedException {
    /* 有部分DS没有设置权重值 */
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0_BAC),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0_bac\r\ndbType=mysql\r\ndbStatus=NA");
        MockServer.setConfigInfo(tds.getFullDbGroupKey(), "qatest_normal_0:wr1,,qatest_normal_1_bac:r3");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int firstCnt = 0;
    int secondCnt = 0;
    int thirdCnt = 0;
    for (int i = 0; i < operationCnt; i++) {
        Map rex = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        if (time.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            firstCnt++;
        } else if (nextDay.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            secondCnt++;
        } else if (theDayAfterTomorow.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            thirdCnt++;
        } else {
            Assert.fail("查询结果中出现不该有的数据。gmt_create = " + String.valueOf(rex.get("gmt_create")));
        }
    }

    System.err.println("firstCnt=" + firstCnt + ", secondCnt=" + secondCnt + ", thirdCnt=" + thirdCnt);
    Assert.assertEquals(operationCnt, firstCnt + secondCnt + thirdCnt);
    Assert.assertEquals(0, secondCnt);
    checkWeight(operationCnt, firstCnt, 1.0 / 2);
    checkWeight(operationCnt, thirdCnt, 1.0 / 2);

    // 恢复配置(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0_BAC),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0_bac\r\ndbType=mysql\r\ndbStatus=RW");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }
}
 
Example 12
Source File: CrudTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void 测试DataSourceWrapper_atom() throws Exception {
    TGroupDataSource ds = new TGroupDataSource();
    ds.setDbGroupKey(GROUP0);
    ds.setAppName(APPNAME);
    MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
    ds.init();

    testCrud(ds);
}
 
Example 13
Source File: GroupSelectDbUseWeightTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void someOfDssWithoutWeightTest() throws InterruptedException {
    /* 有部分DS没有设置权重值(确保推送成功) */
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:wr1,qatest_normal_0_bac:r,qatest_normal_1_bac:r3");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int firstCnt = 0;
    int secondCnt = 0;
    int thirdCnt = 0;
    for (int i = 0; i < operationCnt; i++) {
        Map rex = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        if (time.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            firstCnt++;
        } else if (nextDay.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            secondCnt++;
        } else if (theDayAfterTomorow.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            thirdCnt++;
        } else {
            Assert.fail("查询结果中出现不该有的数据。gmt_create = " + String.valueOf(rex.get("gmt_create")));
        }
    }

    System.err.println("firstCnt=" + firstCnt + ", secondCnt=" + secondCnt + ", thirdCnt=" + thirdCnt);
    Assert.assertEquals(operationCnt, firstCnt + secondCnt + thirdCnt);
    // Assert.assertEquals(0, secondCnt);
    checkWeight(operationCnt, firstCnt, 1.0 / 14);
    checkWeight(operationCnt, secondCnt, 10.0 / 14);
    /**
     * 在权重设置过程中,有部分权重没有设置 , 那么默认情况下为10,这个设计不怎么合理 ,需要开发确认
     */
    checkWeight(operationCnt, thirdCnt, 3.0 / 14);

}
 
Example 14
Source File: AtomDynamicChangePswTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
private void restore() throws Exception {
    MockServer.setConfigInfo(TAtomConstants.getPasswdDataId(DBKEY_0, DBTYPE_MYSQL, "tddl"),
        "encPasswd=4485f91c9426e4d8\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    Map re = null;
    try {
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    } catch (Exception ex) {
        MockServer.setConfigInfo(TAtomConstants.getPasswdDataId(DBKEY_0, DBTYPE_MYSQL, "tddl"),
            "encPasswd=4485f91c9426e4d8\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    }
    Assert.assertEquals("manhong", re.get("name"));
}
 
Example 15
Source File: GroupSelectDbUseWeightTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void balanceWeightTest() throws InterruptedException {
    /* 均衡权重(确保推送成功) */
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:wr10,qatest_normal_0_bac:r10,qatest_normal_1_bac:r10");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int firstCnt = 0;
    int secondCnt = 0;
    int thirdCnt = 0;
    for (int i = 0; i < operationCnt; i++) {
        Map rex = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        if (time.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            firstCnt++;
        } else if (nextDay.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            secondCnt++;
        } else if (theDayAfterTomorow.equalsIgnoreCase(String.valueOf(rex.get("gmt_create")))) {
            thirdCnt++;
        } else {
            Assert.fail("查询结果中出现不该有的数据。gmt_create = " + String.valueOf(rex.get("gmt_create")));
        }
    }

    System.err.println("firstCnt=" + firstCnt + ", secondCnt=" + secondCnt + ", thirdCnt=" + thirdCnt);
    Assert.assertEquals(operationCnt, firstCnt + secondCnt + thirdCnt);
    checkWeight(operationCnt, firstCnt, 1.0 / 3);
    checkWeight(operationCnt, secondCnt, 1.0 / 3);
    checkWeight(operationCnt, thirdCnt, 1.0 / 3);
}
 
Example 16
Source File: AtomDynamicChangeGlobalTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
private void restore() throws Exception {
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    Map re = null;
    try {
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    } catch (Exception ex) {
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    }
    Assert.assertEquals("manhong", re.get("name"));
}
 
Example 17
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void dynamicChangeGlobalDbStatusToRTest() throws InterruptedException {
    String sql = "update normaltbl_0001  set gmt_create=? where pk=?";
    tddlJT.update(sql, new Object[] { nextDay, RANDOM_ID });
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(nextDay, String.valueOf(re.get("gmt_create")));
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=R\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(nextDay, String.valueOf(re.get("gmt_create")));
    try {
        tddlJT.update("update normaltbl_0001 set gmt_create=? where pk=?", new Object[] { time, RANDOM_ID });
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=R\r\n");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
        tddlJT.update("update normaltbl_0001 set gmt_create=? where pk=?", new Object[] { time, RANDOM_ID });
        Assert.fail();
    } catch (Exception ex) {
    }
    MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
        "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=RW\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);

    tddlJT.update("update normaltbl_0001 set gmt_create=? where pk=?", new Object[] { time, RANDOM_ID });
    re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));

}
 
Example 18
Source File: TAtomDataSourceTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
private void testChange(ChangeTestConfig change, ChangeTestConfig restore, String type, String dbType,
                        String methodName) throws IOException, TddlException, Exception {
    String appName = "tddl_sample";
    String dbKey = "unitTestDb-" + methodName;
    String configName = dbType;
    String testSql = TAtomDataSourceTest.TEST_SQL;
    TAtomDataSource tAtomDataSource = createTAtomDataSource(appName, dbKey, configName);
    JdbcTemplate jtp = new JdbcTemplate(tAtomDataSource);
    int actual = jtp.queryForInt(testSql);
    Assert.assertEquals(actual, 1);
    // 设置错误的IP进行推送
    String fileName = "";
    String dataId = "";
    if (type.equals("globa")) {
        fileName = "/globa.properties";
        dataId = TAtomConstants.getGlobalDataId(dbKey);
    } else if (type.equals("app")) {
        fileName = "/app.properties";
        dataId = TAtomConstants.getAppDataId(appName, dbKey);
    }
    Properties prop = PropLoadTestUtil.loadPropFromFile("conf/" + configName + fileName);
    MockServer.setConfigInfo(dataId, PropLoadTestUtil.convertProp2Str(change.doChange(prop)));
    Thread.sleep(3000);
    // 期待出现错误
    boolean result = false;
    try {
        actual = jtp.queryForInt(testSql);
    } catch (Throwable e) {
        result = true;
    }
    Assert.assertTrue(result);
    MockServer.setConfigInfo(dataId,
        PropLoadTestUtil.convertProp2Str(restore.doChange(PropLoadTestUtil.loadPropFromFile("conf/" + configName
                                                                                            + fileName))));
    Thread.sleep(3000);
    // 期待结果正常
    actual = jtp.queryForInt(testSql);
    Assert.assertEquals(actual, 1);
    tAtomDataSource.destroyDataSource();
}
 
Example 19
Source File: GroupUpdateDbUseWeightTest.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
@Test
public void someOfTheDssWeightAreZeroTest() throws InterruptedException {
    // 跳过本用例测试
    if (SOME_SHOULD_NOT_BE_TEST) {
        return;
    }

    // 设置权重(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:w1r10,qatest_normal_0_bac:w0r10,qatest_normal_1_bac:w3r10");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int operationCnt = 1000;
    int firstCnt = 0;
    int secondCnt = 0;
    int thirdCnt = 0;
    for (int i = 0; i < operationCnt; i++) {
        // 插入数据
        int rs = tddlJT.update("insert into normaltbl_0001 (pk,gmt_create) values (?,?)", new Object[] { RANDOM_ID,
                time });
        Assert.assertTrue(rs > 0);

        // 确认更新的atomDS
        GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
        List list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        if (list.size() == 1) {
            firstCnt++;
            GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
            clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        } else {
            GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
            list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
            if (list.size() == 1) {
                secondCnt++;
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
                clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
            } else {
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(2);
                list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
                if (list.size() == 1) {
                    thirdCnt++;
                    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(2);
                    clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
                } else {
                    Assert.fail("查不到数据。");
                }
            }
        }
    }

    System.err.println("firstCnt=" + firstCnt + ", secondCnt=" + secondCnt + ", thirdCnt=" + thirdCnt);
    Assert.assertEquals(operationCnt, firstCnt + secondCnt + thirdCnt);
    Assert.assertEquals(0, secondCnt);
    checkWeight(operationCnt, firstCnt, 1.0 / 4);
    checkWeight(operationCnt, thirdCnt, 3.0 / 4);
}
 
Example 20
Source File: GroupUpdateDbUseWeightTest.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
@Test
public void updateDBUseImbalanceWeightTest() throws Exception {
    // 跳过本用例测试
    if (SOME_SHOULD_NOT_BE_TEST) {
        return;
    }

    // 设置权重(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:w1r10,qatest_normal_0_bac:w2r10,qatest_normal_1_bac:w3r10");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int operationCnt = 1000;
    int firstCnt = 0;
    int secondCnt = 0;
    int thirdCnt = 0;
    for (int i = 0; i < operationCnt; i++) {
        // 插入数据
        int rs = tddlJT.update("insert into normaltbl_0001 (pk,gmt_create) values (?,?)", new Object[] { RANDOM_ID,
                time });
        Assert.assertTrue(rs > 0);

        // 确认更新的atomDS
        GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
        List list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        if (list.size() == 1) {
            firstCnt++;
            GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
            clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        } else {
            GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
            list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
            if (list.size() == 1) {
                secondCnt++;
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1);
                clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
            } else {
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(2);
                list = tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
                if (list.size() == 1) {
                    thirdCnt++;
                    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(2);
                    clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
                } else {
                    Assert.fail("查不到数据。");
                }
            }
        }
    }

    Assert.assertEquals(operationCnt, firstCnt + secondCnt + thirdCnt);
    System.err.println("firstCnt=" + firstCnt + ", secondCnt=" + secondCnt + ", thirdCnt=" + thirdCnt);
    checkWeight(operationCnt, firstCnt, 1.0 / 6);
    checkWeight(operationCnt, secondCnt, 2.0 / 6);
    checkWeight(operationCnt, thirdCnt, 3.0 / 6);
}