blob: 6f617bbc095034186d28359e34308511dba92573 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6192124
27 * @summary Tests that you can turn off the server connection timeout thread
28 * @author Eamonn McManus
29 * @run clean NoServerTimeoutTest
30 * @run build NoServerTimeoutTest
31 * @run main NoServerTimeoutTest
32 */
33
34import java.lang.management.*;
35import java.net.MalformedURLException;
36import java.util.*;
37import javax.management.*;
38import javax.management.remote.*;
39
40public class NoServerTimeoutTest {
41 public static void main(String[] args) throws Exception {
42 boolean ok = true;
43
44 for (String proto : new String[] {"rmi", "iiop", "jmxmp"}) {
45 JMXServiceURL url = new JMXServiceURL(proto, null, 0);
46 try {
47 MBeanServer mbs = MBeanServerFactory.newMBeanServer();
48 // Create server just to see if the protocol is supported
49 JMXConnectorServerFactory.newJMXConnectorServer(url,
50 null,
51 mbs);
52 } catch (MalformedURLException e) {
53 System.out.println();
54 System.out.println("Ignoring protocol: " + proto);
55 continue;
56 }
57 try {
58 ok &= test(url);
59 } catch (Exception e) {
60 System.out.println("TEST FAILED WITH EXCEPTION:");
61 e.printStackTrace();
62 ok = false;
63 }
64 }
65
66 System.out.println();
67 if (ok)
68 System.out.println("Test passed");
69 else
70 throw new Exception("Test failed");
71 }
72
73 private static enum Test {
74 NO_ENV("No Map for connector server"),
75 EMPTY_ENV("Empty Map for connector server"),
76 PLAIN_TIMEOUT("Map with two-minute timeout as int"),
77 PLAIN_STRING_TIMEOUT("Map with two-minute timeout as string"),
78 INFINITE_TIMEOUT("Map with Long.MAX_VALUE timeout as long"),
79 INFINITE_STRING_TIMEOUT("Map with Long.MAX_VALUE timeout as string");
80
81 Test(String description) {
82 this.description = description;
83 }
84
85 public String toString() {
86 return description;
87 }
88
89 private final String description;
90 }
91 // define which tests should see a timeout thread
92 private static final Set<Test> expectThread =
93 EnumSet.copyOf(Arrays.asList(Test.NO_ENV,
94 Test.EMPTY_ENV,
95 Test.PLAIN_TIMEOUT,
96 Test.PLAIN_STRING_TIMEOUT));
97
98 private static boolean test(JMXServiceURL url) throws Exception {
99 System.out.println();
100 System.out.println("Test: " + url);
101
102 boolean ok = true;
103
104 for (Test test : Test.values())
105 ok &= test(url, test);
106
107 return ok;
108 }
109
110 private static final String TIMEOUT =
111 "jmx.remote.x.server.connection.timeout";
112
113 private static boolean test(JMXServiceURL url, Test test)
114 throws Exception {
115
116 System.out.println("* " + test);
117
118 MBeanServer mbs = MBeanServerFactory.newMBeanServer();
119 Map<String, Object> env = new HashMap<String, Object>();
120 switch (test) {
121 case NO_ENV: env = null; break;
122 case EMPTY_ENV: break;
123 case PLAIN_TIMEOUT: env.put(TIMEOUT, 120 * 1000L); break;
124 case PLAIN_STRING_TIMEOUT: env.put(TIMEOUT, (120 * 1000L) + ""); break;
125 case INFINITE_TIMEOUT: env.put(TIMEOUT, Long.MAX_VALUE); break;
126 case INFINITE_STRING_TIMEOUT: env.put(TIMEOUT, "" + Long.MAX_VALUE);
127 break;
128 default: throw new AssertionError();
129 }
130
131 // In case there's a timeout thread left over from a previous run
132 for (int i = 0; i < 10 && countTimeoutThreads() > 0; i++)
133 Thread.sleep(500);
134 if (countTimeoutThreads() > 0) {
135 System.out.println("TIMEOUT THREAD(S) WOULD NOT GO AWAY");
136 return false;
137 }
138
139 JMXConnectorServer cs =
140 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
141 cs.start();
142 JMXServiceURL addr = cs.getAddress();
143 JMXConnector cc = JMXConnectorFactory.connect(addr);
144 MBeanServerConnection mbsc = cc.getMBeanServerConnection();
145 mbsc.getDefaultDomain();
146 int expectTimeoutThreads = expectThread.contains(test) ? 1 : 0;
147 int timeoutThreads = countTimeoutThreads();
148 boolean ok = (expectTimeoutThreads == timeoutThreads);
149 if (!ok) {
150 System.out.println("TEST FAILS: Expected timeout threads: " +
151 expectTimeoutThreads +
152 "; actual timeout threads: " + timeoutThreads);
153 ok = false;
154 }
155 cc.close();
156 cs.stop();
157 return ok;
158 }
159
160 private static int countTimeoutThreads() {
161 ThreadMXBean mb = ManagementFactory.getThreadMXBean();
162 int count = 0;
163 long[] ids = mb.getAllThreadIds();
164 for (ThreadInfo ti : mb.getThreadInfo(ids)) {
165 if (ti != null &&
166 ti.getThreadName().startsWith("JMX server connection timeout"))
167 count++;
168 }
169 return count;
170 }
171}