protected void closeWebDriver(Thread thread) {
    ALL_WEB_DRIVERS_THREADS.remove(thread);
    WebDriver webdriver = THREAD_WEB_DRIVER.remove(thread.getId());

    if (webdriver != null && !holdBrowserOpen) {
      log.info("Close webdriver: " + thread.getId() + " -> " + webdriver);

      long start = System.currentTimeMillis();

      Thread t = new Thread(new CloseBrowser(webdriver));
      t.setDaemon(true);
      t.start();

      try {
        t.join(closeBrowserTimeoutMs);
      } catch (InterruptedException e) {
        log.log(FINE, "Failed to close webdriver in " + closeBrowserTimeoutMs + " milliseconds", e);
      }

      long duration = System.currentTimeMillis() - start;
      if (duration >= closeBrowserTimeoutMs) {
        log.severe("Failed to close webdriver in " + closeBrowserTimeoutMs + " milliseconds");
      } else if (duration > 200) {
        log.info("Closed webdriver in " + duration + " ms");
      } else {
        log.fine("Closed webdriver in " + duration + " ms");
      }
    }
  }
  public void test(TestHarness th) {
    Thread notifyThreadArray = new NotifyThreadArray();
    notifyThreadArray.start();

    try {
      synchronized (a) {
        while (!done) {
          a.wait();
        }
      }

      notifyThreadArray.join();
    } catch (InterruptedException e) {
      th.fail("Unexpected exception: " + e);
    }

    done = false;

    Thread notifyThreadMultiArray = new NotifyThreadMultiArray();
    notifyThreadMultiArray.start();

    try {
      synchronized (b) {
        while (!done) {
          b.wait();
        }
      }

      notifyThreadMultiArray.join();
    } catch (InterruptedException e) {
      th.fail("Unexpected exception: " + e);
    }

    th.check(true);
  }
 private BenchmarkTcpClient(final String host, final int port)
     throws IOException, InterruptedException {
   super();
   final Thread eventLoopThread = eventLoop.start();
   eventLoop.connect(new InetSocketAddress(host, port), this);
   eventLoopThread.join();
 }
  public void testListenerDoesntDeadlockOnStopAndWaitFromTerminated() throws Exception {
    final NoOpThreadedService service = new NoOpThreadedService();
    service.addListener(
        new Listener() {
          @Override
          public void starting() {}

          @Override
          public void running() {}

          @Override
          public void stopping(State from) {}

          @Override
          public void terminated(State from) {
            service.stopAndWait();
          }

          @Override
          public void failed(State from, Throwable failure) {}
        },
        MoreExecutors.sameThreadExecutor());
    service.startAndWait();

    Thread thread =
        new Thread() {
          @Override
          public void run() {
            service.stopAndWait();
          }
        };
    thread.start();
    thread.join(100);
    assertFalse(thread + " is deadlocked", thread.isAlive());
  }
