author | alanb |
Tue, 18 Dec 2018 10:26:15 +0000 | |
changeset 53038 | 9dd0a2fdec24 |
parent 52638 | f39854fffca0 |
permissions | -rw-r--r-- |
52638 | 1 |
/* |
2 |
* Copyright (c) 2018, 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 java.io.IOException; |
|
25 |
import java.lang.reflect.*; |
|
26 |
import java.net.URL; |
|
27 |
import java.net.URLConnection; |
|
28 |
import java.net.URLStreamHandler; |
|
29 |
import java.util.concurrent.CountDownLatch; |
|
30 |
||
31 |
/* |
|
32 |
* @test |
|
33 |
* @bug 8213942 |
|
34 |
* @summary URLStreamHandler initialization race |
|
53038
9dd0a2fdec24
8215449: Several tests failing when jtreg run with -vmoption:--illegal-access=deny
alanb
parents:
52638
diff
changeset
|
35 |
* @modules java.base/java.net:open |
52638 | 36 |
* @run main/othervm RacyHandler |
37 |
* @run main/othervm RacyHandler |
|
38 |
* @run main/othervm RacyHandler |
|
39 |
*/ |
|
40 |
||
41 |
/* |
|
42 |
* This test makes reasonable effort to reproduce the race. |
|
43 |
* Run repeatedly to ensure correctness. |
|
44 |
*/ |
|
45 |
public class RacyHandler { |
|
46 |
static volatile boolean factorySet = false; |
|
47 |
static int NUM_THREADS = 2; |
|
48 |
static CountDownLatch cdl = new CountDownLatch(NUM_THREADS + 1); |
|
49 |
||
50 |
public static void main(String[] args) { |
|
51 |
RacyHandler tester = new RacyHandler(); |
|
52 |
tester.runTest(); |
|
53 |
} |
|
54 |
||
55 |
public void runTest() { |
|
56 |
new Thread(() -> { |
|
57 |
try { |
|
58 |
cdl.await(); |
|
59 |
URL.setURLStreamHandlerFactory(proto -> new CustomHttpHandler()); |
|
60 |
factorySet = true; |
|
61 |
} catch (Exception ignore) { } |
|
62 |
}).start(); |
|
63 |
cdl.countDown(); |
|
64 |
||
65 |
for (int i = 0; i < NUM_THREADS; i++) { |
|
66 |
new Thread(() -> { |
|
67 |
try { |
|
68 |
cdl.await(); |
|
69 |
while (!factorySet) { |
|
70 |
// trigger URL class load |
|
71 |
getURLStreamHandler(); |
|
72 |
} |
|
73 |
} catch (Exception ignore) { } |
|
74 |
}).start(); |
|
75 |
cdl.countDown(); |
|
76 |
} |
|
77 |
||
78 |
// wait for the factory to be set |
|
79 |
while (!factorySet) { } |
|
80 |
// The sleep seems to help trigger the failure |
|
81 |
try { |
|
82 |
Thread.sleep(500); |
|
83 |
} catch (InterruptedException ie) { |
|
84 |
} |
|
85 |
||
86 |
URLStreamHandler httpHandler = getURLStreamHandler(); |
|
87 |
System.out.println("After setting factory URL handlers: http " + httpHandler); |
|
88 |
if (!(httpHandler instanceof CustomHttpHandler)) |
|
89 |
throw new RuntimeException("FAILED: Incorrect handler type"); |
|
90 |
} |
|
91 |
||
92 |
/* |
|
93 |
* This is just so we can see what we get for the URLStreamHandler back |
|
94 |
* from the factory to verify whether it really is using our Handler |
|
95 |
* or something else... |
|
96 |
*/ |
|
97 |
public URLStreamHandler getURLStreamHandler() { |
|
98 |
try { |
|
99 |
Method method = URL.class.getDeclaredMethod("getURLStreamHandler", |
|
100 |
String.class); |
|
101 |
method.setAccessible(true); |
|
102 |
return (URLStreamHandler) method.invoke(null, "http"); |
|
103 |
} catch (Exception e) { |
|
104 |
return null; |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
class CustomHttpHandler extends URLStreamHandler { |
|
109 |
@Override |
|
110 |
protected URLConnection openConnection(URL u) throws IOException { |
|
111 |
return null; |
|
112 |
} |
|
113 |
} |
|
53038
9dd0a2fdec24
8215449: Several tests failing when jtreg run with -vmoption:--illegal-access=deny
alanb
parents:
52638
diff
changeset
|
114 |
} |