Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapPoolTimeoutTest.java
66645 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8277795
27
* @summary Multi-threaded client timeout tests for ldap pool
28
* @library /test/lib
29
* lib/
30
* @run testng/othervm LdapPoolTimeoutTest
31
*/
32
33
import org.testng.Assert;
34
import org.testng.annotations.Test;
35
36
import java.io.IOException;
37
import javax.naming.Context;
38
import javax.naming.NamingException;
39
import javax.naming.directory.InitialDirContext;
40
import java.util.ArrayList;
41
import java.util.Hashtable;
42
import java.util.List;
43
import java.util.concurrent.ExecutionException;
44
import java.util.concurrent.ExecutorService;
45
import java.util.concurrent.Executors;
46
import java.util.concurrent.Future;
47
import java.util.concurrent.TimeoutException;
48
import java.util.concurrent.TimeUnit;
49
50
import static jdk.test.lib.Utils.adjustTimeout;
51
import static org.testng.Assert.assertTrue;
52
import static org.testng.Assert.assertNotNull;
53
import static org.testng.Assert.expectThrows;
54
55
public class LdapPoolTimeoutTest {
56
/*
57
* Practical representation of an infinite timeout.
58
*/
59
private static final long INFINITY_MILLIS = adjustTimeout(20_000);
60
/*
61
* The acceptable variation in timeout measurements.
62
*/
63
private static final long TOLERANCE = adjustTimeout( 3_500);
64
65
private static final long CONNECT_MILLIS = adjustTimeout( 3_000);
66
private static final long READ_MILLIS = adjustTimeout(10_000);
67
68
static {
69
// a series of checks to make sure this timeouts configuration is
70
// consistent and the timeouts do not overlap
71
72
assert (TOLERANCE >= 0);
73
// context creation
74
assert (2 * CONNECT_MILLIS + TOLERANCE < READ_MILLIS);
75
// context creation immediately followed by search
76
assert (2 * CONNECT_MILLIS + READ_MILLIS + TOLERANCE < INFINITY_MILLIS);
77
}
78
79
@Test
80
public void test() throws Exception {
81
List<Future<?>> futures = new ArrayList<>();
82
ExecutorService executorService = Executors.newCachedThreadPool();
83
84
Hashtable<Object, Object> env = new Hashtable<>();
85
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
86
env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(READ_MILLIS));
87
env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(CONNECT_MILLIS));
88
env.put("com.sun.jndi.ldap.connect.pool", "true");
89
env.put(Context.PROVIDER_URL, "ldap://example.com:1234");
90
91
try {
92
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
93
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
94
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
95
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
96
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
97
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
98
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
99
futures.add(executorService.submit(() -> { attemptConnect(env); return null; }));
100
} finally {
101
executorService.shutdown();
102
}
103
int failedCount = 0;
104
for (var f : futures) {
105
try {
106
f.get();
107
} catch (ExecutionException e) {
108
failedCount++;
109
e.getCause().printStackTrace(System.out);
110
}
111
}
112
if (failedCount > 0)
113
throw new RuntimeException(failedCount + " (sub)tests failed");
114
}
115
116
private static void attemptConnect(Hashtable<Object, Object> env) throws Exception {
117
try {
118
LdapTimeoutTest.assertCompletion(CONNECT_MILLIS - 1000,
119
2 * CONNECT_MILLIS + TOLERANCE,
120
() -> new InitialDirContext(env));
121
} catch (RuntimeException e) {
122
String msg = e.getCause() == null ? e.getMessage() : e.getCause().getMessage();
123
System.err.println("MSG RTE: " + msg);
124
// assertCompletion may wrap a CommunicationException in an RTE
125
assertNotNull(msg);
126
assertTrue(msg.contains("Network is unreachable")
127
|| msg.contains("No route to host"));
128
} catch (NamingException ex) {
129
String msg = ex.getCause() == null ? ex.getMessage() : ex.getCause().getMessage();
130
System.err.println("MSG: " + msg);
131
assertTrue(msg != null &&
132
(msg.contains("Network is unreachable")
133
|| msg.contains("Timed out waiting for lock")
134
|| msg.contains("Connect timed out")
135
|| msg.contains("Timeout exceeded while waiting for a connection")));
136
} catch (Throwable t) {
137
throw new RuntimeException(t);
138
}
139
}
140
141
}
142
143
144