00001
00002
00003
00004
00005
00006
00007 package jpl.test;
00008
00009 import java.util.concurrent.CountDownLatch;
00010 import java.util.concurrent.TimeUnit;
00011 import java.util.logging.Logger;
00012 import junit.framework.*;
00013 import jpl.*;
00014
00019 public class JPLTest extends TestCase {
00020
00021 private CountDownLatch latch;
00022 public JPLTest(String testName) {
00023 super(testName);
00024 }
00025 protected void setUp() throws Exception {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 }
00044 public void testThreadedAdds() {
00045 latch = new CountDownLatch(4);
00046 final AddWithThreads[] addTasks = { new AddWithThreads("a", latch), new AddWithThreads("b", latch), new AddWithThreads("c", latch), new AddWithThreads("d", latch) };
00047
00048 for (int i = 0; i < addTasks.length; i++) {
00049 addTasks[i].start();
00050 }
00051 try {
00052
00053 assertTrue("Timed out waiting for action to execute", latch.await(20, TimeUnit.SECONDS));
00054
00055 } catch (final InterruptedException e) {
00056 fail("Waiting thread was interrupted: " + e);
00057 }
00058 for (int i = 0; i < AddWithThreads.REPS; i++) {
00059 for (int j = 0; j < addTasks.length; j++) {
00060 Query query = new Query(addTasks[j].getNamespace() + "(test('" + i + "'))");
00061
00062 boolean ret = query.hasMoreElements();
00063 query.close();
00064 }
00065 }
00066 }
00067 }
00068
00069 class AddWithThreads extends Thread {
00070 private final CountDownLatch latch;
00071 private final String namespace;
00072 private static final Logger logger = Logger.getLogger(JPLTest.class.getName());
00073 public static final int REPS = 2000;
00074 public AddWithThreads(final String namespace, final CountDownLatch latch) {
00075 this.latch = latch;
00076 this.namespace = namespace;
00077 setName("namespace" + namespace);
00078 }
00079 public String getNamespace() {
00080 return namespace;
00081 }
00082 public void run() {
00083 for (int i = 0; i < REPS; i++) {
00084
00085 Query queryA = new Query("assert(" + namespace + "(test('" + i + "')))");
00086 Thread.yield();
00087
00088 boolean retA = queryA.hasMoreElements();
00089 queryA.close();
00090 }
00091 latch.countDown();
00092 }
00093 }