Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapPoolTimeoutTest.java
66645 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 827779526* @summary Multi-threaded client timeout tests for ldap pool27* @library /test/lib28* lib/29* @run testng/othervm LdapPoolTimeoutTest30*/3132import org.testng.Assert;33import org.testng.annotations.Test;3435import java.io.IOException;36import javax.naming.Context;37import javax.naming.NamingException;38import javax.naming.directory.InitialDirContext;39import java.util.ArrayList;40import java.util.Hashtable;41import java.util.List;42import java.util.concurrent.ExecutionException;43import java.util.concurrent.ExecutorService;44import java.util.concurrent.Executors;45import java.util.concurrent.Future;46import java.util.concurrent.TimeoutException;47import java.util.concurrent.TimeUnit;4849import static jdk.test.lib.Utils.adjustTimeout;50import static org.testng.Assert.assertTrue;51import static org.testng.Assert.assertNotNull;52import static org.testng.Assert.expectThrows;5354public class LdapPoolTimeoutTest {55/*56* Practical representation of an infinite timeout.57*/58private static final long INFINITY_MILLIS = adjustTimeout(20_000);59/*60* The acceptable variation in timeout measurements.61*/62private static final long TOLERANCE = adjustTimeout( 3_500);6364private static final long CONNECT_MILLIS = adjustTimeout( 3_000);65private static final long READ_MILLIS = adjustTimeout(10_000);6667static {68// a series of checks to make sure this timeouts configuration is69// consistent and the timeouts do not overlap7071assert (TOLERANCE >= 0);72// context creation73assert (2 * CONNECT_MILLIS + TOLERANCE < READ_MILLIS);74// context creation immediately followed by search75assert (2 * CONNECT_MILLIS + READ_MILLIS + TOLERANCE < INFINITY_MILLIS);76}7778@Test79public void test() throws Exception {80List<Future<?>> futures = new ArrayList<>();81ExecutorService executorService = Executors.newCachedThreadPool();8283Hashtable<Object, Object> env = new Hashtable<>();84env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");85env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(READ_MILLIS));86env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(CONNECT_MILLIS));87env.put("com.sun.jndi.ldap.connect.pool", "true");88env.put(Context.PROVIDER_URL, "ldap://example.com:1234");8990try {91futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));92futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));93futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));94futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));95futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));96futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));97futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));98futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));99} finally {100executorService.shutdown();101}102int failedCount = 0;103for (var f : futures) {104try {105f.get();106} catch (ExecutionException e) {107failedCount++;108e.getCause().printStackTrace(System.out);109}110}111if (failedCount > 0)112throw new RuntimeException(failedCount + " (sub)tests failed");113}114115private static void attemptConnect(Hashtable<Object, Object> env) throws Exception {116try {117LdapTimeoutTest.assertCompletion(CONNECT_MILLIS - 1000,1182 * CONNECT_MILLIS + TOLERANCE,119() -> new InitialDirContext(env));120} catch (RuntimeException e) {121String msg = e.getCause() == null ? e.getMessage() : e.getCause().getMessage();122System.err.println("MSG RTE: " + msg);123// assertCompletion may wrap a CommunicationException in an RTE124assertNotNull(msg);125assertTrue(msg.contains("Network is unreachable")126|| msg.contains("No route to host"));127} catch (NamingException ex) {128String msg = ex.getCause() == null ? ex.getMessage() : ex.getCause().getMessage();129System.err.println("MSG: " + msg);130assertTrue(msg != null &&131(msg.contains("Network is unreachable")132|| msg.contains("Timed out waiting for lock")133|| msg.contains("Connect timed out")134|| msg.contains("Timeout exceeded while waiting for a connection")));135} catch (Throwable t) {136throw new RuntimeException(t);137}138}139140}141142143144