提交 78fb4b5b 编写于 作者: Z zyyang

[TD-1690]<fix>: invalid result set pointer test cases

上级 98624f57
...@@ -112,9 +112,16 @@ ...@@ -112,9 +112,16 @@
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version> <version>2.12.4</version>
<configuration> <configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<!-- <exclude>**/InvalidResultPointerExceptionTest.java</exclude>-->
<exclude>**/FailOverTest.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore> <testFailureIgnore>true</testFailureIgnore>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
...@@ -13,6 +13,7 @@ public class RestfulStatement implements Statement { ...@@ -13,6 +13,7 @@ public class RestfulStatement implements Statement {
private final String catalog; private final String catalog;
private final RestfulConnection conn; private final RestfulConnection conn;
private boolean isClosed = false;
public RestfulStatement(RestfulConnection c, String catalog) { public RestfulStatement(RestfulConnection c, String catalog) {
this.conn = c; this.conn = c;
...@@ -21,8 +22,7 @@ public class RestfulStatement implements Statement { ...@@ -21,8 +22,7 @@ public class RestfulStatement implements Statement {
@Override @Override
public ResultSet executeQuery(String sql) throws SQLException { public ResultSet executeQuery(String sql) throws SQLException {
final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
final String url = "http://" + conn.getHost() + ":"+conn.getPort()+"/rest/sql";
String result = HttpClientPoolUtil.execute(url, sql); String result = HttpClientPoolUtil.execute(url, sql);
String fields = ""; String fields = "";
...@@ -65,17 +65,24 @@ public class RestfulStatement implements Statement { ...@@ -65,17 +65,24 @@ public class RestfulStatement implements Statement {
@Override @Override
public int executeUpdate(String sql) throws SQLException { public int executeUpdate(String sql) throws SQLException {
return 0; String result = HttpClientPoolUtil.execute(conn.getUrl(), sql);
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject.getString("status").equals("error")) {
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + jsonObject.getString("desc") + "\n" +
"error code: " + jsonObject.getString("code")));
}
return Integer.parseInt(jsonObject.getString("rows"));
} }
@Override @Override
public void close() throws SQLException { public void close() throws SQLException {
isClosed = true;
} }
@Override @Override
public int getMaxFieldSize() throws SQLException { public int getMaxFieldSize() throws SQLException {
return 0; //TODO:
throw new SQLFeatureNotSupportedException();
} }
@Override @Override
...@@ -130,7 +137,11 @@ public class RestfulStatement implements Statement { ...@@ -130,7 +137,11 @@ public class RestfulStatement implements Statement {
@Override @Override
public boolean execute(String sql) throws SQLException { public boolean execute(String sql) throws SQLException {
return false; if (isClosed) {
throw new SQLException("Invalid method call on a closed statement.");
}
HttpClientPoolUtil.execute(conn.getUrl(), sql);
return true;
} }
@Override @Override
......
import com.taosdata.jdbc.TSDBDriver;
import com.taosdata.jdbc.TSDBPreparedStatement;
import java.sql.*;
import java.util.Properties;
public class TestPreparedStatement {
public static void main(String[] args) throws SQLException {
Connection connection = null;
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/", properties);
String rawSql = "select * from test.log0601";
// String[] params = new String[]{"ts", "c1"};
PreparedStatement pstmt = (TSDBPreparedStatement) connection.prepareStatement(rawSql);
ResultSet resSet = pstmt.executeQuery();
while(resSet.next()) {
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {
System.out.printf("%d: %s \n", i, resSet.getString(i));
}
}
resSet.close();
pstmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
if (null != connection) {
connection.close();
}
}
}
}
import com.taosdata.jdbc.TSDBDriver;
import java.sql.*;
import java.util.Properties;
public class TestTSDBDatabaseMetaData {
public static void main(String[] args) throws SQLException {
Connection connection = null;
DatabaseMetaData dbMetaData = null;
ResultSet resSet = null;
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/", properties);
dbMetaData = connection.getMetaData();
resSet = dbMetaData.getCatalogs();
while(resSet.next()) {
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {
System.out.printf("dbMetaData.getCatalogs(%d) = %s\n", i, resSet.getString(i));
}
}
resSet.close();
} catch (Exception e) {
e.printStackTrace();
if (null != connection) {
connection.close();
}
}
}
}
import com.taosdata.jdbc.TSDBConnection;
import com.taosdata.jdbc.TSDBDriver;
import com.taosdata.jdbc.TSDBResultSet;
import com.taosdata.jdbc.TSDBSubscribe;
import java.sql.DriverManager;
import java.util.Properties;
public class TestTSDBSubscribe {
public static TSDBConnection connectTDengine(String host, String database) throws Exception {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
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");
String cs = String.format("jdbc:TAOS://%s:0/%s", host, database);
return (TSDBConnection)DriverManager.getConnection(cs, properties);
}
public static void main(String[] args) throws Exception {
String usage = "java -Djava.ext.dirs=../ TestTSDBSubscribe [-host host] <-db database> <-topic topic> <-sql sql>";
if (args.length < 2) {
System.err.println(usage);
return;
}
String host = "localhost", database = "", topic = "", sql = "";
for (int i = 0; i < args.length; i++) {
if ("-db".equalsIgnoreCase(args[i]) && i < args.length - 1) {
database = args[++i];
}
if ("-topic".equalsIgnoreCase(args[i]) && i < args.length - 1) {
topic = args[++i];
}
if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) {
host = args[++i];
}
if ("-sql".equalsIgnoreCase(args[i]) && i < args.length - 1) {
sql = args[++i];
}
}
if (database.isEmpty() || topic.isEmpty() || sql.isEmpty()) {
System.err.println(usage);
return;
}
TSDBConnection connection = null;
TSDBSubscribe sub = null;
try {
connection = connectTDengine(host, database);
sub = ((TSDBConnection) connection).subscribe(topic, sql, false);
int total = 0;
while(true) {
TSDBResultSet rs = sub.consume();
int count = 0;
while(rs.next()) {
count++;
}
total += count;
System.out.printf("%d rows consumed, total %d\n", count, total);
Thread.sleep(900);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != sub) {
sub.close(true);
}
if (null != connection) {
connection.close();
}
}
}
}
...@@ -7,6 +7,9 @@ import java.text.SimpleDateFormat; ...@@ -7,6 +7,9 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/**
* for TD-1590
***/
public class FailOverTest { public class FailOverTest {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
...@@ -16,8 +19,7 @@ public class FailOverTest { ...@@ -16,8 +19,7 @@ public class FailOverTest {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
final String url = "jdbc:TAOS://:/?user=root&password=taosdata"; final String url = "jdbc:TAOS://:/?user=root&password=taosdata";
long end = System.currentTimeMillis() + 1000 * 60 * 5; while (true) {
while (System.currentTimeMillis() < end) {
try (Connection conn = DriverManager.getConnection(url)) { try (Connection conn = DriverManager.getConnection(url)) {
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("select server_status()"); ResultSet resultSet = stmt.executeQuery("select server_status()");
...@@ -30,7 +32,6 @@ public class FailOverTest { ...@@ -30,7 +32,6 @@ public class FailOverTest {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import com.taosdata.jdbc.util.TSDBCommon; import com.taosdata.jdbc.util.TSDBCommon;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -28,15 +27,15 @@ public class InvalidResultPointerExceptionTest { ...@@ -28,15 +27,15 @@ public class InvalidResultPointerExceptionTest {
} }
@Test @Test
public void testInvalidResultPointerException() { public void testUseSameConnection() {
try (Connection conn = TSDBCommon.getConn("localhost")) { try (Connection conn = TSDBCommon.getConn("localhost")) {
List<Thread> threads = IntStream.range(1, 2).boxed().map(i -> new Thread(() -> { List<Thread> threads = IntStream.range(0, 2).boxed().map(i -> new Thread(() -> {
while (true) { while (true) {
String sql = "insert into irp_test.weather values(now, " + random.nextInt(100) + ")"; String sql = "insert into irp_test.weather values(now, " + random.nextInt(100) + ")";
try { try {
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute(sql); stmt.executeUpdate(sql);
stmt.close(); stmt.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -63,7 +62,44 @@ public class InvalidResultPointerExceptionTest { ...@@ -63,7 +62,44 @@ public class InvalidResultPointerExceptionTest {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
@Test
public void testUseSameStatement() {
try (Connection conn = TSDBCommon.getConn("localhost")) {
Statement stmt = conn.createStatement();
List<Thread> threads = IntStream.range(0, 2).boxed().map(i -> new Thread(() -> {
while (true) {
String sql = "insert into irp_test.weather values(now, " + random.nextInt(100) + ")";
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " >>> " + sql);
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "Thread-" + i)).collect(Collectors.toList());
for (Thread thread : threads)
thread.start();
for (Thread thread : threads) {
thread.join();
}
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }
...@@ -7,29 +7,6 @@ import java.sql.*; ...@@ -7,29 +7,6 @@ import java.sql.*;
public class RestfulDriverTest { public class RestfulDriverTest {
@Test
public void testCase001() {
try {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
Connection connection = DriverManager.getConnection("jdbc:TAOS-RS://master:6041/?user=root&password=taosdata");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from log.log");
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String column = metaData.getColumnLabel(i);
String value = resultSet.getString(i);
System.out.print(column + ":" + value + "\t");
}
System.out.println();
}
statement.close();
connection.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test @Test
public void testAcceptUrl() throws SQLException { public void testAcceptUrl() throws SQLException {
Driver driver = new RestfulDriver(); Driver driver = new RestfulDriver();
...@@ -37,4 +14,9 @@ public class RestfulDriverTest { ...@@ -37,4 +14,9 @@ public class RestfulDriverTest {
Assert.assertTrue(isAccept); Assert.assertTrue(isAccept);
} }
@Test
public void testGetPropertyInfo() throws SQLException {
}
} }
package com.taosdata.jdbc.rs;
import com.taosdata.jdbc.util.RestfulJDBCCommon;
import org.junit.Test;
import java.sql.*;
public class RestfulJdbcTest {
@Test
public void testCase001() {
try (Connection conn = RestfulJDBCCommon.getConnection("master")) {
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from log.log");
ResultSetMetaData metaData = resultSet.getMetaData();
int total = 0;
while (resultSet.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String column = metaData.getColumnLabel(i);
String value = resultSet.getString(i);
System.out.print(column + ":" + value + "\t");
}
total++;
System.out.println();
}
System.out.println("total : " + total);
statement.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testCase002() {
try (Connection conn = RestfulJDBCCommon.getConnection("master")) {
RestfulJDBCCommon.createDatabase(conn, "restful_test");
RestfulJDBCCommon.createSuperTable(conn);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package com.taosdata.jdbc.util;
import com.taosdata.jdbc.TSDBDriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class RestfulJDBCCommon {
public static Connection getConnection(String host) throws ClassNotFoundException, SQLException {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
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");
return DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
}
public static void createDatabase(Connection conn, String dbName) throws SQLException {
Statement stmt = conn.createStatement();
stmt.execute("drop database if exists " + dbName);
stmt.execute("create database if not exists " + dbName);
stmt.execute("use " + dbName);
stmt.close();
}
public static void createSuperTable(Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
Map<String, String> fieldsMeta = new LinkedHashMap<>();
fieldsMeta.put("ts", "timestamp");
fieldsMeta.put("temperature", "float");
fieldsMeta.put("humidity", "int");
Map<String, String> tagsMeta = new LinkedHashMap<>();
tagsMeta.put("location", "nchar");
tagsMeta.put("tableId", "int");
String sql = SqlSpeller.createSuperTable("weather", fieldsMeta, tagsMeta);
stmt.executeUpdate(sql);
stmt.close();
}
}
package com.taosdata.jdbc.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class SqlSpeller {
public static String createSuperTable(String stbname, Map<String, String> fieldsMeta, Map<String, String> tagsMeta) {
StringBuilder sql = new StringBuilder();
sql.append("create table " + stbname + " (");
Iterator<String> keyIterator = fieldsMeta.keySet().iterator();
for (int i = 0; keyIterator.hasNext(); i++) {
String key = keyIterator.next();
if (i == 0)
sql.append(key + " " + fieldsMeta.get(key));
else
sql.append(", " + key + " " + fieldsMeta.get(key));
}
sql.append(") tags(");
keyIterator = tagsMeta.keySet().iterator();
for (int i = 0; keyIterator.hasNext(); i++) {
String key = keyIterator.next();
if (i == 0)
sql.append(key + " " + tagsMeta.get(key));
else
sql.append(", " + key + " " + tagsMeta.get(key));
}
sql.append(")");
return sql.toString();
}
public static void main(String[] args) {
Map<String, String> fieldsMeta = new LinkedHashMap<>();
fieldsMeta.put("ts", "timestamp");
fieldsMeta.put("temperature", "float");
fieldsMeta.put("humidity", "int");
Map<String, String> tagsMeta = new LinkedHashMap<>();
tagsMeta.put("location", "nchar(64)");
tagsMeta.put("tableId", "int");
String sql = SqlSpeller.createSuperTable("weather", fieldsMeta, tagsMeta);
System.out.println(sql);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册