Unverified 提交 4801c8ba authored 作者: Andrei Tokar's avatar Andrei Tokar 提交者: GitHub

Merge pull request #1175 from h2database/stop-ignoring-exceptions

tighten test conditions - do not ignore any exceptions
...@@ -8,10 +8,8 @@ package org.h2.test.synth; ...@@ -8,10 +8,8 @@ package org.h2.test.synth;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Random; import java.util.Random;
import org.h2.api.ErrorCode;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.Task; import org.h2.util.Task;
...@@ -29,19 +27,26 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -29,19 +27,26 @@ public class TestConcurrentUpdate extends TestBase {
* @param a ignored * @param a ignored
*/ */
public static void main(String... a) throws Exception { public static void main(String... a) throws Exception {
TestBase t = TestBase.createCaller().init(); org.h2.test.TestAll config = new org.h2.test.TestAll();
t.config.memory = true; config.memory = true;
t.test(); config.multiThreaded = true;
// config.mvStore = false;
// config.mvcc = false;
System.out.println(config);
TestBase test = createCaller().init(config);
for (int i = 0; i < 10; i++) {
System.out.println("Pass #" + i);
test.config.beforeTest();
test.test();
test.config.afterTest();
}
} }
@Override @Override
public void test() throws Exception { public void test() throws Exception {
if (!config.multiThreaded) {
return;
}
deleteDb("concurrent"); deleteDb("concurrent");
final String url = getURL("concurrent", true); final String url = getURL("concurrent", true);
Connection conn = getConnection(url); try (Connection conn = getConnection(url)) {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)"); stat.execute("create table test(id int primary key, name varchar)");
...@@ -52,9 +57,9 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -52,9 +57,9 @@ public class TestConcurrentUpdate extends TestBase {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
Random r = new Random(threadId); Random r = new Random(threadId);
Connection conn = getConnection(url); try (Connection conn = getConnection(url)) {
PreparedStatement insert = conn.prepareStatement( PreparedStatement insert = conn.prepareStatement(
"insert into test values(?, ?)"); "merge into test values(?, ?)");
PreparedStatement update = conn.prepareStatement( PreparedStatement update = conn.prepareStatement(
"update test set name = ? where id = ?"); "update test set name = ? where id = ?");
PreparedStatement delete = conn.prepareStatement( PreparedStatement delete = conn.prepareStatement(
...@@ -62,10 +67,9 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -62,10 +67,9 @@ public class TestConcurrentUpdate extends TestBase {
PreparedStatement select = conn.prepareStatement( PreparedStatement select = conn.prepareStatement(
"select * from test where id = ?"); "select * from test where id = ?");
while (!stop) { while (!stop) {
try {
int x = r.nextInt(ROW_COUNT); int x = r.nextInt(ROW_COUNT);
String data = "x" + r.nextInt(ROW_COUNT); String data = "x" + r.nextInt(ROW_COUNT);
switch (r.nextInt(3)) { switch (r.nextInt(4)) {
case 0: case 0:
insert.setInt(1, x); insert.setInt(1, x);
insert.setString(2, data); insert.setString(2, data);
...@@ -80,7 +84,7 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -80,7 +84,7 @@ public class TestConcurrentUpdate extends TestBase {
delete.setInt(1, x); delete.setInt(1, x);
delete.execute(); delete.execute();
break; break;
case 4: case 3:
select.setInt(1, x); select.setInt(1, x);
ResultSet rs = select.executeQuery(); ResultSet rs = select.executeQuery();
while (rs.next()) { while (rs.next()) {
...@@ -88,13 +92,9 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -88,13 +92,9 @@ public class TestConcurrentUpdate extends TestBase {
} }
break; break;
} }
} catch (SQLException e) {
handleException(e);
} }
} }
conn.close();
} }
}; };
tasks[i] = t; tasks[i] = t;
t.execute(); t.execute();
...@@ -112,24 +112,6 @@ public class TestConcurrentUpdate extends TestBase { ...@@ -112,24 +112,6 @@ public class TestConcurrentUpdate extends TestBase {
for (Task t : tasks) { for (Task t : tasks) {
t.get(); t.get();
} }
conn.close();
}
/**
* Handle or ignore the exception.
*
* @param e the exception
*/
void handleException(SQLException e) throws SQLException {
switch (e.getErrorCode()) {
case ErrorCode.CONCURRENT_UPDATE_1:
case ErrorCode.DUPLICATE_KEY_1:
case ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1:
case ErrorCode.LOCK_TIMEOUT_1:
break;
default:
throw e;
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论