Changeset: dbf6be90023a for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/dbf6be90023a
Modified Files:
        src/test/java/JDBC_API_Tester.java
        src/test/java/org/monetdb/jdbc/ApiTests.java
Branch: default
Log Message:

Convert BugConcurrent_sequences() -> ApiTests#testBugConcurrentSequences()


diffs (213 lines):

diff --git a/src/test/java/JDBC_API_Tester.java 
b/src/test/java/JDBC_API_Tester.java
--- a/src/test/java/JDBC_API_Tester.java
+++ b/src/test/java/JDBC_API_Tester.java
@@ -2113,170 +2113,6 @@ public final class JDBC_API_Tester exten
                                "Cleanup TABLE t1504657\n");
        }
  
-       @Test public void BugConcurrent_sequences() {
-               sb.setLength(0);        // clear the output log buffer
-
-               Connection con1 = null, con2 = null;
-               Statement stmt1 = null, stmt2 = null;
-               ResultSet rs1 = null, rs2 = null;
-               try {
-                       con1 = DriverManager.getConnection(jdbcUrl);
-                       con2 = DriverManager.getConnection(jdbcUrl);
-                       stmt1 = con1.createStatement();
-                       stmt2 = con2.createStatement();
-
-                       // >> true: auto commit should be on by default
-                       sb.append("0. 
true\t").append(con1.getAutoCommit()).append("\n");
-                       sb.append("0. 
true\t").append(con2.getAutoCommit()).append("\n");
-
-                       // create a table
-                       sb.append("1. create table tconc_seq using client 1... 
");
-                       stmt1.executeUpdate("DROP TABLE IF EXISTS tconc_seq; 
CREATE TABLE tconc_seq ( id serial, who varchar(12) )");
-                       sb.append("passed :)\n");
-
-                       // test the insertion of values with concurrent clients
-                       sb.append("2. insert into tconc_seq using client 1 and 
2... ");
-                       stmt1.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client1')");
-                       sb.append("client 1 passed :)\n");
-
-                       con2.setAutoCommit(false);
-                       stmt2.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client2')");
-                       sb.append("transaction on client 2 :)\n");
-
-                       stmt1.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client1')");
-                       sb.append("client 1 passed :)\n");
-
-                       try {
-                               con2.commit();
-                               sb.append("transaction client 2 passed :)\n");
-                       } catch (SQLException e) {
-                               sb.append("transaction client 2 failed!\n");
-                       }
-                       con2.setAutoCommit(true);
-                       stmt2.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client2')");
-                       sb.append("passed :)\n");
-
-                       sb.append("2.1. check table status with client 1...\n");
-                       rs1 = stmt1.executeQuery("SELECT * FROM tconc_seq ORDER 
BY id");
-                       while (rs1.next())
-                               sb.append(rs1.getInt("id")).append(", 
").append(rs1.getString("who")).append("\n");
-                       sb.append("passed :)\n");
-
-                       sb.append("2.2. check table status with client 2...\n");
-                       rs2 = stmt2.executeQuery("SELECT * FROM tconc_seq ORDER 
BY id");
-                       while (rs2.next())
-                               sb.append(rs2.getInt("id")).append(", 
").append(rs2.getString("who")).append("\n");
-                       sb.append("passed :)\n");
-
-                       // drop the table (not dropping the sequence) from 
client 1
-                       sb.append("3.1. drop table tconc_seq using client 1... 
");
-                       stmt1.executeUpdate("DROP TABLE tconc_seq");
-                       sb.append("passed :)\n");
-
-                       sb.append("3.1. recreate tconc_seq using client 1... ");
-                       stmt1.executeUpdate("CREATE TABLE tconc_seq ( id 
serial, who varchar(12) )");
-                       sb.append("passed :)\n");
-               } catch (SQLException e) {
-                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
-               }
-
-               compareExpectedOutput("BugConcurrent_sequences",
-                               "0. true        true\n" +
-                               "0. true        true\n" +
-                               "1. create table tconc_seq using client 1... 
passed :)\n" +
-                               "2. insert into tconc_seq using client 1 and 
2... client 1 passed :)\n" +
-                               "transaction on client 2 :)\n" +
-                               "client 1 passed :)\n" +
-                               "transaction client 2 failed!\n" +
-                               "passed :)\n" +
-                               "2.1. check table status with client 1...\n" +
-                               "1, client1\n" +
-                               "3, client1\n" +
-                               "4, client2\n" +
-                               "passed :)\n" +
-                               "2.2. check table status with client 2...\n" +
-                               "1, client1\n" +
-                               "3, client1\n" +
-                               "4, client2\n" +
-                               "passed :)\n" +
-                               "3.1. drop table tconc_seq using client 1... 
passed :)\n" +
-                               "3.1. recreate tconc_seq using client 1... 
passed :)\n");
-               sb.setLength(0);        // clear the output log buffer
-
-               try {
-                       // re-establish connection
-                       sb.append("x. Reconnecting client 1 and 2... ");
-                       con1.close();
-                       con2.close();
-                       con1 = DriverManager.getConnection(jdbcUrl);
-                       con2 = DriverManager.getConnection(jdbcUrl);
-                       stmt1 = con1.createStatement();
-                       stmt2 = con2.createStatement();
-                       sb.append("passed :)\n");
-
-                       // insert and print, should get 1,2
-                       sb.append("4. insert into tconc_seq using client 1 and 
2...\n");
-                       stmt1.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client1')");
-                       sb.append("passed :)\n");
-                       con2.setAutoCommit(false);
-                       stmt2.executeUpdate("INSERT INTO tconc_seq(who) 
VALUES('client2')");
-                       con2.commit();
-                       con2.setAutoCommit(true);
-                       sb.append("passed :)\n");
-
-                       sb.append("4.1. check table status with client 1...\n");
-                       rs1 = stmt1.executeQuery("SELECT * FROM tconc_seq ORDER 
BY who");
-                       for (int cntr = 1; rs1.next(); cntr++) {
-                               int id = rs1.getInt("id");
-                               sb.append(id).append(", 
").append(rs1.getString("who")).append("\n");
-                               if (id != cntr)
-                                       sb.append("!! expected 
").append(cntr).append(", got ").append(id);
-                       }
-                       sb.append("passed :)\n");
-
-                       sb.append("4.2. check table status with client 2...\n");
-                       rs2 = stmt2.executeQuery("SELECT * FROM tconc_seq ORDER 
BY who");
-                       for (int cntr = 1; rs2.next(); cntr++) {
-                               int id = rs2.getInt("id");
-                               sb.append(id).append(", 
").append(rs2.getString("who")).append("\n");
-                               if (id != cntr)
-                                       sb.append("!! expected 
").append(cntr).append(", got ").append(id);
-                       }
-                       sb.append("passed :)\n");
-               } catch (SQLException e) {
-                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
-               }
-
-               // cleanup
-               try {
-                       sb.append("Cleanup TABLE tconc_seq\n");
-                       stmt2.executeUpdate("DROP TABLE tconc_seq");
-               } catch (SQLException e) {
-                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
-               }
-
-               closeStmtResSet(stmt1, rs1);
-               closeStmtResSet(stmt2, rs2);
-
-               closeConx(con2);
-               closeConx(con1);
-
-               compareExpectedOutput("BugConcurrent_sequences",
-                               "x. Reconnecting client 1 and 2... passed :)\n" 
+
-                               "4. insert into tconc_seq using client 1 and 
2...\n" +
-                               "passed :)\n" +
-                               "passed :)\n" +
-                               "4.1. check table status with client 1...\n" +
-                               "1, client1\n" +
-                               "2, client2\n" +
-                               "passed :)\n" +
-                               "4.2. check table status with client 2...\n" +
-                               "1, client1\n" +
-                               "2, client2\n" +
-                               "passed :)\n" +
-                               "Cleanup TABLE tconc_seq\n");
-       }
-
        @Test    // CHANGED
        public void Bug_Connect_as_voc_getMetaData_Failure_Bug_6388() {
                sb.setLength(0);        // clear the output log buffer
diff --git a/src/test/java/org/monetdb/jdbc/ApiTests.java 
b/src/test/java/org/monetdb/jdbc/ApiTests.java
--- a/src/test/java/org/monetdb/jdbc/ApiTests.java
+++ b/src/test/java/org/monetdb/jdbc/ApiTests.java
@@ -1798,4 +1798,35 @@ public class ApiTests {
                        assertEquals("monetdb monet mon monetdb monet mon 
monetdb monet mon", concatenateColumn(conn3," ", "SELECT name FROM t1504657"));
                }
        }
+
+       @Test
+       public void testBugConcurrentSequences() throws SQLException {
+               stmt.execute("DROP TABLE IF EXISTS tconc_seq");
+
+               try (
+                               Connection conn1 = newConnection();
+                               Connection conn2 = newConnection();
+                       Statement stmt1 = conn1.createStatement();
+                               Statement stmt2 = conn2.createStatement();
+               ) {
+                       stmt1.execute("CREATE TABLE tconc_seq(id SERIAL, who 
VARCHAR(12))");
+
+                       stmt1.execute("INSERT INTO tconc_seq(who) VALUES 
('client1')");
+
+                       conn2.setAutoCommit(false);
+                       stmt2.execute("INSERT INTO tconc_seq(who) VALUES 
('client2')");
+
+                       stmt1.execute("INSERT INTO tconc_seq(who) VALUES 
('client1')");
+
+                       // Committing conn2 fails, somehow conn1 and conn2 
fight over the primary key index
+                       SQLException sqle = assertThrows(SQLException.class, 
conn2::commit);
+                       assertTrue(sqle.getMessage().contains("concurrency 
conflicts"), sqle.getMessage());
+
+                       conn2.setAutoCommit(true);
+                       stmt2.execute("INSERT INTO tconc_seq(who) VALUES 
('client2')");
+
+                       assertEquals("1client1 3client1 4client2", 
concatenateColumn(conn1, " ", "SELECT id || who FROM tconc_seq ORDER BY id"));
+                       assertEquals("1client1 3client1 4client2", 
concatenateColumn(conn1, " ", "SELECT id || who FROM tconc_seq ORDER BY id"));
+               }
+       }
 }
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to