db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r674412 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi: StressMulti10x1.java StressMulti50x59.java StressMultiTest.java
Date Mon, 07 Jul 2008 07:49:31 GMT
Author: kristwaa
Date: Mon Jul  7 00:49:31 2008
New Revision: 674412

URL: http://svn.apache.org/viewvc?rev=674412&view=rev
Log:
DERBY-1764 (partial): Rewrite stress.multi as a JUnit test.
Fixed class name in license header.
Formatting fixes (removed tabs and trailing spaces, a few fixes of indentation level).
Patch file: derby-1764-3a-whitespace_changes.diff

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti10x1.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti50x59.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMultiTest.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti10x1.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti10x1.java?rev=674412&r1=674411&r2=674412&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti10x1.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti10x1.java
Mon Jul  7 00:49:31 2008
@@ -1,7 +1,7 @@
 /*
 
-   Derby - Class 
-   org.apache.derbyTesting.functionTests.tests.multi.StressMulti50x59
+   Derby - Class
+   org.apache.derbyTesting.functionTests.tests.multi.StressMulti10x1
 
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -27,11 +27,11 @@
 
 /**
  * This test runs the StressMultiTest with 10 threads for 1 minute.
- * 
+ *
  */
 public class StressMulti10x1 extends TestCase {
-	
-	public static Test suite() {
-		return StressMultiTest.suite(10,1);
-	}
+
+    public static Test suite() {
+        return StressMultiTest.suite(10,1);
+    }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti50x59.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti50x59.java?rev=674412&r1=674411&r2=674412&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti50x59.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMulti50x59.java
Mon Jul  7 00:49:31 2008
@@ -1,6 +1,6 @@
 /*
 
-   Derby - Class 
+   Derby - Class
    org.apache.derbyTesting.functionTests.tests.multi.StressMulti50x59
 
    Licensed to the Apache Software Foundation (ASF) under one or more
@@ -30,8 +30,8 @@
  *
  */
 public class StressMulti50x59 extends TestCase {
-	
-	public static Test suite() {
-		return StressMultiTest.suite(50,59);
-	}
+
+    public static Test suite() {
+        return StressMultiTest.suite(50,59);
+    }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMultiTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMultiTest.java?rev=674412&r1=674411&r2=674412&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMultiTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/multi/StressMultiTest.java
Mon Jul  7 00:49:31 2008
@@ -1,6 +1,6 @@
 /*
 
-   Derby - Class 
+   Derby - Class
    org.apache.derbyTesting.functionTests.tests.multi.StressMultiTest
 
    Licensed to the Apache Software Foundation (ASF) under one or more
@@ -41,157 +41,157 @@
 import org.apache.derbyTesting.junit.TestConfiguration;
 
 /**
- * Stresstest running any number of threads performing "random" operations on 
+ * Stresstest running any number of threads performing "random" operations on
  * a database for any number of minutes. Default values are 10 threads for 10
  * minutes. The operations are create, select, insert, update and rollback.
- * 
- * To test with a different number of threads and minutes set up a suite by 
- * calling getSuite(int threads, int minutes). See StressMulti50x59.java for 
+ *
+ * To test with a different number of threads and minutes set up a suite by
+ * calling getSuite(int threads, int minutes). See StressMulti50x59.java for
  * an example.
  */
 public class StressMultiTest extends BaseJDBCTestCase {
-    
-	private static int THREADS = 10; //Default number of threads we will run.
+
+    private static int THREADS = 10; //Default number of threads we will run.
     private static int MINUTES = 10; //Default number of minutes we will run.
-	private static boolean DEBUG = false; //Force verbosity, used for debugging.
-	
+    private static boolean DEBUG = false; //Force verbosity, used for debugging.
+
     private Thread threads[] = null;
     private TestResult testResult = null;
     private Random rnd = new Random();
     private boolean complete = false;
-    
+
     public StressMultiTest(String s) {
         super(s);
     }
-    
+
     /**
      * Set up the testsuite to run in embedded, client and encryption mode.
      * Default run is 10 threads for 10 minutes in each mode
      */
     public static Test suite() {
-    	Properties sysprops = System.getProperties();
-    	sysprops.put("derby.locks.deadlockTimeout", "2");
-    	sysprops.put("derby.locks.waitTimeout", "3");
-    	sysprops.put("derby.language.logStatementText", "true");
-    	sysprops.put("derby.storage.keepTransactionLog", "true");
-    	sysprops.put("derby.infolog.append", "true");
-
-    	TestSuite embedded = new TestSuite("StressMultiTest:embedded");
-    	embedded.addTestSuite(StressMultiTest.class);
-    	
-    	TestSuite client = new TestSuite("StressMultiTest:client");
-    	client.addTest(TestConfiguration.clientServerDecorator(
-    			new TestSuite(StressMultiTest.class)));
-
-    	TestSuite encrypted = new TestSuite("StressMultiTest:encrypted");
-    	encrypted.addTestSuite(StressMultiTest.class);
-    	
-    	TestSuite unencrypted = new TestSuite("StressMultiTest:unencrypted");
-    	unencrypted.addTest((embedded));
-    	unencrypted.addTest((client));
-    	
-    	TestSuite suite = new TestSuite("StressMultiTest, " + THREADS + 
-    			" Threads " + MINUTES + " Minutes");
-    	suite.addTest(newCleanDatabase(unencrypted));
-    	//Encrypted uses a different database so it needs its own newCleanDatabase
-    	suite.addTest(Decorator.encryptedDatabase(newCleanDatabase(encrypted)));
-    	
-    	return suite;
+        Properties sysprops = System.getProperties();
+        sysprops.put("derby.locks.deadlockTimeout", "2");
+        sysprops.put("derby.locks.waitTimeout", "3");
+        sysprops.put("derby.language.logStatementText", "true");
+        sysprops.put("derby.storage.keepTransactionLog", "true");
+        sysprops.put("derby.infolog.append", "true");
+
+        TestSuite embedded = new TestSuite("StressMultiTest:embedded");
+        embedded.addTestSuite(StressMultiTest.class);
+
+        TestSuite client = new TestSuite("StressMultiTest:client");
+        client.addTest(TestConfiguration.clientServerDecorator(
+                new TestSuite(StressMultiTest.class)));
+
+        TestSuite encrypted = new TestSuite("StressMultiTest:encrypted");
+        encrypted.addTestSuite(StressMultiTest.class);
+
+        TestSuite unencrypted = new TestSuite("StressMultiTest:unencrypted");
+        unencrypted.addTest((embedded));
+        unencrypted.addTest((client));
+
+        TestSuite suite = new TestSuite("StressMultiTest, " + THREADS +
+                " Threads " + MINUTES + " Minutes");
+        suite.addTest(newCleanDatabase(unencrypted));
+        //Encrypted uses a different database so it needs its own newCleanDatabase
+        suite.addTest(Decorator.encryptedDatabase(newCleanDatabase(encrypted)));
+
+        return suite;
     }
-    
+
     /**
      * Get a testsuite that runs the specified number of threads
      * for the specified number of minutes.
-     * 
+     *
      * @param threads
      * @param minutes
      * @return
      */
     public static Test suite(int threads, int minutes) {
-    	THREADS = threads;
-    	MINUTES = minutes;
-    	return suite();
+        THREADS = threads;
+        MINUTES = minutes;
+        return suite();
     }
-    
+
     /*
      * Create a CleanDatabaseTestSetup that sets up the testdatabase.
      */
     private static Test newCleanDatabase(TestSuite s) {
-    	return new CleanDatabaseTestSetup(s) { 
-    	/**
+        return new CleanDatabaseTestSetup(s) {
+        /**
          * Creates the database objects used in the test cases.
-         * 
+         *
          * @throws SQLException
          */
         protected void decorateSQL(Statement s) throws SQLException {
-        	s.execute("CREATE FUNCTION  PADSTRING (DATA VARCHAR(32000), " 
-        			+ "LENGTH INTEGER) RETURNS VARCHAR(32000) EXTERNAL NAME " +
-    				"'org.apache.derbyTesting.functionTests.util.Formatters" +
-    				".padString' LANGUAGE JAVA PARAMETER STYLE JAVA");
-    		s.execute("CREATE FUNCTION RANDOM() RETURNS DOUBLE EXTERNAL " +
-    				"NAME 'java.lang.Math.random' LANGUAGE JAVA PARAMETER " +
-    				"STYLE JAVA");
-    		s.execute("create table main(x int not null primary key," +
-    				" y varchar(2000))");
-    		s.execute("insert into main values(1, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(2, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(3, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(4, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(5, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(6, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(7, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(8, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(9, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(10, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(12, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main values(13, PADSTRING('aaaa',2000))");
-    		s.execute("create table main2(x int not null primary key," +
-    				" y varchar(2000))");
-    		s.execute("insert into main2 values(1, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(2, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(3, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(4, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(5, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(6, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(7, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(8, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(9, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(10, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(12, PADSTRING('aaaa',2000))");
-    		s.execute("insert into main2 values(13, PADSTRING('aaaa',2000))");
-        	getConnection().commit();
+            s.execute("CREATE FUNCTION  PADSTRING (DATA VARCHAR(32000), "
+                    + "LENGTH INTEGER) RETURNS VARCHAR(32000) EXTERNAL NAME " +
+                    "'org.apache.derbyTesting.functionTests.util.Formatters" +
+                    ".padString' LANGUAGE JAVA PARAMETER STYLE JAVA");
+            s.execute("CREATE FUNCTION RANDOM() RETURNS DOUBLE EXTERNAL " +
+                    "NAME 'java.lang.Math.random' LANGUAGE JAVA PARAMETER " +
+                    "STYLE JAVA");
+            s.execute("create table main(x int not null primary key," +
+                    " y varchar(2000))");
+            s.execute("insert into main values(1, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(2, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(3, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(4, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(5, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(6, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(7, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(8, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(9, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(10, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(12, PADSTRING('aaaa',2000))");
+            s.execute("insert into main values(13, PADSTRING('aaaa',2000))");
+            s.execute("create table main2(x int not null primary key," +
+                    " y varchar(2000))");
+            s.execute("insert into main2 values(1, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(2, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(3, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(4, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(5, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(6, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(7, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(8, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(9, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(10, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(12, PADSTRING('aaaa',2000))");
+            s.execute("insert into main2 values(13, PADSTRING('aaaa',2000))");
+            getConnection().commit();
         };
     };
     }
-    
-    
+
+
     /*
      * (non-Javadoc)
      * @see junit.framework.TestCase#setUp()
      */
     public void setUp() throws Exception{
-    	super.setUp();
-		this.getTestConfiguration().setVerbosity(DEBUG);
-	}
-    
+        super.setUp();
+        this.getTestConfiguration().setVerbosity(DEBUG);
+    }
+
     /*
      * Make sure we clear the fields when done.
      */
     public void tearDown() throws Exception{
-    	testResult = null;
-    	rnd = null;
-    	threads = null;
-    	super.tearDown();
+        testResult = null;
+        rnd = null;
+        threads = null;
+        super.tearDown();
     }
-    
+
     /**
-	 * This is the actual fixture run by the JUnit framework.
-	 * Creates all the runnables we need and pass them on to 
-	 * runTestCaseRunnables
-	 */
+     * This is the actual fixture run by the JUnit framework.
+     * Creates all the runnables we need and pass them on to
+     * runTestCaseRunnables
+     */
     public void testStressMulti() {
         StressMultiRunnable[] tct = new StressMultiRunnable[THREADS];
-        
+
         for (int i = 0; i < tct.length; i++) {
            tct[i] = new StressMultiRunnable ("Tester" + i, MINUTES);
         }
@@ -202,7 +202,7 @@
 
     /*
      * Create all the threads and run them until they finish.
-     * 
+     *
      * @param runnables
      */
     protected void runTestCaseRunnables(final StressMultiRunnable[] runnables) {
@@ -210,7 +210,7 @@
             throw new IllegalArgumentException("runnables is null");
         }
         threads = new Thread[runnables.length];
-        
+
         //Create threads
         for (int i = 0; i < threads.length; i++) {
             threads[i] = new Thread(runnables[i]);
@@ -229,20 +229,20 @@
         }
         threads = null;
     }
- 
+
     public void run(final TestResult result) {
-		testResult = result;
-		super.run(result);
-		testResult = null;
-	}
-    
+        testResult = result;
+        super.run(result);
+        testResult = null;
+    }
+
     /*
-	 * Handles any exceptions we get in the threads
-	 */
-     private void handleException(final Throwable t, String message) {
+     * Handles any exceptions we get in the threads
+     */
+    private void handleException(final Throwable t, String message) {
         synchronized(testResult) {
-        	println("Exception handled!!: "+ message + " - " + t);
-        	complete = true; //This stops the tests.
+            println("Exception handled!!: "+ message + " - " + t);
+            complete = true; //This stops the tests.
             if(t instanceof AssertionFailedError) {
                 testResult.addFailure(this, (AssertionFailedError)t);
             }
@@ -250,11 +250,11 @@
                 testResult.addError(this, t);
             }
         }
-     }
-    
-    
+    }
+
+
     /**
-     * This subclass contains the actual stresstests that will be run in 
+     * This subclass contains the actual stresstests that will be run in
      * multiple threads.
      */
     class StressMultiRunnable implements Runnable {
@@ -262,226 +262,226 @@
         Connection con;
         long starttime;
         long runtime; //How long will this thread run
-        
+
         public StressMultiRunnable(String name, int minutes) {
             super();
             this.name = name;
             starttime = System.currentTimeMillis();
             runtime = minutes*60*1000; //convert min to ms
             try {
-            	con = openDefaultConnection();
-            	con.setAutoCommit(false);
+                con = openDefaultConnection();
+                con.setAutoCommit(false);
             } catch (SQLException e) {
-            	println(e.toString());
+                println(e.toString());
             }
         }
-       
+
        /*
-        * Do the work. Runs an infinite loop picking random operations from 
-        * the methods below until either complete == true, ie. there was a 
+        * Do the work. Runs an infinite loop picking random operations from
+        * the methods below until either complete == true, ie. there was a
         * failure in one of the threads, or the specified time has passed.
         */
         public void run() {
-    	   try {
-              int i = 0;
-              while (!complete) {
-            	  i++;
-                  int r = rnd.nextInt(100);
-                  
-                  if (r < 10) {
-                	  String n = "x";
-                	  switch (rnd.nextInt(4)) {
-	                	  case 0: n = "a"; break;
-	                	  case 1: n = "x"; break;
-	                	  case 2: n = "y"; break;
-	                	  case 3: n = "z"; break;
-                	  }
-                      create(n);
-                	  println(name + " - Run " + i + " - Create " + n + " " + 
-                			  new Date(System.currentTimeMillis()).toString());
-                  } else if (r < 25){
-                	  String n = "main";
-                	  if (rnd.nextInt(2) == 1) n = "main2";
-                	  roll(n);
-                	  println(name + " - Run " + i + " - Roll " + n + " " + 
-                			  new Date(System.currentTimeMillis()).toString());
-                  } else if (r < 40){
-                	  String n = "main";
-                	  if (rnd.nextInt(2) == 1) n = "main2";
-                	  insert(n);
-                      println(name + " - Run " + i + " - Insert " + n + " " + 
-                    		  new Date(System.currentTimeMillis()).toString());
-                  } else if (r < 60){
-                	  String n = "main";
-                	  if (rnd.nextInt(2) == 1) n = "main2";
-                	  update(n);
-                	  println(name + " - Run " + i + " - Update " + 
-                			  n + " " + 
-                			  new Date(System.currentTimeMillis()).toString());
-                  } else if (r <= 99){
-                	  String n = "main";
-                	  if (rnd.nextInt(2) == 1) n = "main2";
-                	  select(n);
-                	  println(name + " - Run " + i + " - Select " + n + " " + 
-                			  new Date(System.currentTimeMillis()).toString());
-                  }
-                  //Break the loop if the running time is reached.
-                  if ((starttime + runtime) <= System.currentTimeMillis()) {
-                	  println(name + " - STOPPING - " + 
-                			  new Date(System.currentTimeMillis()).toString());
-                	  break;
-                  }
-                 Thread.sleep(rnd.nextInt(10)); //Just to spread them out a bit.
-              }
-          }
-          catch(Throwable t) {
-        	 println("Exception in " + name + ": " + t);
-             handleException(t, name + " - " + 
-            		 new Date(System.currentTimeMillis()).toString());
-          }
-          println(name + " terminated!");
-       }
-       
-       
-       /********* Below are the tasks done by the threads  ******************/
-       
-       /**
-        * Create a table with the given name and then drop it
-        * 
-        * @param table
-        * @throws SQLException
-        */
-       private void create(String table) throws SQLException {
-    	   Statement s = con.createStatement();
-    	   try {
-				s.execute("create table " + table + " (x int)");
-				s.execute("insert into " + table + " values (1)");
-				s.execute("insert into " + table + " values (1)");
-				s.execute("insert into " + table + " values (1)");
-				s.execute("insert into " + table + " values (1)");
-				s.execute("insert into " + table + " values (1)");
-				s.execute("drop table " + table);
-				con.commit();
-			} catch (SQLException se) {
-				String e = se.getSQLState();
-				if (e.equals("X0X08") || e.equals("X0X05") || e.equals("42X05")
-						|| e.equals("42Y55") || e.equals("42000")
-						|| e.equals("40001") || e.equals("40XL1")
-						|| e.equals("40XL2") || e.equals("42Y07")
-						|| e.equals("42Y55")) {
-					//Ignore these
-				} else {
-					throw se;
-				}
-			} finally {
-				s = null;
-			}
-		}
-       
-       /**
-        * Insert a random value into the given table. 
-        * Table names can be main or main2.
-        * 
-        * @param table
-        * @throws SQLException
-        */
-       private void insert(String table) throws SQLException {
-			Statement s = con.createStatement();
-			try {
-				s.executeUpdate("insert into " + table
-						+ " values (random() * 1000 + 100, 'rand')");
-				con.commit();
-			} catch (SQLException se) {
-				String e = se.getSQLState();
-				if (e.equals("42000") || e.equals("23505") || e.equals("40001")
-						|| e.equals("40XL1") || e.equals("40XL2")
-						|| e.equals("42Y07") || e.equals("42Y55")) {
-					// ignore these
-				} else {
-					throw se;
-				}
-			}finally {
-				s = null;
-			}
-		}
-       
-       /**
-        * insert a value into the given table, then rollback. 
-        * Table names are main or main2.
-        * 
-        * @param table
-        * @throws SQLException
-        */
-       private void roll(String table) throws SQLException {
-			Statement s = con.createStatement();
-			con.setAutoCommit(false);
-			try {
-				s.executeUpdate("insert into " + table
-								+ " values (666, '666')");
-				con.rollback();
-			} catch (SQLException se) {
-				String e = se.getSQLState();
-				if (e.equals("X0X05") || e.equals("42X05") || e.equals("42Y55")
-						|| e.equals("42000") || e.equals("23505")
-						|| e.equals("40001") || e.equals("40XL1")
-						|| e.equals("40XL2") || e.equals("42Y07")
-						|| e.equals("42Y55")) {
-					// ignore these
-				} else {
-					throw se;
-				}
-			}finally {
-				s = null;
-			}
-		}
-       
-       /**
-		 * Select * from the given table. Table names are main or main2.
-		 * 
-		 * @param table
-		 * @throws SQLException
-		 */
-       private void select(String table) throws SQLException {
-			Statement s = con.createStatement();
-			try {
-				s.executeQuery("select * from " + table);
-			} catch (SQLException se) {
-				String e = se.getSQLState();
-				if (e.equals("42Y55") || e.equals("42000") || e.equals("40001")
-						|| e.equals("40XL1") || e.equals("40XL2")
-						|| e.equals("42Y07")) {
-					// ignore these
-				} else {
-					throw se;
-				}
-			}finally {
-				s = null;
-			}
-		}
-       
-       /**
-        * Update the given table. Table names are main or main2.
-        * 
-        * @param table
-        * @throws SQLException
-        */
-       private void update(String table) throws SQLException {
-			Statement s = con.createStatement();
-			try {
-				s.executeUpdate("update " + table
-						+ " main set y = 'zzz' where x = 5");
-			} catch (SQLException se) {
-				String e = se.getSQLState();
-				if (e.equals("42Y55") || e.equals("42000") || e.equals("40001")
-						|| e.equals("40XL1") || e.equals("40XL2")
-						|| e.equals("42Y07")) {
-					// ignore these
-				} else {
-					throw se;
-				} 
-			} finally {
-				s = null;
-			}
-		}
+            try {
+                int i = 0;
+                while (!complete) {
+                    i++;
+                    int r = rnd.nextInt(100);
+
+                    if (r < 10) {
+                        String n = "x";
+                        switch (rnd.nextInt(4)) {
+                            case 0: n = "a"; break;
+                            case 1: n = "x"; break;
+                            case 2: n = "y"; break;
+                            case 3: n = "z"; break;
+                        }
+                        create(n);
+                        println(name + " - Run " + i + " - Create " + n + " " +
+                                new Date(System.currentTimeMillis()).toString());
+                    } else if (r < 25){
+                        String n = "main";
+                        if (rnd.nextInt(2) == 1) n = "main2";
+                        roll(n);
+                        println(name + " - Run " + i + " - Roll " + n + " " +
+                                new Date(System.currentTimeMillis()).toString());
+                    } else if (r < 40){
+                        String n = "main";
+                        if (rnd.nextInt(2) == 1) n = "main2";
+                        insert(n);
+                        println(name + " - Run " + i + " - Insert " + n + " " +
+                                new Date(System.currentTimeMillis()).toString());
+                    } else if (r < 60){
+                        String n = "main";
+                        if (rnd.nextInt(2) == 1) n = "main2";
+                        update(n);
+                        println(name + " - Run " + i + " - Update " +
+                                n + " " +
+                                new Date(System.currentTimeMillis()).toString());
+                    } else if (r <= 99){
+                        String n = "main";
+                        if (rnd.nextInt(2) == 1) n = "main2";
+                        select(n);
+                        println(name + " - Run " + i + " - Select " + n + " " +
+                                new Date(System.currentTimeMillis()).toString());
+                    }
+                    //Break the loop if the running time is reached.
+                    if ((starttime + runtime) <= System.currentTimeMillis()) {
+                        println(name + " - STOPPING - " +
+                                new Date(System.currentTimeMillis()).toString());
+                        break;
+                    }
+                    Thread.sleep(rnd.nextInt(10)); //Just to spread them out a bit.
+                }
+            }
+            catch(Throwable t) {
+               println("Exception in " + name + ": " + t);
+               handleException(t, name + " - " +
+                       new Date(System.currentTimeMillis()).toString());
+            }
+            println(name + " terminated!");
+        }
+
+
+        /********* Below are the tasks done by the threads  ******************/
+
+        /**
+         * Create a table with the given name and then drop it
+         *
+         * @param table
+         * @throws SQLException
+         */
+        private void create(String table) throws SQLException {
+            Statement s = con.createStatement();
+            try {
+                s.execute("create table " + table + " (x int)");
+                s.execute("insert into " + table + " values (1)");
+                s.execute("insert into " + table + " values (1)");
+                s.execute("insert into " + table + " values (1)");
+                s.execute("insert into " + table + " values (1)");
+                s.execute("insert into " + table + " values (1)");
+                s.execute("drop table " + table);
+                con.commit();
+            } catch (SQLException se) {
+                String e = se.getSQLState();
+                if (e.equals("X0X08") || e.equals("X0X05") || e.equals("42X05")
+                        || e.equals("42Y55") || e.equals("42000")
+                        || e.equals("40001") || e.equals("40XL1")
+                        || e.equals("40XL2") || e.equals("42Y07")
+                        || e.equals("42Y55")) {
+                    //Ignore these
+                } else {
+                    throw se;
+                }
+            } finally {
+                s = null;
+            }
+        }
+
+        /**
+         * Insert a random value into the given table.
+         * Table names can be main or main2.
+         *
+         * @param table
+         * @throws SQLException
+         */
+        private void insert(String table) throws SQLException {
+            Statement s = con.createStatement();
+            try {
+                s.executeUpdate("insert into " + table
+                        + " values (random() * 1000 + 100, 'rand')");
+                con.commit();
+            } catch (SQLException se) {
+                String e = se.getSQLState();
+                if (e.equals("42000") || e.equals("23505") || e.equals("40001")
+                        || e.equals("40XL1") || e.equals("40XL2")
+                        || e.equals("42Y07") || e.equals("42Y55")) {
+                    // ignore these
+                } else {
+                    throw se;
+                }
+            }finally {
+                s = null;
+            }
+        }
+
+        /**
+         * insert a value into the given table, then rollback.
+         * Table names are main or main2.
+         *
+         * @param table
+         * @throws SQLException
+         */
+        private void roll(String table) throws SQLException {
+            Statement s = con.createStatement();
+            con.setAutoCommit(false);
+            try {
+                s.executeUpdate("insert into " + table
+                                + " values (666, '666')");
+                con.rollback();
+            } catch (SQLException se) {
+                String e = se.getSQLState();
+                if (e.equals("X0X05") || e.equals("42X05") || e.equals("42Y55")
+                        || e.equals("42000") || e.equals("23505")
+                        || e.equals("40001") || e.equals("40XL1")
+                        || e.equals("40XL2") || e.equals("42Y07")
+                        || e.equals("42Y55")) {
+                    // ignore these
+                } else {
+                    throw se;
+                }
+            }finally {
+                s = null;
+            }
+        }
+
+        /**
+          * Select * from the given table. Table names are main or main2.
+          *
+          * @param table
+          * @throws SQLException
+          */
+        private void select(String table) throws SQLException {
+            Statement s = con.createStatement();
+            try {
+                s.executeQuery("select * from " + table);
+            } catch (SQLException se) {
+                String e = se.getSQLState();
+                if (e.equals("42Y55") || e.equals("42000") || e.equals("40001")
+                        || e.equals("40XL1") || e.equals("40XL2")
+                        || e.equals("42Y07")) {
+                    // ignore these
+                } else {
+                    throw se;
+                }
+            }finally {
+                s = null;
+            }
+        }
+
+        /**
+         * Update the given table. Table names are main or main2.
+         *
+         * @param table
+         * @throws SQLException
+         */
+        private void update(String table) throws SQLException {
+            Statement s = con.createStatement();
+            try {
+                s.executeUpdate("update " + table
+                        + " main set y = 'zzz' where x = 5");
+            } catch (SQLException se) {
+                String e = se.getSQLState();
+                if (e.equals("42Y55") || e.equals("42000") || e.equals("40001")
+                        || e.equals("40XL1") || e.equals("40XL2")
+                        || e.equals("42Y07")) {
+                    // ignore these
+                } else {
+                    throw se;
+                }
+            } finally {
+                s = null;
+            }
+        }
     }
 }



Mime
View raw message