Path: blob/master/test/jdk/sun/net/ext/ExtendedSocketOptionsTest.java
41149 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*/2223import org.testng.Assert;24import org.testng.annotations.Test;2526import java.util.ArrayList;27import java.util.Collections;28import java.util.List;29import java.util.concurrent.Callable;30import java.util.concurrent.CountDownLatch;31import java.util.concurrent.ExecutorService;32import java.util.concurrent.Executors;33import java.util.concurrent.Future;3435/**36* @test37* @bug 826036638* @summary Verify that concurrent classloading of sun.net.ext.ExtendedSocketOptions and39* jdk.net.ExtendedSocketOptions doesn't lead to a deadlock40* @modules java.base/sun.net.ext:open41* jdk.net42* @run testng/othervm ExtendedSocketOptionsTest43* @run testng/othervm ExtendedSocketOptionsTest44* @run testng/othervm ExtendedSocketOptionsTest45* @run testng/othervm ExtendedSocketOptionsTest46* @run testng/othervm ExtendedSocketOptionsTest47*/48public class ExtendedSocketOptionsTest {4950/**51* Loads {@code jdk.net.ExtendedSocketOptions} and {@code sun.net.ext.ExtendedSocketOptions}52* and invokes {@code sun.net.ext.ExtendedSocketOptions#getInstance()} concurrently in a thread53* of their own and expects the classloading of both those classes54* to succeed. Additionally, after these tasks are done, calls the55* sun.net.ext.ExtendedSocketOptions#getInstance() and expects it to return a registered56* ExtendedSocketOptions instance.57*/58@Test59public void testConcurrentClassLoad() throws Exception {60final CountDownLatch taskTriggerLatch = new CountDownLatch(4);61final List<Callable<?>> tasks = new ArrayList<>();62tasks.add(new Task("jdk.net.ExtendedSocketOptions", taskTriggerLatch));63tasks.add(new Task("sun.net.ext.ExtendedSocketOptions", taskTriggerLatch));64// add a couple of tasks which call sun.net.ext.ExtendedSocketOptions#getInstance65tasks.add(new GetInstanceTask(taskTriggerLatch));66tasks.add(new GetInstanceTask(taskTriggerLatch));67final ExecutorService executor = Executors.newFixedThreadPool(tasks.size());68try {69final Future<?>[] results = new Future[tasks.size()];70// submit71int i = 0;72for (final Callable<?> task : tasks) {73results[i++] = executor.submit(task);74}75// wait for completion76for (i = 0; i < tasks.size(); i++) {77results[i].get();78}79} finally {80executor.shutdownNow();81}82// check that the sun.net.ext.ExtendedSocketOptions#getInstance() does indeed return83// the registered instance84final Object extSocketOptions = callSunNetExtSocketOptionsGetInstance();85Assert.assertNotNull(extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" +86" unexpectedly returned null");87// now verify that each call to getInstance(), either in the tasks or here, returned the exact88// same instance of ExtendedSocketOptions89Assert.assertEquals(2, GetInstanceTask.extendedSocketOptionsInstances.size());90for (final Object inst : GetInstanceTask.extendedSocketOptionsInstances) {91Assert.assertSame(inst, extSocketOptions, "sun.net.ext.ExtendedSocketOptions#getInstance()" +92" returned different instances");93}94}9596/**97* Reflectively calls sun.net.ext.ExtendedSocketOptions#getInstance() and returns98* the result99*/100private static Object callSunNetExtSocketOptionsGetInstance() throws Exception {101final Class<?> k = Class.forName("sun.net.ext.ExtendedSocketOptions");102return k.getDeclaredMethod("getInstance").invoke(null);103}104105private static class Task implements Callable<Class<?>> {106private final String className;107private final CountDownLatch latch;108109private Task(final String className, final CountDownLatch latch) {110this.className = className;111this.latch = latch;112}113114@Override115public Class<?> call() {116System.out.println(Thread.currentThread().getName() + " loading " + this.className);117try {118// let the other tasks know we are ready to trigger our work119latch.countDown();120// wait for the other task to let us know they are ready to trigger their work too121latch.await();122return Class.forName(this.className);123} catch (Exception e) {124throw new RuntimeException(e);125}126}127}128129private static class GetInstanceTask implements Callable<Object> {130// keeps track of the instances returned by calls to sun.nex.ext.ExtendedSocketOptions#getInstance()131// by the GetInstanceTask(s)132private static final List<Object> extendedSocketOptionsInstances = Collections.synchronizedList(new ArrayList<>());133private final CountDownLatch latch;134135private GetInstanceTask(final CountDownLatch latch) {136this.latch = latch;137}138139@Override140public Object call() {141System.out.println(Thread.currentThread().getName()142+ " calling sun.net.ext.ExtendedSocketOptions#getInstance()");143try {144// let the other tasks know we are ready to trigger our work145latch.countDown();146// wait for the other task to let us know they are ready to trigger their work too147latch.await();148// let's call getInstance on sun.net.ext.ExtendedSocketOptions149final Object inst = callSunNetExtSocketOptionsGetInstance();150extendedSocketOptionsInstances.add(inst);151return inst;152} catch (Exception e) {153throw new RuntimeException(e);154}155}156}157}158159