コード例 #1
0
ファイル: TimeOutShrink.java プロジェクト: FauxFaux/jdk9-jdk
public class TimeOutShrink {
  static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  static final long KEEPALIVE_MS = 12L;

  static void checkPoolSizes(ThreadPoolExecutor pool, int size, int core, int max) {
    equal(pool.getPoolSize(), size);
    equal(pool.getCorePoolSize(), core);
    equal(pool.getMaximumPoolSize(), max);
  }

  private static void realMain(String[] args) throws Throwable {
    final int n = 4;
    final CyclicBarrier barrier = new CyclicBarrier(2 * n + 1);
    final ThreadPoolExecutor pool =
        new ThreadPoolExecutor(
            n, 2 * n, KEEPALIVE_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
    final Runnable r =
        new Runnable() {
          public void run() {
            try {
              barrier.await();
              barrier.await();
            } catch (Throwable t) {
              unexpected(t);
            }
          }
        };

    for (int i = 0; i < 2 * n; i++) pool.execute(r);
    barrier.await();
    checkPoolSizes(pool, 2 * n, n, 2 * n);
    barrier.await();
    long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
    for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
      check((sleepyTime += nap) <= LONG_DELAY_MS);
      Thread.sleep(nap);
    }
    checkPoolSizes(pool, n, n, 2 * n);
    Thread.sleep(nap);
    checkPoolSizes(pool, n, n, 2 * n);
    pool.shutdown();
    check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  }

  // --------------------- Infrastructure ---------------------------
  static volatile int passed = 0, failed = 0;

  static void pass() {
    passed++;
  }

  static void fail() {
    failed++;
    Thread.dumpStack();
  }

  static void fail(String msg) {
    System.out.println(msg);
    fail();
  }

  static void unexpected(Throwable t) {
    failed++;
    t.printStackTrace();
  }

  static void check(boolean cond) {
    if (cond) pass();
    else fail();
  }

  static void equal(Object x, Object y) {
    if (x == null ? y == null : x.equals(y)) pass();
    else fail(x + " not equal to " + y);
  }

  public static void main(String[] args) throws Throwable {
    try {
      realMain(args);
    } catch (Throwable t) {
      unexpected(t);
    }
    System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
    if (failed > 0) throw new AssertionError("Some tests failed");
  }
}
コード例 #2
0
  private static boolean doTest(String testId, String arg) throws Exception {
    List<String> args = new ArrayList<>();
    args.add("-XX:+UsePerfData");
    args.addAll(Utils.getVmOptions());
    args.add("-cp");
    args.add(TEST_CLASSPATH);

    if (arg != null) {
      args.add(arg);
    }
    args.add("TestApplication");
    ProcessBuilder server =
        ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));

    Process serverPrc = null, clientPrc = null;
    try {
      final AtomicReference<String> port = new AtomicReference<>();

      serverPrc =
          ProcessTools.startProcess(
              "TestApplication(" + testId + ")",
              server,
              (String line) -> {
                if (line.startsWith("port:")) {
                  port.set(line.split("\\:")[1]);
                } else if (line.startsWith("waiting")) {
                  return true;
                }
                return false;
              });

      System.out.println("Attaching test manager:");
      System.out.println("=========================");
      System.out.println("  PID           : " + serverPrc.getPid());
      System.out.println("  shutdown port : " + port.get());

      ProcessBuilder client =
          ProcessTools.createJavaProcessBuilder(
              "-cp",
              TEST_CLASSPATH,
              "--add-exports",
              "java.management/sun.management=ALL-UNNAMED",
              "TestManager",
              String.valueOf(serverPrc.getPid()),
              port.get(),
              "true");

      clientPrc =
          ProcessTools.startProcess(
              "TestManager",
              client,
              (String line) -> line.startsWith("Starting TestManager for PID"));

      int clientExitCode = clientPrc.waitFor();
      int serverExitCode = serverPrc.waitFor();
      return clientExitCode == 0 && serverExitCode == 0;
    } finally {
      if (clientPrc != null) {
        System.out.println("Stopping process " + clientPrc);
        clientPrc.destroy();
        clientPrc.waitFor();
      }
      if (serverPrc != null) {
        System.out.println("Stopping process " + serverPrc);
        serverPrc.destroy();
        serverPrc.waitFor();
      }
    }
  }