Example #5
0
  // Private since we can't add a vtable slot in 4.1.x.
  private void exitNoChecks(int status) {
    if (runShutdownHooks()) exitInternal(status);

    // Someone else already called runShutdownHooks().
    // Make sure we are not/no longer in the shutdownHooks set.
    // And wait till the thread that is calling runShutdownHooks() finishes.
    synchronized (libpath) {
      if (shutdownHooks != null) {
        shutdownHooks.remove(Thread.currentThread());
        // Interrupt the exit sequence thread, in case it was waiting
        // inside a join on our thread.
        exitSequence.interrupt();
        // Shutdown hooks are still running, so we clear status to
        // make sure we don't halt.
        status = 0;
      }
    }

    // If exit() is called again after the shutdown hooks have run, but
    // while finalization for exit is going on and the status is non-zero
    // we halt immediately.
    if (status != 0) exitInternal(status);

    while (true)
      try {
        exitSequence.join();
      } catch (InterruptedException e) {
        // Ignore, we've suspended indefinitely to let all shutdown
        // hooks complete, and to let any non-zero exits through, because
        // this is a duplicate call to exit(0).
      }
  }
  @Test
  public void shouldAllowTwoInstancesOfFirefoxAtTheSameTimeInDifferentThreads()
      throws InterruptedException {
    class FirefoxRunner implements Runnable {
      private volatile WebDriver myDriver;
      private final String url;

      public FirefoxRunner(String url) {
        this.url = url;
      }

      public void run() {
        myDriver = newFirefoxDriver();
        myDriver.get(url);
      }

      public void quit() {
        if (myDriver != null) {
          myDriver.quit();
        }
      }

      public void assertOnRightPage() {
        assertEquals(url, myDriver.getCurrentUrl());
      }
    }

    FirefoxRunner runnable1 = new FirefoxRunner(pages.formPage);
    Thread thread1 = new Thread(runnable1); // Thread safety reviewed
    FirefoxRunner runnable2 = new FirefoxRunner(pages.xhtmlTestPage);
    Thread thread2 = new Thread(runnable2); // Thread safety reviewed

    try {
      thread1.start();
      thread2.start();

      thread1.join();
      thread2.join();

      runnable1.assertOnRightPage();
      runnable1.assertOnRightPage();
    } finally {
      runnable1.quit();
      runnable2.quit();
    }
  }
