|
1 /* |
|
2 * Copyright (c) 2019, 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 8231387 |
|
27 * @library ../testlibrary |
|
28 * @summary make sure getService() avoids a race |
|
29 * @author Tianmin Shi |
|
30 */ |
|
31 |
|
32 import java.security.Provider; |
|
33 |
|
34 public class GetServiceRace { |
|
35 |
|
36 private static final Provider testProvider; |
|
37 static { |
|
38 testProvider = new Provider("MyProvider", 1.0, "test") { |
|
39 }; |
|
40 testProvider.put("CertificateFactory.Fixed", "MyCertificateFactory"); |
|
41 } |
|
42 |
|
43 private static final int NUMBER_OF_RETRIEVERS = 3; |
|
44 private static final int TEST_TIME_MS = 1000; |
|
45 |
|
46 public static boolean testFailed = false; |
|
47 |
|
48 public static void main(String[] args) throws Exception { |
|
49 Updater updater = new Updater(); |
|
50 updater.start(); |
|
51 Retriever [] retrievers = new Retriever[NUMBER_OF_RETRIEVERS]; |
|
52 for (int i=0; i<retrievers.length; i++) { |
|
53 retrievers[i] = new Retriever(); |
|
54 retrievers[i].start(); |
|
55 } |
|
56 Thread.sleep(TEST_TIME_MS); |
|
57 System.out.println("Interrupt"); |
|
58 updater.interrupt(); |
|
59 updater.join(); |
|
60 for (int i=0; i<retrievers.length; i++) { |
|
61 retrievers[i].interrupt(); |
|
62 retrievers[i].join(); |
|
63 } |
|
64 System.out.println("Done"); |
|
65 if (testFailed) { |
|
66 throw new Exception("Test Failed"); |
|
67 } |
|
68 System.out.println("Test Passed"); |
|
69 } |
|
70 |
|
71 private static class Updater extends Thread { |
|
72 @Override |
|
73 public void run() { |
|
74 while (!isInterrupted()) { |
|
75 testProvider.put("CertificateFactory.Added", "MyCertificateFactory"); |
|
76 } |
|
77 System.out.println("Updater stopped"); |
|
78 } |
|
79 } |
|
80 |
|
81 private static class Retriever extends Thread { |
|
82 @Override |
|
83 public void run() { |
|
84 while (!isInterrupted()) { |
|
85 Provider.Service service = testProvider.getService("CertificateFactory", "Fixed"); |
|
86 if (service == null) { |
|
87 if (!testFailed) { |
|
88 System.err.println("CertificateFactory.Fixed is NULL"); |
|
89 testFailed = true; |
|
90 } |
|
91 } else { |
|
92 //System.out.println("CertificateFactory.Fixed is good"); |
|
93 } |
|
94 } |
|
95 System.out.println("Retriever stopped"); |
|
96 } |
|
97 } |
|
98 } |