End test with a select all 44/91944/2
authorTschaen, Brendan <ctschaen@att.com>
Wed, 24 Jul 2019 13:43:23 +0000 (09:43 -0400)
committerTschaen, Brendan <ctschaen@att.com>
Thu, 25 Jul 2019 18:20:50 +0000 (14:20 -0400)
Issue-ID: MUSIC-420
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
Change-Id: I8abb83b81fbbf976842f244111eb939d7e077152

mdbc-server/src/main/java/org/onap/music/mdbc/examples/MdbcTestMultiClient.java

index 2923cd7..02b7c7c 100755 (executable)
@@ -49,6 +49,8 @@ public class MdbcTestMultiClient implements Runnable {
        private int selectInsteadOfUpdatePct = 25;\r
        private int rollbackChancePct = 15;\r
        private int maxTables = 0;\r
+    public static boolean[] threadsDone;\r
+\r
 \r
        private boolean sequentialIds = false;\r
        private static Integer currentId = -1;\r
@@ -62,6 +64,7 @@ public class MdbcTestMultiClient implements Runnable {
     private static final List<String> defaultTableNames = Arrays.asList(new String[] {"persons", "persons2"});\r
 \r
        private boolean explainConnection = true;\r
+       private boolean endInSelect = true;\r
        \r
     public static class Employee {\r
         public final int empid;\r
@@ -716,7 +719,44 @@ public class MdbcTestMultiClient implements Runnable {
                \r
                doLog("");\r
         }\r
+        threadsDone[threadId] = true;\r
 \r
+        if (endInSelect) {\r
+            doLog("Ending in select to ensure all db's are in sync");\r
+            while (!allThreadsDone()) {\r
+                try {\r
+                    Thread.sleep(1000);\r
+                } catch (InterruptedException e) {\r
+                    continue;\r
+                }\r
+            }\r
+            doLog("All threads are done. Ending in select");\r
+            if (connection==null) {\r
+                try {\r
+                    doLog("Opening new connection");\r
+                    connection = DriverManager.getConnection(connectionString);\r
+                    connection.setAutoCommit(false);\r
+                } catch (SQLException e) {\r
+                    e.printStackTrace();\r
+                    return;\r
+                }\r
+            }\r
+            for (String tableName : tableNames) {\r
+                try {\r
+                    Statement querySt = connection.createStatement();\r
+                    ResultSet rs = executeQueryTimed("select * from " + tableName, querySt, false);\r
+                    while (rs.next()) {\r
+                        //              doLog("PersonId = " + rs.getInt("personId") + ", lastname = " + rs.getString("lastname") + ", firstname = " + rs.getString("firstname"));\r
+                        Employee emp = new Employee(rs.getInt("personId"), rs.getString("lastname"), rs.getString("firstname"), rs.getString("address"), rs.getString("city"));\r
+                        doLog("Found: " + emp);\r
+                    }\r
+                    querySt.close();\r
+                } catch (SQLException e) {\r
+                    e.printStackTrace();\r
+                }\r
+            }\r
+        }\r
+        \r
         if (connection!=null) {\r
                try {\r
                        doLog("Closing connection at end");\r
@@ -729,27 +769,39 @@ public class MdbcTestMultiClient implements Runnable {
         doLog("All done.");\r
     }\r
 \r
-       private void doLog(String string) {\r
+       private boolean allThreadsDone() {\r
+        for (Boolean b: this.threadsDone) {\r
+            if (!b) {\r
+                return false;\r
+            }\r
+        }\r
+        return true;\r
+    }\r
+\r
+    private void doLog(String string) {\r
                System.out.println(">> Thread " + threadId + " " + sdf.format(new java.util.Date()) + " >> " + string);\r
        }\r
        \r
-       public static void main(String[] args) {\r
+       public static void main(String[] args) throws InterruptedException {\r
                MdbcTestMultiClient mtc = new MdbcTestMultiClient(args);\r
                mtc.runTests(); \r
        }\r
 \r
-    private void runTests() {\r
+    private void runTests() throws InterruptedException {\r
        if (randomSeed==null) {\r
                randomSeed = new Random().nextLong();\r
        }\r
                doLog("Using random seed = " + randomSeed);\r
        Random seedRandom = new Random(randomSeed);\r
+       this.threadsDone = new boolean[connectionStrings.size()];\r
+\r
         for (int i=0; i<connectionStrings.size(); i++) {\r
             MdbcTestMultiClient mt = new MdbcTestMultiClient(this, i);\r
             mt.setRandomSeed(seedRandom.nextLong());\r
             Thread t = new Thread(mt);\r
             t.start();\r
         }\r
+        \r
     }\r
                \r
 }\r