EventClientExecutorTest.java 7.0 KB
Newer Older
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
/*
 * 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 5108776
 * @summary Test that the various Executor parameters in an EventClient do
 * what they are supposed to.
 * @author Eamonn McManus
 */

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.event.EventClient;
import javax.management.event.EventClientDelegate;
import javax.management.event.EventClientDelegateMBean;
import javax.management.event.FetchingEventRelay;
import javax.management.remote.MBeanServerForwarder;

public class EventClientExecutorTest {
    private static volatile String failure;
    private static final Set testedPrefixes = new HashSet();

    public static void main(String[] args) throws Exception {
        Executor fetchExecutor = Executors.newSingleThreadExecutor(
                new NamedThreadFactory("FETCH"));
        Executor listenerExecutor = Executors.newSingleThreadExecutor(
                new NamedThreadFactory("LISTENER"));
        ScheduledExecutorService leaseScheduler =
            Executors.newSingleThreadScheduledExecutor(
                new NamedThreadFactory("LEASE"));

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
68
        MBeanServerForwarder mbsf = EventClientDelegate.newForwarder(mbs, null);
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        mbs = mbsf;

        EventClientDelegateMBean ecd = EventClientDelegate.getProxy(mbs);
        ecd = (EventClientDelegateMBean) Proxy.newProxyInstance(
                EventClientDelegateMBean.class.getClassLoader(),
                new Class<?>[] {EventClientDelegateMBean.class},
                new DelegateCheckIH(ecd));

        ObjectName mbeanName = new ObjectName("d:type=Notifier");
        Notifier notifier = new Notifier();
        mbs.registerMBean(notifier, mbeanName);

        FetchingEventRelay eventRelay = new FetchingEventRelay(
                ecd, fetchExecutor);
        EventClient ec = new EventClient(
                ecd, eventRelay, listenerExecutor, leaseScheduler, 1000L);
        NotificationListener checkListener = new NotificationListener() {
            public void handleNotification(Notification notification,
                                           Object handback) {
                assertThreadName("listener dispatch", "LISTENER");
            }
        };
        ec.addNotificationListener(mbeanName, checkListener, null, null);

        mbs.invoke(mbeanName, "send", null, null);

        // Now wait until we have seen all three thread types.
        long deadline = System.currentTimeMillis() + 5000;
        synchronized (testedPrefixes) {
            while (testedPrefixes.size() < 3 && failure == null) {
                long remain = deadline - System.currentTimeMillis();
                if (remain <= 0) {
                    fail("Timed out waiting for all three thread types to show, " +
                            "saw only " + testedPrefixes);
                    break;
                }
                try {
                    testedPrefixes.wait(remain);
                } catch (InterruptedException e) {
                    fail("Unexpected InterruptedException");
                    break;
                }
            }
        }

        // We deliberately don't close the EventClient to check that it has
        // not created any non-daemon threads.

        if (failure != null)
            throw new Exception("TEST FAILED: " + failure);
        else
            System.out.println("TEST PASSED");
    }

    public static interface NotifierMBean {
        public void send();
    }

    public static class Notifier extends NotificationBroadcasterSupport
            implements NotifierMBean {
        public void send() {
            Notification n = new Notification("a.b.c", this, 0L);
            sendNotification(n);
        }
    }

    static void fail(String why) {
        System.out.println("FAIL: " + why);
        failure = why;
    }

    static void assertThreadName(String what, String prefix) {
        String name = Thread.currentThread().getName();
        if (!name.startsWith(prefix)) {
            fail("Wrong thread for " + what + ": " + name);
            return;
        }

        synchronized (testedPrefixes) {
            if (testedPrefixes.add(prefix))
                testedPrefixes.notify();
        }
    }

    private static class DelegateCheckIH implements InvocationHandler {
        private final EventClientDelegateMBean ecd;

        public DelegateCheckIH(EventClientDelegateMBean ecd) {
            this.ecd = ecd;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            String methodName = method.getName();
            if (methodName.equals("fetchNotifications"))
                assertThreadName("fetchNotifications", "FETCH");
            else if (methodName.equals("lease"))
                assertThreadName("lease renewal", "LEASE");
            try {
                return method.invoke(ecd, args);
            } catch (InvocationTargetException e) {
                throw e.getCause();
            }
        }
    }

    private static class NamedThreadFactory implements ThreadFactory {
        private final String namePrefix;
        private int count;

        NamedThreadFactory(String namePrefix) {
            this.namePrefix = namePrefix;
        }

        public synchronized Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName(namePrefix + " " + ++count);
            t.setDaemon(true);
            return t;
        }
    }
}