author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 21650 | 390b5df3fcaf |
child 30376 | 2ccf2cf7ea48 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2013 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 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6222826 |
|
27 |
* @summary Test that each thread in the thread pool runs |
|
28 |
* in the context of the monitor.start() caller. |
|
29 |
* @author Luis-Miguel Alventosa |
|
30 |
* @run clean ThreadPoolAccTest |
|
31 |
* @run build ThreadPoolAccTest |
|
32 |
* @run main ThreadPoolAccTest |
|
33 |
*/ |
|
34 |
||
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
37 |
import java.util.Date; |
2 | 38 |
import java.util.Set; |
39 |
import javax.management.MBeanServer; |
|
40 |
import javax.management.MBeanServerFactory; |
|
41 |
import javax.management.ObjectName; |
|
42 |
import javax.management.monitor.CounterMonitor; |
|
43 |
import javax.management.monitor.GaugeMonitor; |
|
44 |
import javax.management.monitor.Monitor; |
|
45 |
import javax.management.monitor.StringMonitor; |
|
46 |
import javax.management.remote.JMXPrincipal; |
|
47 |
import javax.security.auth.Subject; |
|
48 |
||
49 |
public class ThreadPoolAccTest { |
|
50 |
||
51 |
// MBean class |
|
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
52 |
public static class ObservedObject implements ObservedObjectMBean { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
53 |
public volatile String principal; |
2 | 54 |
public Integer getInteger() { |
55 |
setPrincipal(); |
|
56 |
return 0; |
|
57 |
} |
|
58 |
public Double getDouble() { |
|
59 |
setPrincipal(); |
|
60 |
return 0.0; |
|
61 |
} |
|
62 |
public String getString() { |
|
63 |
setPrincipal(); |
|
64 |
return ""; |
|
65 |
} |
|
66 |
private void setPrincipal() { |
|
67 |
Subject subject = Subject.getSubject(AccessController.getContext()); |
|
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
68 |
Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
69 |
principal = principals.iterator().next().getName(); |
2 | 70 |
} |
71 |
} |
|
72 |
||
73 |
// MBean interface |
|
74 |
public interface ObservedObjectMBean { |
|
75 |
public Integer getInteger(); |
|
76 |
public Double getDouble(); |
|
77 |
public String getString(); |
|
78 |
} |
|
79 |
||
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
80 |
public static void main (String args[]) throws Exception { |
2 | 81 |
|
82 |
ObjectName[] mbeanNames = new ObjectName[6]; |
|
83 |
ObservedObject[] monitored = new ObservedObject[6]; |
|
84 |
ObjectName[] monitorNames = new ObjectName[6]; |
|
85 |
Monitor[] monitor = new Monitor[6]; |
|
86 |
String[] principals = { "role1", "role2" }; |
|
87 |
String[] attributes = { "Integer", "Double", "String" }; |
|
88 |
||
89 |
try { |
|
90 |
echo(">>> CREATE MBeanServer"); |
|
91 |
MBeanServer server = MBeanServerFactory.newMBeanServer(); |
|
92 |
||
93 |
for (int i = 0; i < 6; i++) { |
|
94 |
mbeanNames[i] = |
|
95 |
new ObjectName(":type=ObservedObject,instance=" + i); |
|
96 |
monitored[i] = new ObservedObject(); |
|
97 |
echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString()); |
|
98 |
server.registerMBean(monitored[i], mbeanNames[i]); |
|
99 |
||
100 |
switch (i) { |
|
101 |
case 0: |
|
102 |
case 3: |
|
103 |
monitorNames[i] = |
|
104 |
new ObjectName(":type=CounterMonitor,instance=" + i); |
|
105 |
monitor[i] = new CounterMonitor(); |
|
106 |
break; |
|
107 |
case 1: |
|
108 |
case 4: |
|
109 |
monitorNames[i] = |
|
110 |
new ObjectName(":type=GaugeMonitor,instance=" + i); |
|
111 |
monitor[i] = new GaugeMonitor(); |
|
112 |
break; |
|
113 |
case 2: |
|
114 |
case 5: |
|
115 |
monitorNames[i] = |
|
116 |
new ObjectName(":type=StringMonitor,instance=" + i); |
|
117 |
monitor[i] = new StringMonitor(); |
|
118 |
break; |
|
119 |
} |
|
120 |
||
121 |
echo(">>> CREATE Monitor = " + monitorNames[i].toString()); |
|
122 |
server.registerMBean(monitor[i], monitorNames[i]); |
|
123 |
monitor[i].addObservedObject(mbeanNames[i]); |
|
124 |
monitor[i].setObservedAttribute(attributes[i % 3]); |
|
125 |
monitor[i].setGranularityPeriod(500); |
|
126 |
final Monitor m = monitor[i]; |
|
127 |
Subject subject = new Subject(); |
|
128 |
echo(">>> RUN Principal = " + principals[i / 3]); |
|
129 |
subject.getPrincipals().add(new JMXPrincipal(principals[i / 3])); |
|
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
130 |
PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
131 |
public Void run() { |
2 | 132 |
m.start(); |
133 |
return null; |
|
134 |
} |
|
135 |
}; |
|
136 |
Subject.doAs(subject, action); |
|
137 |
} |
|
138 |
||
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
139 |
while(!testPrincipals(monitored, monitorNames, monitor, principals)); |
2 | 140 |
|
141 |
} finally { |
|
142 |
for (int i = 0; i < 6; i++) |
|
143 |
if (monitor[i] != null) |
|
144 |
monitor[i].stop(); |
|
145 |
} |
|
146 |
} |
|
147 |
||
21650
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
148 |
private static boolean testPrincipals(ObservedObject[] monitored, ObjectName[] monitorNames, |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
149 |
Monitor[] monitor, String[] principals) throws Exception { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
150 |
for (int i = 0; i < 6; i++) { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
151 |
String principal = monitored[i].principal; |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
152 |
String expected = principals[i / 3]; |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
153 |
if (principal == null) { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
154 |
echo("Task not submitted " + new Date() + ". RETRY"); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
155 |
return false; |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
156 |
} |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
157 |
echo(">>> Monitor = " + monitorNames[i]); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
158 |
echo(">>> ObservedObject = " + monitor[i].getObservedObject()); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
159 |
echo(">>> ObservedAttribute = " + monitor[i].getObservedAttribute()); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
160 |
echo(">>> Principal = " + principal); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
161 |
|
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
162 |
if (expected.equals(principal)) { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
163 |
echo("\tOK: Got Expected principal"); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
164 |
} else { |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
165 |
throw new Exception("Unexpected principal. Got: " + principal + " Expected: " + expected); |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
166 |
} |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
167 |
} |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
168 |
return true; |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
169 |
} |
390b5df3fcaf
8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents:
5506
diff
changeset
|
170 |
|
2 | 171 |
private static void echo(String message) { |
172 |
System.out.println(message); |
|
173 |
} |
|
174 |
} |