提交 e4b38111 编写于 作者: Z zyyang

[TS-598]<test>: add more jdbc test cases

上级 00c762cb
......@@ -980,8 +980,10 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override
public void close() throws SQLException {
this.columnDataClearBatchInternal();
this.columnDataCloseBatch();
if (this.nativeStmtHandle != 0L) {
this.columnDataClearBatchInternal();
this.columnDataCloseBatch();
}
super.close();
}
}
......@@ -30,42 +30,6 @@ public class TSDBConnectionTest {
}
}
@Test
public void runSubscribe() {
try {
// given
TSDBConnection unwrap = conn.unwrap(TSDBConnection.class);
TSDBSubscribe subscribe = unwrap.subscribe("topic1", "select * from log.log", false);
// when
TSDBResultSet rs = subscribe.consume();
ResultSetMetaData metaData = rs.getMetaData();
// then
Assert.assertNotNull(rs);
Assert.assertEquals(4, metaData.getColumnCount());
Assert.assertEquals("ts", metaData.getColumnLabel(1));
Assert.assertEquals("level", metaData.getColumnLabel(2));
Assert.assertEquals("content", metaData.getColumnLabel(3));
Assert.assertEquals("ipaddr", metaData.getColumnLabel(4));
rs.next();
// row 1
{
Assert.assertNotNull(rs.getTimestamp(1));
Assert.assertNotNull(rs.getTimestamp("ts"));
Assert.assertNotNull(rs.getByte(2));
Assert.assertNotNull(rs.getByte("level"));
Assert.assertNotNull(rs.getString(3));
Assert.assertNotNull(rs.getString("content"));
Assert.assertNotNull(rs.getString(4));
Assert.assertNotNull(rs.getString("ipaddr"));
}
subscribe.close(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void prepareStatement() throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("select server_status()");
......@@ -391,13 +355,9 @@ public class TSDBConnectionTest {
}
@Test
public void unwrap() {
try {
TSDBConnection tsdbConnection = conn.unwrap(TSDBConnection.class);
Assert.assertNotNull(tsdbConnection);
} catch (SQLException e) {
e.printStackTrace();
}
public void unwrap() throws SQLException {
TSDBConnection tsdbConnection = conn.unwrap(TSDBConnection.class);
Assert.assertNotNull(tsdbConnection);
}
@Test
......@@ -406,32 +366,22 @@ public class TSDBConnectionTest {
}
@BeforeClass
public static void beforeClass() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/log?user=root&password=taosdata", properties);
// create test database for test cases
try (Statement stmt = conn.createStatement()) {
stmt.execute("create database if not exists test");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
public static void beforeClass() throws SQLException {
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/log?user=root&password=taosdata", properties);
// create test database for test cases
try (Statement stmt = conn.createStatement()) {
stmt.execute("create database if not exists test");
}
}
@AfterClass
public static void afterClass() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
public static void afterClass() throws SQLException {
if (conn != null)
conn.close();
}
}
\ No newline at end of file
package com.taosdata.jdbc.cases;
import org.junit.Test;
import java.sql.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class TaosInfoMonitorTest {
@Test
public void testCreateTooManyConnection() throws ClassNotFoundException {
Class.forName("com.taosdata.jdbc.TSDBDriver");
final String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata";
List<Connection> connectionList = IntStream.range(0, 100).mapToObj(i -> {
try {
TimeUnit.MILLISECONDS.sleep(100);
return DriverManager.getConnection(url);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
connectionList.forEach(conn -> {
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("show databases");
while (rs.next()) {
}
TimeUnit.MILLISECONDS.sleep(100);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
});
connectionList.forEach(conn -> {
try {
conn.close();
TimeUnit.MILLISECONDS.sleep(100);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
});
}
}
package com.taosdata.jdbc.confprops;
import org.junit.*;
import org.junit.runners.MethodSorters;
import java.sql.*;
import java.util.Random;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BatchFetchTest {
private static String host = "127.0.0.1";
private long rowFetchCost, batchFetchCost;
@Test
public void case01_rowFetch() throws SQLException {
String url = "jdbc:TAOS://" + host + ":6030/test?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
boolean batchfetch = Boolean.parseBoolean(conn.getClientInfo("batchfetch"));
Assert.assertFalse(batchfetch);
long start = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery("select * from weather");
while (rs.next()) {
}
long end = System.currentTimeMillis();
rowFetchCost = end - start;
}
}
@Test
public void case02_batchFetch() throws SQLException {
String url = "jdbc:TAOS://" + host + ":6030/test?user=root&password=taosdata&batchfetch=true";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
boolean batchfetch = Boolean.parseBoolean(conn.getClientInfo("batchfetch"));
Assert.assertTrue(batchfetch);
long start = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery("select * from weather");
while (rs.next()) {
}
long end = System.currentTimeMillis();
batchFetchCost = end - start;
}
}
@Test
public void case03_batchFetchFastThanRowFetch() {
Assert.assertTrue(rowFetchCost - batchFetchCost >= 0);
}
@BeforeClass
public static void beforeClass() throws SQLException {
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test");
stmt.execute("create database if not exists test");
stmt.execute("use test");
stmt.execute("create table weather(ts timestamp, f int) tags(t int)");
for (int i = 0; i < 1000; i++) {
stmt.execute(generateSql(100, 100));
}
}
}
private static String generateSql(int tableSize, int valueSize) {
Random random = new Random(System.currentTimeMillis());
StringBuilder builder = new StringBuilder("insert into ");
for (int i = 0; i < tableSize; i++) {
builder.append("t" + i).append(" using weather tags(").append(random.nextInt(100)).append(") values");
for (int j = 0; j < valueSize; j++) {
builder.append(" (now + ").append(i).append("s, ").append(random.nextInt(100)).append(")");
}
}
return builder.toString();
}
}
package com.taosdata.jdbc.confprops;
import com.taosdata.jdbc.TSDBDriver;
import org.junit.Assert;
import org.junit.Test;
import java.sql.*;
import java.util.Properties;
public class CharsetTest {
private static final String host = "127.0.0.1";
@Test
public void test() throws SQLException {
// given
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
try (Connection conn = DriverManager.getConnection(url, props);
Statement stmt = conn.createStatement()) {
// when
stmt.execute("drop database if exists test");
stmt.execute("create database if not exists test");
stmt.execute("use test");
stmt.execute("create table weather(ts timestamp, temperature nchar(10))");
stmt.execute("insert into weather values(now, '北京')");
// then
ResultSet rs = stmt.executeQuery("select * from weather");
while (rs.next()) {
Object value = rs.getObject("temperature");
Assert.assertTrue(value instanceof String);
Assert.assertEquals("北京", value.toString());
}
}
}
}
package com.taosdata.jdbc.confprops;
import org.junit.Assert;
import org.junit.Test;
import java.sql.*;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class TaosInfoMonitorTest {
private static final String host = "127.0.0.1";
private Random random = new Random(System.currentTimeMillis());
@Test
public void testCreateTooManyConnection() throws InterruptedException {
List<Thread> threads = IntStream.range(1, 11).mapToObj(i -> new Thread(() -> {
final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
int connSize = random.nextInt(10);
for (int j = 0; j < connSize; j++) {
try {
Connection conn = DriverManager.getConnection(url);
TimeUnit.MILLISECONDS.sleep(random.nextInt(3000));
int stmtSize = random.nextInt(100);
for (int k = 0; k < stmtSize; k++) {
Statement stmt = conn.createStatement();
TimeUnit.MILLISECONDS.sleep(random.nextInt(3000));
ResultSet rs = stmt.executeQuery("show databases");
while (rs.next()) {
}
rs.close();
stmt.close();
}
} catch (SQLException | InterruptedException throwables) {
Assert.fail();
}
}
}, "thread-" + i)).collect(Collectors.toList());
threads.forEach(Thread::start);
for (Thread thread : threads) {
thread.join();
}
}
}
package com.taosdata.jdbc.cases;
package com.taosdata.jdbc.confprops;
import com.taosdata.jdbc.TSDBDriver;
import org.junit.Test;
......
package com.taosdata.jdbc.confprops;
import com.taosdata.jdbc.TSDBDriver;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.sql.*;
import java.time.Instant;
import java.util.Properties;
public class TimestampFormatTest {
private static final String host = "127.0.0.1";
private long ts = Instant.now().toEpochMilli();
@Test
public void string() throws SQLException {
// given
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
// when
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// then
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals("STRING", actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
String value = rs.getString("ts");
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value);
}
}
}
@Test
public void stringInProperties() throws SQLException {
// given
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
// when
String timestampFormat = "STRING";
Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT, timestampFormat);
try (Connection conn = DriverManager.getConnection(url, props);
Statement stmt = conn.createStatement()) {
// then
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals(timestampFormat, actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
String value = rs.getString("ts");
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value);
}
}
}
@Test
public void timestampInUrl() throws SQLException {
// given
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata&timestampFormat=";
String timestampFormat = "TIMESTAMP";
// when
try (Connection conn = DriverManager.getConnection(url + timestampFormat);
Statement stmt = conn.createStatement()) {
// then
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals(timestampFormat, actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
Object value = rs.getObject("ts");
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value.toString());
}
}
}
@Test
public void timestampInProperties() throws SQLException {
// given
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
String timestampFormat = "TIMESTAMP";
// when
Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT, timestampFormat);
try (Connection conn = DriverManager.getConnection(url, props);
Statement stmt = conn.createStatement()) {
// then
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals(timestampFormat, actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
Object value = rs.getObject("ts");
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value.toString());
}
}
}
@Test
public void utcInUrl() throws SQLException {
// given
String timestampFormat = "UTC";
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata&timestampFormat=" + timestampFormat;
// when & then
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals(timestampFormat, actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
Object value = rs.getObject("ts");
Assert.assertTrue(value instanceof Timestamp);
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value.toString());
}
}
}
@Test
public void utcInProperties() throws SQLException {
// given
String timestampFormat = "UTC";
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
// when
Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT, timestampFormat);
try (Connection conn = DriverManager.getConnection(url, props);
Statement stmt = conn.createStatement()) {
// then
String actual = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT);
Assert.assertEquals(timestampFormat, actual);
ResultSet rs = stmt.executeQuery("select * from test.weather");
while (rs.next()) {
Object value = rs.getObject("ts");
Assert.assertTrue(value instanceof Timestamp);
String expect = new Timestamp(ts).toString();
Assert.assertEquals(expect, value.toString());
}
}
}
@Before
public void before() throws SQLException {
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test");
stmt.execute("create database if not exists test");
stmt.execute("use test");
stmt.execute("create table weather(ts timestamp, temperature nchar(10))");
stmt.execute("insert into weather values(" + ts + ", '北京')");
}
}
}
......@@ -1085,30 +1085,26 @@ public class RestfulDatabaseMetaDataTest {
}
@BeforeClass
public static void beforeClass() {
try {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection(url, properties);
metaData = connection.getMetaData().unwrap(RestfulDatabaseMetaData.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
public static void beforeClass() throws SQLException {
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection(url, properties);
Statement stmt = connection.createStatement();
stmt.execute("drop database if exists log");
stmt.execute("create database if not exists log precision 'us'");
stmt.execute("use log");
stmt.execute("create table `dn` (ts TIMESTAMP,cpu_taosd FLOAT,cpu_system FLOAT,cpu_cores INT,mem_taosd FLOAT,mem_system FLOAT,mem_total INT,disk_used FLOAT,disk_total INT,band_speed FLOAT,io_read FLOAT,io_write FLOAT,req_http INT,req_select INT,req_insert INT) TAGS (dnodeid INT,fqdn BINARY(128))");
stmt.execute("insert into dn1 using dn tags(1,'a') (ts) values(now)");
metaData = connection.getMetaData().unwrap(RestfulDatabaseMetaData.class);
}
@AfterClass
public static void afterClass() {
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
public static void afterClass() throws SQLException {
if (connection != null)
connection.close();
}
}
\ No newline at end of file
......@@ -543,15 +543,6 @@ public class SQLTest {
Assert.assertNotNull(rs);
}
@Test
public void testCase053() {
String sql = "select avg(cpu_taosd), avg(cpu_system), max(cpu_cores), avg(mem_taosd), avg(mem_system), max(mem_total), avg(disk_used), max(disk_total), avg(band_speed), avg(io_read), avg(io_write), sum(req_http), sum(req_select), sum(req_insert) from log.dn1 where ts> now - 60m and ts<= now interval(1m) fill(value, 0)";
// when
ResultSet rs = executeQuery(connection, sql);
// then
Assert.assertNotNull(rs);
}
private boolean execute(Connection connection, String sql) {
try (Statement statement = connection.createStatement()) {
return statement.execute(sql);
......
......@@ -17,7 +17,6 @@ public class HttpClientPoolUtilTest {
String user = "root";
String password = "taosdata";
String host = "127.0.0.1";
// String host = "master";
@Test
public void useLog() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册