@SuppressWarnings("deprecation")
  // test explicitly needs to use "stop", which is deprecated
  public static void testCase() {
    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  synchronized (o) {
                    in_wait = true;
                    // System.out.println("wait");
                    o.wait(3000); // just a bit
                    in_wait = false;
                  }
                } catch (Exception e) {
                  // System.out.println(Thread.currentThread().getName() + " caught " + e);
                  // e.printStackTrace();
                  in_wait = false;
                }
              }
            });

    t.start();
    while (!in_wait) {} // Lame
    synchronized (o) {
      // System.out.println("stop");
      t.stop();
    }
    try {
      Thread.sleep(8000); // sleep longer than our wait thread
    } catch (Exception e) {
      assertTrue(false);
    }
    assertTrue(in_wait); // if we finished wait, we failed to interrupt
  }
  /** compareAndSet in one thread enables another waiting for value to succeed */
  public void testCompareAndSetInMultipleThreads() throws Exception {
    x = one;
    final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
    try {
      a =
          AtomicReferenceFieldUpdater.newUpdater(
              AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
    } catch (RuntimeException ok) {
      return;
    }

    Thread t =
        new Thread(
            new CheckedRunnable() {
              public void realRun() {
                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
                  Thread.yield();
              }
            });

    t.start();
    assertTrue(a.compareAndSet(this, one, two));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(a.get(this), three);
  }
Beispiel #3
0
  @Override
  protected void setUp() throws Exception {
    tmp = IO.getFile("generated/tmp");
    tmp.mkdirs();
    IO.copy(IO.getFile("testdata/ws"), tmp);
    workspace = Workspace.getWorkspace(tmp);
    workspace.refresh();

    InfoRepository repo = workspace.getPlugin(InfoRepository.class);
    t1 = create("bsn-1", new Version(1, 0, 0));
    t2 = create("bsn-2", new Version(1, 0, 0));

    repo.put(new FileInputStream(t1), null);
    repo.put(new FileInputStream(t2), null);
    t1 = repo.get("bsn-1", new Version(1, 0, 0), null);
    t2 = repo.get("bsn-2", new Version(1, 0, 0), null);
    repo.put(new FileInputStream(IO.getFile("generated/biz.aQute.remote.launcher.jar")), null);

    workspace.getPlugins().add(repo);

    File storage = IO.getFile("generated/storage-1");
    storage.mkdirs();

    configuration = new HashMap<String, Object>();
    configuration.put(
        Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
    configuration.put(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());

    configuration.put(
        Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.osgi.framework.launch;version=1.2");

    framework = new org.apache.felix.framework.FrameworkFactory().newFramework(configuration);
    framework.init();
    framework.start();
    context = framework.getBundleContext();
    location = "reference:" + IO.getFile("generated/biz.aQute.remote.agent.jar").toURI().toString();
    agent = context.installBundle(location);
    agent.start();

    thread =
        new Thread() {
          @Override
          public void run() {
            try {
              Main.main(
                  new String[] {
                    "-s", "generated/storage", "-c", "generated/cache", "-p", "1090", "-et"
                  });
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        };
    thread.setDaemon(true);
    thread.start();

    super.setUp();
  }
Beispiel #4
0
  private void beforeFirstTest() {
    if ((ourMode & START_GUARD) != 0) {
      Thread timeAndMemoryGuard =
          new Thread() {
            @Override
            public void run() {
              log("Starting Time and Memory Guard");
              while (true) {
                try {
                  try {
                    Thread.sleep(10000);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                  // check for time spent on current test
                  if (myLastTestStartTime != 0) {
                    long currTime = System.currentTimeMillis();
                    long secondsSpent = (currTime - myLastTestStartTime) / 1000L;
                    Thread currentThread = getCurrentThread();
                    if (!mySavingMemorySnapshot) {
                      if (secondsSpent > PlatformTestCase.ourTestTime * myLastTestTestMethodCount) {
                        UsefulTestCase.printThreadDump();
                        log(
                            "Interrupting current Test (out of time)! Test class: "
                                + myLastTestClass
                                + " Seconds spent = "
                                + secondsSpent);
                        myInterruptedByOutOfTime = true;
                        if (currentThread != null) {
                          currentThread.interrupt();
                          if (!currentThread.isInterrupted()) {
                            //noinspection deprecation
                            currentThread.stop(
                                new RuntimeException("Current Test Interrupted: OUT OF TIME!"));
                          }

                          break;
                        }
                      }
                    }
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
              log("Time and Memory Guard finished.");
            }
          };
      timeAndMemoryGuard.setDaemon(true);
      timeAndMemoryGuard.start();
    }
    myStartTime = System.currentTimeMillis();
  }
Beispiel #5
0
  /*
   * Launches against the agent
   */
  public void testSimpleLauncher() throws Exception {
    Project project = workspace.getProject("p1");
    Run bndrun = new Run(workspace, project.getBase(), project.getFile("one.bndrun"));
    bndrun.setProperty("-runpath", "biz.aQute.remote.launcher");
    bndrun.setProperty("-runbundles", "bsn-1,bsn-2");
    bndrun.setProperty("-runremote", "test");

    final RemoteProjectLauncherPlugin pl =
        (RemoteProjectLauncherPlugin) bndrun.getProjectLauncher();
    pl.prepare();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger exitCode = new AtomicInteger(-1);

    List<? extends RunSession> sessions = pl.getRunSessions();
    assertEquals(1, sessions.size());

    final RunSession session = sessions.get(0);

    Thread t =
        new Thread("test-launch") {
          public void run() {
            try {
              exitCode.set(session.launch());
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              latch.countDown();
            }
          }
        };
    t.start();
    Thread.sleep(500);

    for (Bundle b : context.getBundles()) {
      System.out.println(b.getLocation());
    }
    assertEquals(4, context.getBundles().length);
    String p1 = t1.getAbsolutePath();
    System.out.println(p1);

    assertNotNull(context.getBundle(p1));
    assertNotNull(context.getBundle(t2.getAbsolutePath()));

    pl.cancel();
    latch.await();

    assertEquals(-3, exitCode.get());

    bndrun.close();
  }
 /** getExclusiveQueuedThreads returns all exclusive waiting threads */
 public void testGetExclusiveQueuedThreads() {
   final Mutex sync = new Mutex();
   Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
   Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
   assertHasExclusiveQueuedThreads(sync, NO_THREADS);
   sync.acquire();
   assertHasExclusiveQueuedThreads(sync, NO_THREADS);
   t1.start();
   waitForQueuedThread(sync, t1);
   assertHasExclusiveQueuedThreads(sync, t1);
   assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
   assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
   t2.start();
   waitForQueuedThread(sync, t2);
   assertHasExclusiveQueuedThreads(sync, t1, t2);
   assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
   assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
   t1.interrupt();
   awaitTermination(t1);
   assertHasExclusiveQueuedThreads(sync, t2);
   sync.release();
   awaitTermination(t2);
   assertHasExclusiveQueuedThreads(sync, NO_THREADS);
 }
 /** isQueued reports whether a thread is queued */
 public void testIsQueued() {
   final Mutex sync = new Mutex();
   Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
   Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
   assertFalse(sync.isQueued(t1));
   assertFalse(sync.isQueued(t2));
   sync.acquire();
   t1.start();
   waitForQueuedThread(sync, t1);
   assertTrue(sync.isQueued(t1));
   assertFalse(sync.isQueued(t2));
   t2.start();
   waitForQueuedThread(sync, t2);
   assertTrue(sync.isQueued(t1));
   assertTrue(sync.isQueued(t2));
   t1.interrupt();
   awaitTermination(t1);
   assertFalse(sync.isQueued(t1));
   assertTrue(sync.isQueued(t2));
   sync.release();
   awaitTermination(t2);
   assertFalse(sync.isQueued(t1));
   assertFalse(sync.isQueued(t2));
 }
Beispiel #8
0
  private CountDownLatch launch(final RunSession session) {
    final CountDownLatch latch = new CountDownLatch(1);

    Thread t =
        new Thread("test-launch") {
          public void run() {
            try {
              session.launch();
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              latch.countDown();
            }
          }
        };
    t.start();
    return latch;
  }
 /** compareAndSet in one thread enables another waiting for value to succeed */
 public void testCompareAndSetInMultipleThreads() {
   final AtomicLong ai = new AtomicLong(1);
   Thread t =
       new Thread(
           new Runnable() {
             public void run() {
               while (!ai.compareAndSet(2, 3)) Thread.yield();
             }
           });
   try {
     t.start();
     assertTrue(ai.compareAndSet(1, 2));
     t.join(LONG_DELAY_MS);
     assertFalse(t.isAlive());
     assertEquals(ai.get(), 3);
   } catch (Exception e) {
     unexpectedException();
   }
 }
 /** Returns a new started daemon Thread running the given runnable. */
 Thread newStartedThread(Runnable runnable) {
   Thread t = new Thread(runnable);
   t.setDaemon(true);
   t.start();
   return t;
 }
  private void bug40333Simulation(final Region region, final String queryStr) throws Exception {
    final QueryService qs = CacheUtils.getQueryService();
    Region rgn = CacheUtils.getRegion("/new_pos");
    for (int i = 1; i < 100; ++i) {
      NewPortfolio pf = new NewPortfolio("name" + i, i);
      rgn.put("name" + i, pf);
    }
    final Object lock = new Object();
    final boolean[] expectionOccured = new boolean[] {false};
    final boolean[] keepGoing = new boolean[] {true};

    Thread indexCreatorDestroyer =
        new Thread(
            new Runnable() {
              public void run() {
                boolean continueRunning = true;
                do {

                  synchronized (lock) {
                    continueRunning = keepGoing[0];
                  }
                  try {
                    Index indx1 =
                        qs.createIndex(
                            "MarketValues",
                            IndexType.FUNCTIONAL,
                            "itr2.mktValue",
                            "/new_pos itr1, itr1.positions.values itr2");
                    Index indx2 =
                        qs.createIndex("Name", IndexType.FUNCTIONAL, "itr1.name", "/new_pos itr1");
                    Index indx3 =
                        qs.createIndex("nameIndex", IndexType.PRIMARY_KEY, "name", "/new_pos");
                    Index indx4 = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "id", "/new_pos");
                    Index indx5 =
                        qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/new_pos");
                    Index indx6 =
                        qs.createIndex(
                            "undefinedFieldIndex",
                            IndexType.FUNCTIONAL,
                            "undefinedTestField.toString",
                            "/new_pos");
                    Thread.sleep(800);
                    qs.removeIndex(indx1);
                    qs.removeIndex(indx2);
                    qs.removeIndex(indx3);
                    qs.removeIndex(indx4);
                    qs.removeIndex(indx5);
                    qs.removeIndex(indx6);
                  } catch (Throwable e) {
                    region.getCache().getLogger().error(e);
                    e.printStackTrace();
                    synchronized (lock) {
                      expectionOccured[0] = true;
                      keepGoing[0] = false;
                      continueRunning = false;
                    }
                  }
                } while (continueRunning);
              }
            });

    indexCreatorDestroyer.start();
    final Query q = qs.newQuery(queryStr);
    final int THREAD_COUNT = 10;
    Thread queryThreads[] = new Thread[THREAD_COUNT];
    final int numTimesToRun = 75;
    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i] =
          new Thread(
              new Runnable() {
                public void run() {
                  boolean continueRunning = true;
                  for (int i = 0; i < numTimesToRun && continueRunning; ++i) {
                    synchronized (lock) {
                      continueRunning = keepGoing[0];
                    }
                    try {
                      SelectResults sr = (SelectResults) q.execute();
                    } catch (Throwable e) {
                      e.printStackTrace();
                      region.getCache().getLogger().error(e);
                      synchronized (lock) {
                        expectionOccured[0] = true;
                        keepGoing[0] = false;
                        continueRunning = false;
                      }
                      break;
                    }
                  }
                }
              });
    }
    synchronized (lock) {
      assertFalse(expectionOccured[0]);
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i].start();
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i].join();
    }
    synchronized (lock) {
      keepGoing[0] = false;
    }

    indexCreatorDestroyer.join();
    synchronized (lock) {
      assertFalse(expectionOccured[0]);
    }
  }
  /** getWaitingThreads returns only and all waiting threads */
  public void testGetWaitingThreads() {
    final Mutex sync = new Mutex();
    final ConditionObject c = sync.newCondition();
    final BooleanLatch acquired1 = new BooleanLatch();
    final BooleanLatch acquired2 = new BooleanLatch();
    final Thread t1 =
        new Thread(
            new CheckedRunnable() {
              public void realRun() throws InterruptedException {
                sync.acquire();
                assertHasWaitersLocked(sync, c, NO_THREADS);
                assertTrue(sync.getWaitingThreads(c).isEmpty());
                assertTrue(acquired1.releaseShared(0));
                c.await();
                sync.release();
              }
            });

    final Thread t2 =
        new Thread(
            new CheckedRunnable() {
              public void realRun() throws InterruptedException {
                sync.acquire();
                assertHasWaitersLocked(sync, c, t1);
                assertTrue(sync.getWaitingThreads(c).contains(t1));
                assertFalse(sync.getWaitingThreads(c).isEmpty());
                assertEquals(1, sync.getWaitingThreads(c).size());
                assertTrue(acquired2.releaseShared(0));
                c.await();
                sync.release();
              }
            });

    sync.acquire();
    assertHasWaitersLocked(sync, c, NO_THREADS);
    assertFalse(sync.getWaitingThreads(c).contains(t1));
    assertFalse(sync.getWaitingThreads(c).contains(t2));
    assertTrue(sync.getWaitingThreads(c).isEmpty());
    assertEquals(0, sync.getWaitingThreads(c).size());
    sync.release();

    t1.start();
    acquired1.acquireShared(0);
    sync.acquire();
    assertHasWaitersLocked(sync, c, t1);
    assertTrue(sync.getWaitingThreads(c).contains(t1));
    assertFalse(sync.getWaitingThreads(c).contains(t2));
    assertFalse(sync.getWaitingThreads(c).isEmpty());
    assertEquals(1, sync.getWaitingThreads(c).size());
    sync.release();

    t2.start();
    acquired2.acquireShared(0);
    sync.acquire();
    assertHasWaitersLocked(sync, c, t1, t2);
    assertHasExclusiveQueuedThreads(sync, NO_THREADS);
    assertTrue(sync.getWaitingThreads(c).contains(t1));
    assertTrue(sync.getWaitingThreads(c).contains(t2));
    assertFalse(sync.getWaitingThreads(c).isEmpty());
    assertEquals(2, sync.getWaitingThreads(c).size());
    c.signalAll();
    assertHasWaitersLocked(sync, c, NO_THREADS);
    assertHasExclusiveQueuedThreads(sync, t1, t2);
    assertFalse(sync.getWaitingThreads(c).contains(t1));
    assertFalse(sync.getWaitingThreads(c).contains(t2));
    assertTrue(sync.getWaitingThreads(c).isEmpty());
    assertEquals(0, sync.getWaitingThreads(c).size());
    sync.release();

    awaitTermination(t1);
    awaitTermination(t2);
    assertHasWaitersUnlocked(sync, c, NO_THREADS);
  }