com.taobao.diamond.mockserver.MockServer Java Examples

The following examples show how to use com.taobao.diamond.mockserver.MockServer. 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 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 #2
Source File: AtomDynamicChangePswTest.java    From tddl 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 #3
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 #4
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 #5
Source File: DataSourceIndexTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupDataSourceRouteHelper() throws Exception {
    try {
        TGroupDataSource ds = new TGroupDataSource(GROUP0, APPNAME);
        MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
        ds.init();

        GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(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 #6
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 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 #7
Source File: AtomDynamicChangeGlobalTest.java    From tddl 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 #8
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 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 #9
Source File: DataSourceIndexTest.java    From tddl5 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 #10
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example #11
Source File: DataSourceIndexTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupDataSourceRouteHelper() throws Exception {
    try {
        TGroupDataSource ds = new TGroupDataSource(GROUP0, APPNAME);
        MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":rw" + "," + DSKEY1 + ":r");
        ds.init();

        GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(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 #12
Source File: AtomWriteRestraintTest.java    From tddl with Apache License 2.0 6 votes vote down vote up
@Test
public void moreThanWriteRestraintTest() throws InterruptedException {
    if (ASTATICISM_TEST) {
        return;
    }

    int WriteCount = 20;
    int executCount = 10;

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

    String sql = "update normaltbl_0001 set gmt_create=? where pk=?";
    Object[] args = new Object[] { nextDay, RANDOM_ID };
    for (int i = 0; i < WriteCount; i++) {
        try {
            int rs = tddlJT.update(sql, args);
            Assert.assertEquals(1, rs);
            executCount--;
        } catch (DataAccessException ex) {
        }
    }

    Assert.assertEquals(0, executCount);
}
 
Example #13
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
public String getAvailableConfigureInfomation(String dataId, String group, long timeout) {
    // 尝试先从本地和网络获取配置信息
    try {
        String result = getConfigureInfomation(dataId, group, timeout);
        if (result != null && result.length() > 0) {
            return result;
        }
    }
    catch (Throwable t) {
        log.error(t.getMessage(), t);
    }

    // 测试模式不使用本地dump
    if (MockServer.isTestMode()) {
        return null;
    }
    return getSnapshotConfiginfomation(dataId, group);
}
 
Example #14
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example #15
Source File: AtomWriteRestraintTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
@Test
public void moreThanWriteRestraintTest() throws InterruptedException {
    if (ASTATICISM_TEST) {
        return;
    }

    int WriteCount = 20;
    int executCount = 10;

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

    String sql = "update normaltbl_0001 set gmt_create=? where pk=?";
    Object[] args = new Object[] { nextDay, RANDOM_ID };
    for (int i = 0; i < WriteCount; i++) {
        try {
            int rs = tddlJT.update(sql, args);
            Assert.assertEquals(1, rs);
            executCount--;
        } catch (DataAccessException ex) {
        }
    }

    Assert.assertEquals(0, executCount);
}
 
Example #16
Source File: AtomDynamicChangeAppTest.java    From tddl 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 #17
Source File: ChangeMasterSlaveTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void dynamicChangeMSDSTest() throws Exception {
    // 主备切换之前,正常执行一条sql
    String sql = "insert into normaltbl_0001 (pk,gmt_create) values (?,?)";
    tddlJT.update(sql, new Object[] { RANDOM_ID, time });

    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0);
    Map re = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(time, String.valueOf(re.get("gmt_create")));
    // 清除数据
    clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });

    // 主备切换(确保推送成功)
    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);
    }

    // 主备切换之后,正常执行一条sql
    clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    sql = "insert into normaltbl_0001 (pk,gmt_create) values (?,?)";
    int rs = tddlJT.update(sql, new Object[] { RANDOM_ID, time });
    Assert.assertEquals(1, rs);

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

    // 清除数据
    clearData(tddlJT, "delete from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });

    // 指定写库的dataSourceIndex
    GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(2);
    sql = "insert into normaltbl_0001 (pk,gmt_create) values (?,?)";
    rs = tddlJT.update(sql, new Object[] { RANDOM_ID, time });
    Assert.assertEquals(1, rs);
}
 
Example #18
Source File: CrudTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void 三个数据库_测试db1可读写_db2与db3只能读_atom() throws Exception {
    TGroupDataSource ds = new TGroupDataSource();
    ds.setDbGroupKey(GROUP0);
    ds.setAppName(APPNAME);
    MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":r10w" + "," + DSKEY1 + ":r20" + "," + DSKEY2
                                                     + ":r30");
    ds.init();

    testCrud(ds);
}
 
Example #19
Source File: CrudTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void 在只写库上更新后再查询会重用写库上的连接_即使它是一个只写库也不管_atom() throws Exception {
    TGroupDataSource ds = new TGroupDataSource();
    ds.setDbGroupKey(GROUP0);
    ds.setAppName(APPNAME);
    MockServer.setConfigInfo(ds.getFullDbGroupKey(), DSKEY0 + ":w" + "," + DSKEY1 + ":r20" + "," + DSKEY2 + ":r30");
    ds.init();

    testCrud_Read(ds);
}
 