Example #7
0
 /**
  * As the result of Updater output depends on the thread's completion, it is necessary to wait for
  * the thread to finish before alloowing anyone to check the result.
  */
 public void waitForThread() {
   if (thread.isAlive()) {
     try {
       thread.join();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
Example #8
0
  public void test(TestHarness th) {
    try {
      Thread t = new RuntimeExceptionThread();
      t.start();
      t.join();
      result = true;
    } catch (InterruptedException e) {
      th.fail("unexpected InterruptedException");
    }

    th.check(result);
  }
  /* Iterates over all application hooks creating a new thread for each
   * to run in. Hooks are run concurrently and this method waits for
   * them to finish.
   */
  public void run() {
    Collection<Thread> threads;
    synchronized (ApplicationShutdownHooks.class) {
      threads = hooks.keySet();
      hooks = null;
    }

    for (Thread hook : threads) {
      hook.start();
    }
    for (Thread hook : threads) {
      try {
        hook.join();
      } catch (InterruptedException x) {
      }
    }
  }
  public void testThreadedServiceStartStopIdempotenceAfterWait() throws Throwable {
    ThreadedService service = new ThreadedService();

    service.start().get();
    service.start();
    assertEquals(State.RUNNING, service.state());

    service.awaitRunChecks();

    service.stop().get();
    service.stop();
    assertEquals(State.TERMINATED, service.state());

    executionThread.join();

    throwIfSet(thrownByExecutionThread);
  }
Example #11
0
  public static void audioQuit() {
    if (mAudioThread != null) {
      try {
        mAudioThread.join();
      } catch (Exception e) {
        Log.v("SDL", "Problem stopping audio thread: " + e);
      }
      mAudioThread = null;

      // Log.v("SDL", "Finished waiting for audio thread");
    }

    if (mAudioTrack != null) {
      mAudioTrack.stop();
      mAudioTrack = null;
    }
  }
 public void testAddListenerAfterFailureDoesntCauseDeadlock() throws InterruptedException {
   final StartFailingService service = new StartFailingService();
   service.start();
   assertEquals(State.FAILED, service.state());
   service.addListener(new RecordingListener(service), MoreExecutors.sameThreadExecutor());
   Thread thread =
       new Thread() {
         @Override
         public void run() {
           // Internally start() grabs a lock, this could be any such method on AbstractService.
           service.start();
         }
       };
   thread.start();
   thread.join(100);
   assertFalse(thread + " is deadlocked", thread.isAlive());
 }
 protected WebDriver createWebDriverWithTimeout() {
   for (int i = 0; i < 3; i++) {
     CreateWebdriver create = new CreateWebdriver();
     Thread t = new Thread(create);
     t.setDaemon(true);
     t.start();
     try {
       t.join(openBrowserTimeoutMs);
       if (create.webdriver != null) {
         return create.webdriver;
       }
     } catch (InterruptedException e) {
       throw runtime(e);
     }
   }
   throw new RuntimeException("Could not create webdriver in " + openBrowserTimeoutMs + " ms");
 }
Example #14
0
 /**
  * Waits for the runtime thread to stop. This interrupts the thread currently running the
  * runnable and then waits for it to exit.
  */
 public void stop() {
   Thread threadToStop;
   synchronized (this) {
     threadToStop = thread;
     thread = null;
   }
   if (threadToStop == null) {
     throw new IllegalStateException("not running");
   }
   threadToStop.interrupt();
   while (true) {
     try {
       threadToStop.join();
       return;
     } catch (InterruptedException ignored) {
     }
   }
 }
Example #15
0
  /**
   * Blocks the current Thread (<code>Thread.currentThread()</code>) until the receiver finishes its
   * execution and dies or the specified timeout expires, whatever happens first.
   *
   * @param millis The maximum time to wait (in milliseconds).
   * @param nanos Extra nanosecond precision
   * @throws InterruptedException if <code>interrupt()</code> was called for the receiver while it
   *     was in the <code>join()</code> call
   * @see Object#notifyAll
   * @see java.lang.ThreadDeath
   */
  public final void join(long millis, int nanos) throws InterruptedException {
    if (millis < 0 || nanos < 0 || nanos >= NANOS_PER_MILLI) {
      throw new IllegalArgumentException("bad timeout: millis=" + millis + ",nanos=" + nanos);
    }

    // avoid overflow: if total > 292,277 years, just wait forever
    boolean overflow = millis >= (Long.MAX_VALUE - nanos) / NANOS_PER_MILLI;
    boolean forever = (millis | nanos) == 0;
    if (forever | overflow) {
      join();
      return;
    }

    VMThread t = vmThread;
    if (t == null) {
      return;
    }

    synchronized (t) {
      if (!isAlive()) {
        return;
      }

      // guaranteed not to overflow
      long nanosToWait = millis * NANOS_PER_MILLI + nanos;

      // wait until this thread completes or the timeout has elapsed
      long start = System.nanoTime();
      while (true) {
        t.wait(millis, nanos);
        if (!isAlive()) {
          break;
        }
        long nanosElapsed = System.nanoTime() - start;
        long nanosRemaining = nanosToWait - nanosElapsed;
        if (nanosRemaining <= 0) {
          break;
        }
        millis = nanosRemaining / NANOS_PER_MILLI;
        nanos = (int) (nanosRemaining - millis * NANOS_PER_MILLI);
      }
    }
  }
Example #16
0
  // Called when we lose the surface
  public void surfaceDestroyed(SurfaceHolder holder) {
    // Log.v("SDL", "surfaceDestroyed()");

    // Send a quit message to the application
    SDLActivity.nativeQuit();

    // Now wait for the SDL thread to quit
    if (mSDLThread != null) {
      try {
        mSDLThread.join();
      } catch (Exception e) {
        Log.v("SDL", "Problem stopping thread: " + e);
      }
      mSDLThread = null;

      // Log.v("SDL", "Finished waiting for SDL thread");
    }

    enableSensor(Sensor.TYPE_ACCELEROMETER, false);
  }
Example #17
0
 /**
  * Blocks the current Thread (<code>Thread.currentThread()</code>) until the receiver finishes its
  * execution and dies or the specified timeout expires, whatever happens first.
  *
  * @param millis The maximum time to wait (in milliseconds).
  * @throws InterruptedException if <code>interrupt()</code> was called for the receiver while it
  *     was in the <code>join()</code> call
  * @see Object#notifyAll
  * @see java.lang.ThreadDeath
  */
 public final void join(long millis) throws InterruptedException {
   join(millis, 0);
 }