author | jbachorik |
Fri, 25 Jul 2014 15:07:49 +0200 | |
changeset 25751 | 561ceea4b5e9 |
parent 25180 | 36c18cc1bc60 |
child 30376 | 2ccf2cf7ea48 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
2 |
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4531526 |
|
26 |
* @summary Test that more than one debuggee cannot bind to same port |
|
27 |
* at the same time. |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
28 |
* @library /lib/testlibrary |
2 | 29 |
* |
24509
5d02a6d25f78
8043520: Serviceability tests using @library failing with java.lang.NoClassDefFoundError
ykantser
parents:
22947
diff
changeset
|
30 |
* @build jdk.testlibrary.* VMConnection ExclusiveBind HelloWorld |
24973
8c4bc3fa4c4e
6622468: TEST_BUG: Time to retire the @debuggeeVMOptions mechanism used in the com.sun.jdi infrastructure
sla
parents:
24509
diff
changeset
|
31 |
* @run driver ExclusiveBind |
2 | 32 |
*/ |
33 |
import java.net.ServerSocket; |
|
34 |
import com.sun.jdi.Bootstrap; |
|
35 |
import com.sun.jdi.VirtualMachine; |
|
36 |
import com.sun.jdi.connect.Connector; |
|
37 |
import com.sun.jdi.connect.AttachingConnector; |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
38 |
|
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
39 |
import java.util.ArrayList; |
2 | 40 |
import java.util.Map; |
41 |
import java.util.List; |
|
42 |
import java.util.Iterator; |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
43 |
import java.util.concurrent.TimeUnit; |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
44 |
|
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
45 |
import jdk.testlibrary.ProcessTools; |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
46 |
import jdk.testlibrary.Utils; |
2 | 47 |
|
48 |
public class ExclusiveBind { |
|
49 |
/* |
|
50 |
* Find a connector by name |
|
51 |
*/ |
|
52 |
private static Connector findConnector(String name) { |
|
53 |
List connectors = Bootstrap.virtualMachineManager().allConnectors(); |
|
54 |
Iterator iter = connectors.iterator(); |
|
55 |
while (iter.hasNext()) { |
|
56 |
Connector connector = (Connector)iter.next(); |
|
57 |
if (connector.name().equals(name)) { |
|
58 |
return connector; |
|
59 |
} |
|
60 |
} |
|
61 |
return null; |
|
62 |
} |
|
63 |
||
64 |
/* |
|
65 |
* Launch (in server mode) a debuggee with the given address and |
|
66 |
* suspend mode. |
|
67 |
*/ |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
68 |
private static ProcessBuilder prepareLauncher(String address, boolean suspend, String class_name) throws Exception { |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
69 |
List<String> args = new ArrayList<>(); |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
70 |
for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) { |
25180
36c18cc1bc60
8048687: [TESTBUG] com/sun/jdi/ExclusiveBind.java "Could not find or load main class"
sla
parents:
24973
diff
changeset
|
71 |
if (!dbgOption.trim().isEmpty()) { |
36c18cc1bc60
8048687: [TESTBUG] com/sun/jdi/ExclusiveBind.java "Could not find or load main class"
sla
parents:
24973
diff
changeset
|
72 |
args.add(dbgOption); |
36c18cc1bc60
8048687: [TESTBUG] com/sun/jdi/ExclusiveBind.java "Could not find or load main class"
sla
parents:
24973
diff
changeset
|
73 |
} |
2 | 74 |
} |
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
75 |
String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend="; |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
76 |
if (suspend) { |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
77 |
lib += "y"; |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
78 |
} else { |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
79 |
lib += "n"; |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
80 |
} |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
81 |
lib += ",address=" + address; |
2 | 82 |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
83 |
args.add(lib); |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
84 |
args.add(class_name); |
2 | 85 |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
86 |
return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()])); |
2 | 87 |
} |
88 |
||
89 |
/* |
|
90 |
* - pick a TCP port |
|
91 |
* - Launch a debuggee in server=y,suspend=y,address=${port} |
|
92 |
* - Launch a second debuggee in server=y,suspend=n with the same port |
|
93 |
* - Second debuggee should fail with an error (address already in use) |
|
94 |
* - For clean-up we attach to the first debuggee and resume it. |
|
95 |
*/ |
|
96 |
public static void main(String args[]) throws Exception { |
|
97 |
// find a free port |
|
98 |
ServerSocket ss = new ServerSocket(0); |
|
99 |
int port = ss.getLocalPort(); |
|
100 |
ss.close(); |
|
101 |
||
102 |
String address = String.valueOf(port); |
|
103 |
||
104 |
// launch the first debuggee |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
105 |
ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld"); |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
106 |
// start the debuggee and wait for the "ready" message |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
107 |
Process p = ProcessTools.startProcess( |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
108 |
"process1", |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
109 |
process1, |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
110 |
line -> line.equals("Listening for transport dt_socket at address: " + address), |
25751
561ceea4b5e9
8049194: com/sun/tools/attach/StartManagementAgent.java start failing after JDK-8048193
jbachorik
parents:
25180
diff
changeset
|
111 |
Utils.adjustTimeout(5000), |
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
112 |
TimeUnit.MILLISECONDS |
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
113 |
); |
2 | 114 |
|
115 |
// launch a second debuggee with the same address |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
116 |
ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld"); |
2 | 117 |
|
118 |
// get exit status from second debuggee |
|
22947
c7d9e7b465f9
6791551: ExclusiveBind.java has a race condition
jbachorik
parents:
20201
diff
changeset
|
119 |
int exitCode = ProcessTools.startProcess("process2", process2).waitFor(); |
2 | 120 |
|
121 |
// clean-up - attach to first debuggee and resume it |
|
122 |
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); |
|
123 |
Map conn_args = conn.defaultArguments(); |
|
124 |
Connector.IntegerArgument port_arg = |
|
125 |
(Connector.IntegerArgument)conn_args.get("port"); |
|
126 |
port_arg.setValue(port); |
|
127 |
VirtualMachine vm = conn.attach(conn_args); |
|
128 |
vm.resume(); |
|
129 |
||
130 |
// if the second debuggee ran to completion then we've got a problem |
|
131 |
if (exitCode == 0) { |
|
132 |
throw new RuntimeException("Test failed - second debuggee didn't fail to bind"); |
|
133 |
} else { |
|
134 |
System.out.println("Test passed - second debuggee correctly failed to bind"); |
|
135 |
} |
|
136 |
} |
|
137 |
} |