Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jdp/JdpTestCase.java
38841 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* A JVM with JDP on should send multicast JDP packets regularly.25* Look at JdpOnTestCase.java and JdpOffTestCase.java26*/272829import sun.management.jdp.JdpJmxPacket;3031import java.io.IOException;32import java.io.UnsupportedEncodingException;33import java.net.DatagramPacket;34import java.net.MulticastSocket;35import java.net.SocketTimeoutException;36import java.util.Arrays;37import java.util.Map;38import java.util.logging.Level;39import java.util.logging.Logger;4041public abstract class JdpTestCase {42final Logger log = Logger.getLogger("sun.management.jdp");43final int MAGIC = 0xC0FFEE42; // Jdp magic number.44private static final int BUFFER_LENGTH = 64 * 1024; // max UDP size, except for IPv6 jumbograms.45private final int TIME_OUT_FACTOR = 10; // Socket times out after 10 times the jdp pause.46protected int timeOut;47private long startTime;48protected ClientConnection connection;4950public JdpTestCase(ClientConnection connection) {51this.connection = connection;52JdpTestUtil.enableConsoleLogging(log, Level.ALL);53}5455public void run() throws Exception {56log.fine("Test started.");57log.fine("Listening for multicast packets at " + connection.address.getHostAddress()58+ ":" + String.valueOf(connection.port));59log.fine(initialLogMessage());60log.fine("Pause in between packets is: " + connection.pauseInSeconds + " seconds.");6162startTime = System.currentTimeMillis();63timeOut = connection.pauseInSeconds * TIME_OUT_FACTOR;64log.fine("Timeout set to " + String.valueOf(timeOut) + " seconds.");6566MulticastSocket socket = connection.connectWithTimeout(timeOut * 1000);6768byte[] buffer = new byte[BUFFER_LENGTH];69DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);7071do {72try {73socket.receive(datagram);74onReceived(extractUDPpayload(datagram));75} catch (SocketTimeoutException e) {76onSocketTimeOut(e);77}7879if (hasTestLivedLongEnough()) {80shutdown();81}8283} while (shouldContinue());84log.fine("Test ended successfully.");85}8687/**88* Subclasses: JdpOnTestCase and JdpOffTestCase have different messages.89*/90protected abstract String initialLogMessage();919293/**94* Executed when the socket receives a UDP packet.95*/96private void onReceived(byte[] packet) throws Exception {97if (isJDP(packet)) {98Map<String, String> payload = checkStructure(packet);99jdpPacketReceived(payload);100} else {101log.fine("Non JDP packet received, ignoring it.");102}103}104105/**106* Determine whether the test should end.107*108* @return109*/110abstract protected boolean shouldContinue();111112/**113* This method is executed when the socket has not received any packet for timeOut seconds.114*/115abstract protected void onSocketTimeOut(SocketTimeoutException e) throws Exception;116117/**118* This method is executed after a correct Jdp packet has been received.119*120* @param payload A dictionary containing the data if the received Jdp packet.121*/122private void jdpPacketReceived(Map<String, String> payload) throws Exception {123final String instanceName = payload.get("INSTANCE_NAME");124if (instanceName.equals(connection.instanceName)) {125packetFromThisVMReceived(payload);126} else {127packetFromOtherVMReceived(payload);128}129}130131/**132* This method is executed after a correct Jdp packet, coming from this VM has been received.133*134* @param payload A dictionary containing the data if the received Jdp packet.135*/136protected abstract void packetFromThisVMReceived(Map<String, String> payload) throws Exception;137138139/**140* This method is executed after a correct Jdp packet, coming from another VM has been received.141*142* @param payload A dictionary containing the data if the received Jdp packet.143*/144protected void packetFromOtherVMReceived(Map<String, String> payload) {145final String jdpName = payload.get("INSTANCE_NAME");146log.fine("Ignoring JDP packet sent by other VM, jdp.name=" + jdpName);147}148149150/**151* The test should stop if it has been 12 times the jdp.pause.152* jdp.pause is how many seconds in between packets.153* <p/>154* This timeout (12 times)is slightly longer than the socket timeout (10 times) on purpose.155* In the off test case, the socket should time out first.156*157* @return158*/159protected boolean hasTestLivedLongEnough() {160long now = System.currentTimeMillis();161boolean haslivedLongEnough = (now - startTime) > (timeOut * 1.2 * 1000);162return haslivedLongEnough;163}164165/**166* This exit condition arises when we receive UDP packets but they are not valid Jdp.167*/168protected void shutdown() throws Exception {169log.severe("Shutting down the test.");170throw new Exception("Not enough JDP packets received before timeout!");171}172173/**174* Assert that this Jdp packet contains the required two keys.175* <p/>176* We expect zero packet corruption and thus fail on the first corrupted packet.177* This might need revision.178*/179protected Map<String, String> checkStructure(byte[] packet) throws UnsupportedEncodingException {180Map<String, String> payload = JdpTestUtil.readPayload(packet);181assertTrue(payload.size() >= 2, "JDP should have minimun 2 entries.");182assertTrue(payload.get(JdpJmxPacket.UUID_KEY).length() > 0);183assertTrue(payload.get(JdpJmxPacket.JMX_SERVICE_URL_KEY).length() > 0);184return payload;185}186187188/**189* Check if packet has correct JDP magic number.190*191* @param packet192* @return193* @throws IOException194*/195private boolean isJDP(byte[] packet) throws IOException {196int magic = JdpTestUtil.decode4ByteInt(packet, 0);197return (magic == MAGIC);198}199200private byte[] extractUDPpayload(DatagramPacket datagram) {201byte[] data = Arrays.copyOf(datagram.getData(), datagram.getLength());202return data;203}204205/**206* Hack until I find a way to use TestNG's assertions.207*/208private void assertTrue(boolean assertion, String message) {209if (assertion == false) {210log.severe(message);211assert (false);212}213}214215private void assertTrue(boolean assertion) {216assertTrue(assertion, "");217}218219}220221222