MessageStatisticsInOnOpenTest.java
/*
* Copyright (c) 2014, 2017 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.tyrus.ext.monitoring.jmx;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.core.monitoring.ApplicationEventListener;
import org.glassfish.tyrus.server.Server;
import org.glassfish.tyrus.test.tools.TestContainer;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests that messages sent from server in onOpen get counted in the message statistics.
*
* @author Petr Janouch
*/
public class MessageStatisticsInOnOpenTest extends TestContainer {
@ServerEndpoint("/jmxEndpoint")
public static class AnnotatedServerEndpoint {
@OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText("Hello");
}
}
@ClientEndpoint
public static class AnnotatedClientEndpoint {
@OnMessage
public void onMessage(String message) {
}
}
@Test
public void test() {
Server server = null;
try {
CountDownLatch messageSentLatch = new CountDownLatch(1);
setContextPath("/jmxTestApp");
ApplicationEventListener applicationEventListener =
new TestApplicationEventListener(new SessionlessApplicationMonitor(), null, null, messageSentLatch,
null, null);
getServerProperties().put(ApplicationEventListener.APPLICATION_EVENT_LISTENER, applicationEventListener);
server = startServer(AnnotatedServerEndpoint.class);
ClientManager client = createClient();
client.connectToServer(AnnotatedClientEndpoint.class, getURI(AnnotatedServerEndpoint.class));
assertTrue(messageSentLatch.await(1, TimeUnit.SECONDS));
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
String fullApplicationMxBeanName = "org.glassfish.tyrus:type=/jmxTestApp";
ApplicationMXBean applicationBean =
JMX.newMXBeanProxy(mBeanServer, new ObjectName(fullApplicationMxBeanName), ApplicationMXBean.class);
assertEquals(1, applicationBean.getSentMessagesCount());
} catch (Exception e) {
fail();
} finally {
stopServer(server);
}
}
}