/** If the update was successful restart the MainActivity */
      @Override
      protected void onPostExecute(String result) {
        super.onPostExecute(result);

        progr_dlg.dismiss();

        // After all the update is (succesfully) done we have to restart
        // the main activity

        if (DONE.equals(result)) {
          // either we were started tru intent (return to main)
          if (context instanceof PreferencesActivity) {
            Activity a = (Activity) context;
            a.setResult(PreferencesActivity.RESULT_DATA_UPDATED);
            // this will call on activity result
            a.finish();
          }

          // or directly by passing context: restart directly
          if (context instanceof MainActivity) {
            MainActivity mainActivity = (MainActivity) context;
            mainActivity.restartActivity(0);
          }
        }
      }
Exemplo n.º 2
0
  /** {@inheritDoc} */
  @Override
  @NotNull
  public String getTimeout() {
    if (!(DONE.equals(status) || RUNNING.equals(status))) {
      return TIMER_STUB;
    }

    RunnerMetric timeoutMetric = getRunnerMetricByName(RunnerMetric.TERMINATION_TIME);

    if (timeoutMetric != null) {
      return getTimeOut(timeoutMetric);
    }

    RunnerMetric lifeTimeMetric = getRunnerMetricByName(RunnerMetric.LIFETIME);

    if (lifeTimeMetric != null && NEW.equals(descriptor.getStatus())) {
      return getLifeTime(lifeTimeMetric);
    }

    return TIMER_STUB;
  }
  /**
   * Launches a group of 2 * getMaxActive() threads, each of which will attempt to obtain a
   * connection from the pool, hold it for <holdTime> ms, and then return it to the pool. If
   * <loopOnce> is false, threads will continue this process indefinitely. If <expectError> is true,
   * exactly 1/2 of the threads are expected to either throw exceptions or fail to complete. If
   * <expectError> is false, all threads are expected to complete successfully.
   *
   * @param holdTime time in ms that a thread holds a connection before returning it to the pool
   * @param expectError whether or not an error is expected
   * @param loopOnce whether threads should complete the borrow - hold - return cycle only once, or
   *     loop indefinitely
   * @param maxWait passed in by client - has no impact on the test itself, but does get reported
   * @throws Exception
   */
  protected void multipleThreads(
      final int holdTime, final boolean expectError, final boolean loopOnce, final long maxWait)
      throws Exception {
    long startTime = timeStamp();
    final PoolTest[] pts = new PoolTest[2 * getMaxActive()];
    // Catch Exception so we can stop all threads if one fails
    ThreadGroup threadGroup =
        new ThreadGroup("foo") {
          public void uncaughtException(Thread t, Throwable e) {
            for (int i = 0; i < pts.length; i++) {
              pts[i].stop();
            }
          }
        };
    // Create all the threads
    for (int i = 0; i < pts.length; i++) {
      pts[i] = new PoolTest(threadGroup, holdTime, expectError, loopOnce);
    }
    // Start all the threads
    for (int i = 0; i < pts.length; i++) {
      pts[i].start();
    }

    // Give all threads a chance to start and succeed
    Thread.sleep(300L);

    // Stop threads
    for (int i = 0; i < pts.length; i++) {
      pts[i].stop();
    }

    /*
     * Wait for all threads to terminate.
     * This is essential to ensure that all threads have a chance to update success[0]
     * and to ensure that the variable is published correctly.
     */
    int done = 0;
    int failed = 0;
    int didNotRun = 0;
    int loops = 0;
    for (int i = 0; i < pts.length; i++) {
      final PoolTest poolTest = pts[i];
      poolTest.thread.join();
      loops += poolTest.loops;
      final String state = poolTest.state;
      if (DONE.equals(state)) {
        done++;
      }
      if (poolTest.loops == 0) {
        didNotRun++;
      }
      final Throwable thrown = poolTest.thrown;
      if (thrown != null) {
        failed++;
        if (!expectError || !(thrown instanceof SQLException)) {
          System.out.println("Unexpected error: " + thrown.getMessage());
        }
      }
    }

    long time = timeStamp() - startTime;
    System.out.println(
        "Multithread test time = "
            + time
            + " ms. Threads: "
            + pts.length
            + ". Loops: "
            + loops
            + ". Hold time: "
            + holdTime
            + ". Maxwait: "
            + maxWait
            + ". Done: "
            + done
            + ". Did not run: "
            + didNotRun
            + ". Failed: "
            + failed
            + ". expectError: "
            + expectError);
    if (expectError) {
      if (DISPLAY_THREAD_DETAILS || (pts.length / 2 != failed)) {
        long offset =
            pts[0].created
                - 1000; // To reduce size of output numbers, but ensure they have 4 digits
        System.out.println("Offset: " + offset);
        for (int i = 0; i < pts.length; i++) {
          PoolTest pt = pts[i];
          System.out.println(
              "Pre: "
                  + (pt.preconnected - offset) // First, so can sort on this easily
                  + ". Post: "
                  + (pt.postconnected != 0 ? Long.toString(pt.postconnected - offset) : "-")
                  + ". Hash: "
                  + pt.connHash
                  + ". Startup: "
                  + (pt.started - pt.created)
                  + ". getConn(): "
                  + (pt.connected != 0 ? Long.toString(pt.connected - pt.preconnected) : "-")
                  + ". Runtime: "
                  + (pt.ended - pt.started)
                  + ". IDX: "
                  + i
                  + ". Loops: "
                  + pt.loops
                  + ". State: "
                  + pt.state
                  + ". thrown: "
                  + pt.thrown
                  + ".");
        }
      }
      if (didNotRun > 0) {
        System.out.println("NOTE: some threads did not run the code: " + didNotRun);
      }
      // Perform initial sanity check:
      assertTrue("Expected some of the threads to fail", failed > 0);
      // Assume that threads that did not run would have timed out.
      assertEquals(
          "WARNING: Expected half the threads to fail", pts.length / 2, failed + didNotRun);
    } else {
      assertEquals("Did not expect any threads to fail", 0, failed);
    }
  }