Path: blob/master/test/jdk/java/security/Provider/GetServiceRace.java
41149 views
/*1* Copyright (c) 2019, 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 823138726* @library ../testlibrary27* @summary make sure getService() avoids a race28* @author Tianmin Shi29*/3031import java.security.Provider;3233public class GetServiceRace {3435private static final Provider testProvider;36static {37testProvider = new Provider("MyProvider", 1.0, "test") {38};39testProvider.put("CertificateFactory.Fixed", "MyCertificateFactory");40}4142private static final int NUMBER_OF_RETRIEVERS = 3;43private static final int TEST_TIME_MS = 1000;4445public static boolean testFailed = false;4647public static void main(String[] args) throws Exception {48Updater updater = new Updater();49updater.start();50Retriever [] retrievers = new Retriever[NUMBER_OF_RETRIEVERS];51for (int i=0; i<retrievers.length; i++) {52retrievers[i] = new Retriever();53retrievers[i].start();54}55Thread.sleep(TEST_TIME_MS);56System.out.println("Interrupt");57updater.interrupt();58updater.join();59for (int i=0; i<retrievers.length; i++) {60retrievers[i].interrupt();61retrievers[i].join();62}63System.out.println("Done");64if (testFailed) {65throw new Exception("Test Failed");66}67System.out.println("Test Passed");68}6970private static class Updater extends Thread {71@Override72public void run() {73while (!isInterrupted()) {74testProvider.put("CertificateFactory.Added", "MyCertificateFactory");75}76System.out.println("Updater stopped");77}78}7980private static class Retriever extends Thread {81@Override82public void run() {83while (!isInterrupted()) {84Provider.Service service = testProvider.getService("CertificateFactory", "Fixed");85if (service == null) {86if (!testFailed) {87System.err.println("CertificateFactory.Fixed is NULL");88testFailed = true;89}90} else {91//System.out.println("CertificateFactory.Fixed is good");92}93}94System.out.println("Retriever stopped");95}96}97}9899100