cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1385201 - /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java
Date Sun, 16 Sep 2012 00:09:26 GMT
Author: aadamchik
Date: Sun Sep 16 00:09:26 2012
New Revision: 1385201

URL: http://svn.apache.org/viewvc?rev=1385201&view=rev
Log:
better timeout wrapper for the time-sensitive test

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java?rev=1385201&r1=1385200&r2=1385201&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContext_DeadlockTest.java
Sun Sep 16 00:09:26 2012
@@ -30,96 +30,99 @@ import org.apache.cayenne.test.jdbc.Tabl
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
+import org.apache.cayenne.unit.util.ThreadedTestHelper;
 
 @UseServerRuntime(ServerCase.TESTMAP_PROJECT)
 public class NestedDataContext_DeadlockTest extends ServerCase {
 
-    @Inject
-    private DataContext parent;
+	@Inject
+	private DataContext parent;
 
-    @Inject
-    private ServerRuntime runtime;
+	@Inject
+	private ServerRuntime runtime;
 
-    @Inject
-    protected DBHelper dbHelper;
+	@Inject
+	protected DBHelper dbHelper;
 
-    protected TableHelper tArtist;
-
-    @Override
-    protected void setUpAfterInjection() throws Exception {
-        dbHelper.deleteAll("PAINTING_INFO");
-        dbHelper.deleteAll("PAINTING");
-        dbHelper.deleteAll("ARTIST_EXHIBIT");
-        dbHelper.deleteAll("ARTIST_GROUP");
-        dbHelper.deleteAll("ARTIST");
-
-        tArtist = new TableHelper(dbHelper, "ARTIST");
-        tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
-    }
-
-    private void createArtists() throws Exception {
-        for (int i = 0; i < 300; i++) {
-            tArtist.insert(i + 1, "X" + i);
-        }
-    }
-
-    public void testDeadlock() throws Exception {
-
-        createArtists();
-
-        Thread[] threads = new Thread[2];
-
-        Random rnd = new Random(System.currentTimeMillis());
-        for (int i = 0; i < threads.length; i++) {
-            threads[i] = new UpdateThread(
-                    "UpdateThread-" + i,
-                    runtime.getContext(parent),
-                    rnd);
-        }
-
-        for (int i = 0; i < threads.length; i++) {
-            threads[i].start();
-        }
-
-        synchronized (parent) {
-            parent.wait(10000);
-        }
-
-        for (int i = 0; i < threads.length; i++) {
-            // unfortunately here we'll have to leave some dead threads behind... Of
-            // course if there's no deadlock, there won't be a leak either
-            assertTrue("Deadlocked thread", !threads[i].isAlive());
-        }
-    }
-
-    static class UpdateThread extends Thread {
-
-        protected ObjectContext nestedContext;
-        protected Random rnd;
-
-        UpdateThread(String name, ObjectContext nestedContext, Random rnd) {
-            super(name);
-            setDaemon(true);
-            this.nestedContext = nestedContext;
-            this.rnd = rnd;
-        }
-
-        @Override
-        public void run() {
-
-            List<Artist> artists = nestedContext.performQuery(new EJBQLQuery(
-                    "select a FROM Artist a"));
-
-            for (int i = 0; i < 100; i++) {
-
-                for (int j = 0; j < 5; j++) {
-                    int index = rnd.nextInt(artists.size());
-                    Artist a = artists.get(index);
-                    a.setArtistName("Y" + rnd.nextInt());
-                }
-
-                nestedContext.commitChanges();
-            }
-        }
-    }
+	protected TableHelper tArtist;
+
+	@Override
+	protected void setUpAfterInjection() throws Exception {
+		dbHelper.deleteAll("PAINTING_INFO");
+		dbHelper.deleteAll("PAINTING");
+		dbHelper.deleteAll("ARTIST_EXHIBIT");
+		dbHelper.deleteAll("ARTIST_GROUP");
+		dbHelper.deleteAll("ARTIST");
+
+		tArtist = new TableHelper(dbHelper, "ARTIST");
+		tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+	}
+
+	private void createArtists() throws Exception {
+		for (int i = 0; i < 300; i++) {
+			tArtist.insert(i + 1, "X" + i);
+		}
+	}
+
+	public void testDeadlock() throws Exception {
+
+		createArtists();
+
+		final Thread[] threads = new Thread[2];
+
+		Random rnd = new Random(System.currentTimeMillis());
+		for (int i = 0; i < threads.length; i++) {
+			threads[i] = new UpdateThread("UpdateThread-" + i,
+					runtime.getContext(parent), rnd);
+		}
+
+		for (int i = 0; i < threads.length; i++) {
+			threads[i].start();
+		}
+
+		new ThreadedTestHelper() {
+
+			@Override
+			protected void assertResult() throws Exception {
+				for (int i = 0; i < threads.length; i++) {
+					// unfortunately here we'll have to leave some dead threads
+					// behind... Of course if there's no deadlock, there won't
+					// be a leak either
+					assertTrue("Deadlocked thread", !threads[i].isAlive());
+				}
+			}
+		}.assertWithTimeout(2000);
+
+	}
+
+	static class UpdateThread extends Thread {
+
+		protected ObjectContext nestedContext;
+		protected Random rnd;
+
+		UpdateThread(String name, ObjectContext nestedContext, Random rnd) {
+			super(name);
+			setDaemon(true);
+			this.nestedContext = nestedContext;
+			this.rnd = rnd;
+		}
+
+		@Override
+		public void run() {
+
+			List<Artist> artists = nestedContext.performQuery(new EJBQLQuery(
+					"select a FROM Artist a"));
+
+			for (int i = 0; i < 100; i++) {
+
+				for (int j = 0; j < 5; j++) {
+					int index = rnd.nextInt(artists.size());
+					Artist a = artists.get(index);
+					a.setArtistName("Y" + rnd.nextInt());
+				}
+
+				nestedContext.commitChanges();
+			}
+		}
+	}
 }



Mime
View raw message