/* * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * These are timeout tests and they are run in parallel to reduce the total * amount of run time. * * Currently it doesn't seem possible to instruct JTREG to run TestNG test * methods in parallel. That said, this JTREG test is still * a "TestNG-flavored" test for the sake of having org.testng.Assert * capability.
*/
@Test publicvoid test() throws Exception {
List<Future<?>> futures = new ArrayList<>();
ExecutorService executorService = Executors.newCachedThreadPool(); try {
futures.add(executorService.submit(() -> { test1(); returnnull; }));
futures.add(executorService.submit(() -> { test2(); returnnull; }));
futures.add(executorService.submit(() -> { test3(); returnnull; }));
futures.add(executorService.submit(() -> { test4(); returnnull; }));
futures.add(executorService.submit(() -> { test5(); returnnull; }));
futures.add(executorService.submit(() -> { test6(); returnnull; }));
futures.add(executorService.submit(() -> { test7(); returnnull; }));
} finally {
executorService.shutdown();
} int failedCount = 0; for (var f : futures) { try {
f.get();
} catch (ExecutionException e) {
failedCount++;
e.getCause().printStackTrace(System.out);
}
} if (failedCount > 0) thrownew RuntimeException(failedCount + " (sub)tests failed");
}
staticvoid test1() throws Exception {
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // Here and in the other tests it's important to close the server as // calling `thread.interrupt` from assertion may not be enough // (depending on where the blocking call has stuck) try (TestServer server = new NotBindableServer()) {
env.put(Context.PROVIDER_URL, urlTo(server));
server.start(); // Here and in the other tests joining done purely to reduce timing // jitter. Commenting out or removing that should not make the test // incorrect. (ServerSocket can accept connection as soon as it is // bound, not need to call `accept` before that.)
server.starting().join();
assertIncompletion(INFINITY_MILLIS, () -> new InitialDirContext(env));
}
}
staticvoid test2() throws Exception {
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(CONNECT_MILLIS)); try (TestServer server = new BindableButNotReadableServer()) {
env.put(Context.PROVIDER_URL, urlTo(server));
server.start();
server.starting().join();
InitialDirContext ctx = new InitialDirContext(env);
SearchControls scl = new SearchControls();
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
assertIncompletion(INFINITY_MILLIS,
() -> ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl));
}
}
staticvoid test3() throws Exception {
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); try (TestServer server = new BindableButNotReadableServer()) {
env.put(Context.PROVIDER_URL, urlTo(server));
server.start();
server.starting().join();
InitialDirContext ctx = new InitialDirContext(env);
SearchControls scl = new SearchControls();
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
assertIncompletion(INFINITY_MILLIS,
() -> ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl));
}
}
/* * Asserts that the specified executable yields a result or an exception * within the specified time frame. Interrupts the executable * unconditionally. * * If the executable yields a result or an exception within the specified * time frame, the result will be returned and the exception will be * rethrown respectively in a transparent fashion as if the executable was * executed directly.
*/ publicstatic <T> T assertCompletion(long loMillis, long hiMillis,
Callable<T> code) throws Throwable { if (loMillis < 0 || hiMillis < 0 || loMillis > hiMillis) { thrownew IllegalArgumentException("loMillis=" + loMillis + ", hiMillis=" + hiMillis);
}
Objects.requireNonNull(code);
// this queue acts both as an exchange point and a barrier
SynchronousQueue<Long> startTime = new SynchronousQueue<>();
Callable<T> wrappedTask = () -> { // by the time this value reaches the "stopwatch" thread it might be // well outdated and that's okay, we will adjust the wait time
startTime.put(System.nanoTime()); return code.call();
};
FutureTask<T> task = new FutureTask<>(wrappedTask); Thread t = newThread(task);
t.start();
finallong startNanos; try {
startNanos = startTime.take(); // (1) wait for the initial time mark
} catch (Throwable e) {
t.interrupt(); throw e;
}
try {
T r = task.get(waitTime, MILLISECONDS); // (3) wait for the task to complete long elapsed = NANOSECONDS.toMillis(System.nanoTime() - startNanos); if (elapsed < loMillis || elapsed > hiMillis) { thrownew RuntimeException(format( "After %s ms. (waitTime %s ms.) returned result '%s'", elapsed, waitTime, r));
} return r;
} catch (ExecutionException e) { long elapsed = NANOSECONDS.toMillis(System.nanoTime() - startNanos); if (elapsed < loMillis || elapsed > hiMillis) { thrownew RuntimeException(format( "After %s ms. (waitTime %s ms.) thrown exception", elapsed, waitTime), e);
} throw e.getCause();
} catch (TimeoutException e) { // We trust timed get not to throw TimeoutException prematurely // (i.e. before the wait time elapses) long elapsed = NANOSECONDS.toMillis(System.nanoTime() - startNanos); thrownew RuntimeException(format( "After %s ms. (waitTime %s ms.) is incomplete", elapsed, waitTime));
} finally {
t.interrupt();
}
}
/* * Asserts that the specified executable yields no result and no exception * for at least the specified amount of time. Interrupts the executable * unconditionally.
*/ publicstaticvoid assertIncompletion(long millis, Callable<?> code) throws Exception
{ if (millis < 0) { thrownew IllegalArgumentException("millis=" + millis);
}
Objects.requireNonNull(code);
// this queue acts both as an exchange point and a barrier
SynchronousQueue<Long> startTime = new SynchronousQueue<>();
Callable<?> wrappedTask = () -> { // by the time this value reaches the "stopwatch" thread it might be // well outdated and that's okay, we will adjust the wait time
startTime.put(System.nanoTime()); return code.call();
};
FutureTask<?> task = new FutureTask<>(wrappedTask); Thread t = newThread(task);
t.start();
finallong startNanos; try {
startNanos = startTime.take(); // (1) wait for the initial time mark
} catch (Throwable e) {
t.interrupt(); throw e;
}
/* * A diagnostic aid that might help with debugging timeout issues. The idea * is to continuously measure accuracy and responsiveness of the system that * runs this test. If the system is overwhelmed (with something else), it * might affect the test run. At the very least we will have traces of that * in the logs. * * This utility does not automatically scale up test timeouts, it simply * gathers information.
*/ privatestaticvoid startAuxiliaryDiagnosticOutput() {
System.out.printf("Starting diagnostic output (probe)%n"); Thread t = newThread(() -> { for (int i = 0; ; i = ((i % 20) + 1)) { // 500, 1_000, 1_500, ..., 9_500, 10_000, 500, 1_000, ... long expected = i * 500; long start = System.nanoTime(); try {
MILLISECONDS.sleep(expected);
} catch (InterruptedException e) { return;
} long stop = System.nanoTime(); long actual = NANOSECONDS.toMillis(stop - start);
System.out.printf("(probe) expected [ms.]: %s, actual [ms.]: %s%n",
expected, actual);
}
}, "probe");
t.setDaemon(true);
t.start();
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.3 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.