Example #20
Source File: TAtomDataSourceTest.java    From tddl 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, AtomAlreadyInitException, 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 #21
Source File: GroupRetryExecuteTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void oneOfAtomDssInGropuIsOkTest() throws Exception {
    // 改变group中的rw状态(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:NA,qatest_normal_0_bac:r,qatest_normal_1_bac:r");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    int successCnt = 0;
    for (int i = 0; i < 20; i++) {
        tddlJT.queryForList(sql);
        successCnt++;
    }
    Assert.assertEquals(20, successCnt);

    // qatest_normal_0状态改为只读(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(TAtomConstants.getGlobalDataId(DBKEY_0),
            "ip=10.232.31.154\r\nport=3306\r\ndbName=qatest_normal_0\r\ndbType=mysql\r\ndbStatus=NA");
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:wr,qatest_normal_0_bac:r,qatest_normal_1_bac:r");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    successCnt = 0;
    for (int i = 0; i < 20; i++) {
        tddlJT.queryForList(sql);
        successCnt++;
    }
    Assert.assertEquals(20, successCnt);
}
 
Example #22
Source File: GroupSelectDbUseWeightTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void selectDBUseWeightTest() throws InterruptedException {
    // qatest_normal_0:w0r0,qatest_normal_0_bac:r0,qatest_normal_1:r10,肯定会读到qatest_normal_1库(确保推送成功)
    for (int i = 0; i < 2; i++) {
        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:wr0,qatest_normal_0_bac:r0,qatest_normal_1_bac:r10");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    // 肯定查到qatest_normal_1_bac库
    Map rex = tddlJT.queryForMap("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
    Assert.assertEquals(theDayAfterTomorow, String.valueOf(rex.get("gmt_create")));

    // 三个数据源R全变成0(确保推送成功)
    for (int i = 0; i < 2; i++) {

        MockServer.setConfigInfo(tds.getFullDbGroupKey(),
            "qatest_normal_0:wr0,qatest_normal_0_bac:r0,qatest_normal_1_bac:r0");
        TimeUnit.SECONDS.sleep(SLEEP_TIME);
    }

    // 3个数据源查询全为0,肯定查不到任何数据,肯定抛异常
    try {
        tddlJT.queryForList("select * from normaltbl_0001 where pk=?", new Object[] { RANDOM_ID });
        Assert.fail();
    } catch (DataAccessException e1) {

    }
}
 
Example #23
Source File: BaseMatrixTestCase.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void IEnvInit() throws Exception {
    MockServer.tearDownMockServer();
    // setMatrixMockInfo(MATRIX_DBGROUPS_PATH, TDDL_DBGROUPS);

    if (tddlDatasource == null) {
        tddlDatasource = JDBCClient(dbType, false);
    }

    mysqlConnection = getConnection();
    // tddlConnection = getTddlServerConnection();
    tddlConnection = tddlDatasource.getConnection();
}
 
Example #24
Source File: AtomDynamicChangeGlobalTest.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
@Test
public void dynamicChangeGlobalDbStatusToWTest() 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=W\r\n");
    TimeUnit.SECONDS.sleep(SLEEP_TIME);
    int result = tddlJT.update("update normaltbl_0001 set gmt_create=? where pk=?",
        new Object[] { time, RANDOM_ID });
    Assert.assertEquals(1, result);

    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=mysql\r\ndbStatus=W\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);

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

}
 
Example #25
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 #26
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 #27
Source File: AtomTestCase.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
protected static void setAtomMockInfo(String path, String appName, String dbKey) throws Exception {
    dataMap = new HashMap<String, String>();

    // -----------------db1
    initAtomConfig(path, appName, dbKey);

    // -----------------MockServer
    MockServer.setConfigInfos(dataMap);
}
 
Example #28
Source File: BaseGroupTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    MockServer.setUpMockServer();
    // 初始化一些配置
    mockConfig("group0", APPNAME, GROUP0, DSKEY0);
    mockConfig("group1", APPNAME, GROUP1, DSKEY1);
    mockConfig("group2", APPNAME, GROUP2, DSKEY2);
}
 
Example #29
Source File: GroupSelectDbUseWeightTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void someOfDssWithoutRWAndWeightTest() throws InterruptedException {
    /* 有部分DS没有设置权重值(确保推送成功) */
    for (int i = 0; i < 2; i++) {
        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);
    checkWeight(operationCnt, firstCnt, 1.0 / 3);
    checkWeight(operationCnt, secondCnt, 1.0 / 3);
    checkWeight(operationCnt, thirdCnt, 1.0 / 3);
}
 
Example #30
Source File: AtomDynamicChangePswTest.java    From tddl 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"));
}