Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/ext/ExtendedSocketOptionsTest.java
41149 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
import org.testng.Assert;
25
import org.testng.annotations.Test;
26
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.List;
30
import java.util.concurrent.Callable;
31
import java.util.concurrent.CountDownLatch;
32
import java.util.concurrent.ExecutorService;
33
import java.util.concurrent.Executors;
34
import java.util.concurrent.Future;
35
36
/**
37
* @test
38
* @bug 8260366
39
* @summary Verify that concurrent classloading of sun.net.ext.ExtendedSocketOptions and
40
* jdk.net.ExtendedSocketOptions doesn't lead to a deadlock
41
* @modules java.base/sun.net.ext:open
42
* jdk.net
43
* @run testng/othervm ExtendedSocketOptionsTest
44
* @run testng/othervm ExtendedSocketOptionsTest
45
* @run testng/othervm ExtendedSocketOptionsTest
46
* @run testng/othervm ExtendedSocketOptionsTest
47
* @run testng/othervm ExtendedSocketOptionsTest
48
*/
49
public class ExtendedSocketOptionsTest {
50
51
/**
52
* Loads {@code jdk.net.ExtendedSocketOptions} and {@code sun.net.ext.ExtendedSocketOptions}
53
* and invokes {@code sun.net.ext.ExtendedSocketOptions#getInstance()} concurrently in a thread
54
* of their own and expects the classloading of both those classes
55
* to succeed. Additionally, after these tasks are done, calls the
56
* sun.net.ext.ExtendedSocketOptions#getInstance() and expects it to return a registered
57
* ExtendedSocketOptions instance.
58
*/
59
@Test
60
public void testConcurrentClassLoad() throws Exception {
61
final CountDownLatch taskTriggerLatch = new CountDownLatch(4);
62
final List<Callable<?>> tasks = new ArrayList<>();
63
tasks.add(new Task("jdk.net.ExtendedSocketOptions", taskTriggerLatch));
64
tasks.add(new Task("sun.net.ext.ExtendedSocketOptions", taskTriggerLatch));
65
// add a couple of tasks which call sun.net.ext.ExtendedSocketOptions#getInstance
66
tasks.add(new GetInstanceTask(taskTriggerLatch));
67
tasks.add(new GetInstanceTask(taskTriggerLatch));
68
final ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
69
try {
70
final Future<?>[] results = new Future[tasks.size()];
71
// submit
72
int i = 0;
73
for (final Callable<?> task : tasks) {
74
results[i++] = executor.submit(task);
75
}
76
// wait for completion
77
for (i = 0; i < tasks.size(); i++) {
78
results[i].get();
79
}
80
} finally {
81
executor.shutdownNow();
82
}
83
// check that the sun.net.ext.ExtendedSocketOptions#getInstance() does indeed return
84
// the registered instance
85
final Object extSocketOptions = callSunNetExtSocketOptionsGetInstance();
86
Assert.assertNotNull(extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" +
87
" unexpectedly returned null");
88
// now verify that each call to getInstance(), either in the tasks or here, returned the exact
89
// same instance of ExtendedSocketOptions
90
Assert.assertEquals(2, GetInstanceTask.extendedSocketOptionsInstances.size());
91
for (final Object inst : GetInstanceTask.extendedSocketOptionsInstances) {
92
Assert.assertSame(inst, extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" +
93
" returned different instances");
94
}
95
}
96
97
/**
98
* Reflectively calls sun.net.ext.ExtendedSocketOptions#getInstance() and returns
99
* the result
100
*/
101
private static Object callSunNetExtSocketOptionsGetInstance() throws Exception {
102
final Class<?> k = Class.forName("sun.net.ext.ExtendedSocketOptions");
103
return k.getDeclaredMethod("getInstance").invoke(null);
104
}
105
106
private static class Task implements Callable<Class<?>> {
107
private final String className;
108
private final CountDownLatch latch;
109
110
private Task(final String className, final CountDownLatch latch) {
111
this.className = className;
112
this.latch = latch;
113
}
114
115
@Override
116
public Class<?> call() {
117
System.out.println(Thread.currentThread().getName() + " loading " + this.className);
118
try {
119
// let the other tasks know we are ready to trigger our work
120
latch.countDown();
121
// wait for the other task to let us know they are ready to trigger their work too
122
latch.await();
123
return Class.forName(this.className);
124
} catch (Exception e) {
125
throw new RuntimeException(e);
126
}
127
}
128
}
129
130
private static class GetInstanceTask implements Callable<Object> {
131
// keeps track of the instances returned by calls to sun.nex.ext.ExtendedSocketOptions#getInstance()
132
// by the GetInstanceTask(s)
133
private static final List<Object> extendedSocketOptionsInstances = Collections.synchronizedList(new ArrayList<>());
134
private final CountDownLatch latch;
135
136
private GetInstanceTask(final CountDownLatch latch) {
137
this.latch = latch;
138
}
139
140
@Override
141
public Object call() {
142
System.out.println(Thread.currentThread().getName()
143
+ " calling sun.net.ext.ExtendedSocketOptions#getInstance()");
144
try {
145
// let the other tasks know we are ready to trigger our work
146
latch.countDown();
147
// wait for the other task to let us know they are ready to trigger their work too
148
latch.await();
149
// let's call getInstance on sun.net.ext.ExtendedSocketOptions
150
final Object inst = callSunNetExtSocketOptionsGetInstance();
151
extendedSocketOptionsInstances.add(inst);
152
return inst;
153
} catch (Exception e) {
154
throw new RuntimeException(e);
155
}
156
}
157
}
158
}
159