1 /*
   2  * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
   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  *
  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.
  22  */
  23 
  24 /**
  25  * A JVM with JDP on should send multicast JDP packets regularly.
  26  *
  27  * See main for more information on running this test manually.
  28  * See Launcher classes for automated runs.
  29  *
  30  */
  31 
  32 import java.net.SocketTimeoutException;
  33 import java.util.Map;
  34 import static jdk.test.lib.Asserts.assertNotEquals;
  35 
  36 public class JdpOnTestCase extends JdpTestCase {
  37 
  38     private int receivedJDPpackets = 0;
  39 
  40     public JdpOnTestCase(ClientConnection connection) {
  41         super(connection);
  42     }
  43 
  44     /**
  45      * Subclasses: JdpOnTestCase and JdpOffTestCase have different messages.
  46      */
  47     @Override
  48     protected String initialLogMessage() {
  49         return "Waiting for 3 packets with jdp.name=" + connection.instanceName;
  50     }
  51 
  52     /**
  53      * This method is executed after a correct Jdp packet (coming from this VM) has been received.
  54      *
  55      * @param payload A dictionary containing the data if the received Jdp packet.
  56      */
  57     protected void packetFromThisVMReceived(Map<String, String> payload) {
  58         receivedJDPpackets++;
  59         final String jdpName = payload.get("INSTANCE_NAME");
  60         log.fine("Received correct JDP packet #" + String.valueOf(receivedJDPpackets) +
  61                 ", jdp.name=" + jdpName);
  62         assertNotEquals(null, payload.get("PROCESS_ID"), "Expected payload.get(\"PROCESS_ID\") to be not null.");
  63     }
  64 
  65     /**
  66      * The socket should not timeout.
  67      * It is set to wait for 10 times the defined pause between Jdp packet. See JdpOnTestCase.TIME_OUT_FACTOR.
  68      */
  69     @Override
  70     protected void onSocketTimeOut(SocketTimeoutException e) throws Exception {
  71         String message = "Timed out waiting for JDP packet. Should arrive within " +
  72                 connection.pauseInSeconds + " seconds, but waited for " +
  73                 timeOut + " seconds.";
  74         log.severe(message);
  75         throw new Exception(message, e);
  76     }
  77 
  78     /**
  79      * After receiving three Jdp packets the test should end.
  80      */
  81     @Override
  82     protected boolean shouldContinue() {
  83         return receivedJDPpackets < 3;
  84     }
  85 
  86     /**
  87      * To run this test manually you might need the following VM options:
  88      * <p/>
  89      * -Dcom.sun.management.jmxremote.authenticate=false
  90      * -Dcom.sun.management.jmxremote.ssl=false
  91      * -Dcom.sun.management.jmxremote.port=4711    (or some other port number)
  92      * -Dcom.sun.management.jmxremote=true
  93      * -Dcom.sun.management.jmxremote.autodiscovery=true
  94      * -Dcom.sun.management.jdp.pause=1
  95      * -Dcom.sun.management.jdp.name=alex  (or some other string to identify this VM)
  96      * <p/>
  97      * Recommended for nice output:
  98      * -Djava.util.logging.SimpleFormatter.format="%1$tF %1$tT %4$-7s %5$s %n"
  99      *
 100      * @param args
 101      * @throws Exception
 102      */
 103     public static void main(String[] args) throws Exception {
 104         JdpTestCase client = new JdpOnTestCase(new ClientConnection());
 105         client.run();
 106     }
 107 
 108 }