Added:
trunk/test/jconch/testing/DummyCallable.java
trunk/test/jconch/testing/DummyRunnable.java
Modified:
trunk/build.number
trunk/src/jconch/testing/Assert.java
trunk/test/jconch/testing/AssertTest.java
trunk/test/jconch/testing/SerialExecutorServiceTest.java
Log:
Fixed bug in assertSynchronized which caused deadlock if the # of tasks was
greater than the # of threads.
Modified: trunk/build.number
==============================================================================
--- trunk/build.number (original)
+++ trunk/build.number Sun Jan 18 05:11:45 2009
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
-#Thu Jan 15 22:43:26 CST 2009
-build.number=9
+#Sun Jan 18 07:10:59 CST 2009
+build.number=11
Modified: trunk/src/jconch/testing/Assert.java
==============================================================================
--- trunk/src/jconch/testing/Assert.java (original)
+++ trunk/src/jconch/testing/Assert.java Sun Jan 18 05:11:45 2009
@@ -101,7 +101,7 @@
private static void doAssertSynchronized(Callable<? extends
Collection<Callable<Void>>> taskFactory, int maxThreads, int numIterations)
throws Exception {
if (taskFactory == null) throw new NullPointerException("Null:
taskFactory");
if (maxThreads <= 0) throw new IllegalArgumentException("maxThreads must
be positive. Received: " + maxThreads);
- if (numIterations <= 0) throw new
IllegalArgumentException("numIterations must be positive. Received: " +
maxThreads);
+ if (numIterations <= 0) throw new
IllegalArgumentException("numIterations must be positive. Received: " +
numIterations);
final ExecutorService executor =
Executors.newFixedThreadPool(maxThreads);
final AtomicInteger failureCount = new AtomicInteger(0);
@@ -110,7 +110,7 @@
for (int x = 0; x < numIterations; x++) {
final Collection<Callable<Void>> tasks = taskFactory.call();
final int numTasks = tasks.size();
- final CyclicBarrier startGate = new CyclicBarrier(numTasks);
+ final CyclicBarrier startGate = new CyclicBarrier(Math.min(numTasks,
maxThreads));
final CountDownLatch endGate = new CountDownLatch(numTasks);
for (final Callable task : tasks) {
Modified: trunk/test/jconch/testing/AssertTest.java
==============================================================================
--- trunk/test/jconch/testing/AssertTest.java (original)
+++ trunk/test/jconch/testing/AssertTest.java Sun Jan 18 05:11:45 2009
@@ -134,4 +134,19 @@
}
);
}
+
+ @Test
+ public void testAssertSynchronized_TasksCountGreaterThanMaxThreads()
throws Exception {
+ Assert.assertSynchronized(
+ new Callable<List<Callable<Void>>>(){
+ public List<Callable<Void>> call() throws Exception {
+ return new ArrayList<Callable<Void>>(){{
+ add(new DummyCallable());
+ add(new DummyCallable());
+ }};
+ }
+ },
+ 1, //only use 1 thread to force # tasks > # threads
+ 1);
+ }
}
Added: trunk/test/jconch/testing/DummyCallable.java
==============================================================================
--- (empty file)
+++ trunk/test/jconch/testing/DummyCallable.java Sun Jan 18 05:11:45 2009
@@ -0,0 +1,21 @@
+package jconch.testing;
+
+import java.util.concurrent.Callable;
+
+/**
+ * A dummy Callable capable of telling you if it was called or not.
+ *
+ * @author Hamlet D'Arcy
+ */
+class DummyCallable implements Callable<Void> {
+ private boolean wasCalled = false;
+
+ public Void call() throws Exception {
+ wasCalled = true;
+ return null;
+ }
+
+ boolean wasCalled() {
+ return wasCalled;
+ }
+}
Added: trunk/test/jconch/testing/DummyRunnable.java
==============================================================================
--- (empty file)
+++ trunk/test/jconch/testing/DummyRunnable.java Sun Jan 18 05:11:45 2009
@@ -0,0 +1,19 @@
+package jconch.testing;
+
+import java.util.concurrent.Callable;
+
+/**
+ * A dummy Runnable capable of telling you if it was called or not.
+ *
+ * @author Hamlet D'Arcy
+ */
+class DummyRunnable implements Runnable {
+ private boolean wasCalled = false;
+ public void run() {
+ wasCalled = true;
+ }
+
+ boolean wasCalled() {
+ return wasCalled;
+ }
+}
Modified: trunk/test/jconch/testing/SerialExecutorServiceTest.java
==============================================================================
--- trunk/test/jconch/testing/SerialExecutorServiceTest.java (original)
+++ trunk/test/jconch/testing/SerialExecutorServiceTest.java Sun Jan 18
05:11:45 2009
@@ -44,12 +44,12 @@
//expected
}
try {
- service.submit(new ErrorlRunnable(), null);
+ service.submit(new ErrorRunnable(), null);
} catch (RejectedExecutionException ignored) {
//expected
}
try {
- service.submit(new ErrorlRunnable());
+ service.submit(new ErrorRunnable());
} catch (RejectedExecutionException ignored) {
//expected
}
@@ -82,17 +82,17 @@
assertSame(expectedResult, service.submit(new
ValueCallable(expectedResult)).get());
final DummyRunnable dummyRunnable = new DummyRunnable();
assertNull(((Future<?>) service.submit(dummyRunnable)).get());
- assertTrue(dummyRunnable.wasCalled);
+ assertTrue(dummyRunnable.wasCalled());
final DummyRunnable dummyRunnable2 = new DummyRunnable();
assertSame(expectedResult, service.submit(dummyRunnable2,
expectedResult).get());
- assertTrue(dummyRunnable2.wasCalled);
+ assertTrue(dummyRunnable2.wasCalled());
}
@Test
public void testExecute() throws Exception {
final DummyRunnable runnable = new DummyRunnable();
service.execute(runnable);
- assertTrue(runnable.wasCalled);
+ assertTrue(runnable.wasCalled());
}
@Test
@@ -157,17 +157,11 @@
return value;
}
}
- private static class ErrorlRunnable implements Runnable {
+ private static class ErrorRunnable implements Runnable {
public void run() {
throw new IllegalStateException(String.format("%s may not be invoked.",
getClass().getName()));
}
}
- private static class DummyRunnable implements Runnable {
- private boolean wasCalled = false;
- public void run() {
- wasCalled = true;
- }
- }
}