aboutsummaryrefslogtreecommitdiff
path: root/test/javax/management/eventService/EventClientThreadTest.java
blob: 910bc9cc2d2bc04a690fe71fa4e79f508b2440bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test
 * @bug 6747411
 * @summary Check that EventClient instances don't leak threads.
 * @author Eamonn McManus
 */

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.Set;
import java.util.TreeSet;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanServerNotification;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.event.EventClient;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class EventClientThreadTest {
    private static final int MAX_TIME_SECONDS = 20;

    private static final BlockingQueue<Notification> queue =
            new ArrayBlockingQueue(100);

    private static final NotificationListener queueListener =
            new NotificationListener() {
        public void handleNotification(Notification notification,
                                       Object handback) {
            queue.add(notification);
        }
    };

    private static final NotificationFilter dummyFilter =
            new NotificationFilter() {
        public boolean isNotificationEnabled(Notification notification) {
            return true;
        }
    };

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        long deadline = start + MAX_TIME_SECONDS * 1000;

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(
                url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();

        ThreadMXBean threads = ManagementFactory.getThreadMXBean();

        System.out.println("Opening and closing some EventClients...");
        // If we create a connection, then create and destroy EventClients
        // over it, then close it, there should be no "JMX *" threads left.
        for (int i = 0; i < 5; i++)
            test(mbsc);

        cc.close();

        showTime("opening and closing initial EventClients", start);

        Set<String> jmxThreads = threadsMatching("JMX .*");
        while (!jmxThreads.isEmpty() && System.currentTimeMillis() < deadline) {
            Set<String> jmxThreadsNow = threadsMatching("JMX .*");
            Set<String> gone = new TreeSet<String>(jmxThreads);
            gone.removeAll(jmxThreadsNow);
            for (String s : gone)
                showTime("expiry of \"" + s + "\"", start);
            jmxThreads = jmxThreadsNow;
            Thread.sleep(10);
        }
        if (System.currentTimeMillis() >= deadline) {
            showThreads(threads);
            throw new Exception("Timed out waiting for JMX threads to expire");
        }

        showTime("waiting for JMX threads to expire", start);

        System.out.println("TEST PASSED");
    }

    static void showThreads(ThreadMXBean threads) throws Exception {
        long[] ids = threads.getAllThreadIds();
        for (long id : ids) {
            ThreadInfo ti = threads.getThreadInfo(id);
            String name = (ti == null) ? "(defunct)" : ti.getThreadName();
            System.out.printf("%4d %s\n", id, name);
        }
    }

    static void showTime(String what, long start) {
        long elapsed = System.currentTimeMillis() - start;
        System.out.printf("Time after %s: %.3f s\n", what, elapsed / 1000.0);
    }

    static Set<String> threadsMatching(String pattern) {
        Set<String> matching = new TreeSet<String>();
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
        long[] ids = threads.getAllThreadIds();
        for (long id : ids) {
            ThreadInfo ti = threads.getThreadInfo(id);
            String name = (ti == null) ? "(defunct)" : ti.getThreadName();
            if (name.matches(pattern))
                matching.add(name);
        }
        return matching;
    }

    static void test(MBeanServerConnection mbsc) throws Exception {
        final ObjectName delegateName = MBeanServerDelegate.DELEGATE_NAME;
        final ObjectName testName = new ObjectName("test:type=Test");
        EventClient ec = new EventClient(mbsc);
        ec.addNotificationListener(delegateName, queueListener, null, null);
        mbsc.createMBean(MBeanServerDelegate.class.getName(), testName);
        mbsc.unregisterMBean(testName);
        final String[] expectedTypes = {
            MBeanServerNotification.REGISTRATION_NOTIFICATION,
            MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
        };
        for (String s : expectedTypes) {
            Notification n = queue.poll(3, TimeUnit.SECONDS);
            if (n == null)
                throw new Exception("Timed out waiting for notif: " + s);
            if (!(n instanceof MBeanServerNotification))
                throw new Exception("Got notif of wrong class: " + n.getClass());
            if (!n.getType().equals(s)) {
                throw new Exception("Got notif of wrong type: " + n.getType() +
                        " (expecting " + s + ")");
            }
        }
        ec.removeNotificationListener(delegateName, queueListener);

        ec.addNotificationListener(delegateName, queueListener, dummyFilter, "foo");
        ec.removeNotificationListener(delegateName, queueListener, dummyFilter, "foo");

        ec.close();
    }
}