1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.rjmx.services.jfr.test; 34 35 import static org.openjdk.jmc.common.unit.UnitLookup.MILLISECOND; 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertNotNull; 38 39 import java.io.InputStream; 40 import java.util.Random; 41 42 import org.junit.Test; 43 44 import org.openjdk.jmc.common.unit.IConstrainedMap; 45 import org.openjdk.jmc.common.unit.IQuantity; 46 import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID; 47 import org.openjdk.jmc.flightrecorder.configuration.recording.RecordingOptionsBuilder; 48 import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService; 49 import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor; 50 51 public class JfrControlTest extends JfrTestCase { 52 @Test 53 public void testGetAvailableRecordings() throws Exception { 54 assumeHotSpot7u12OrLater(getConnectionHandle()); 55 56 IFlightRecorderService service = getFlightRecorderService(); 57 assertNotNull(service); 58 assertNotNull(service.getAvailableRecordings()); 59 } 60 61 @Test 62 public void testGetContinuousRecording() throws Exception { 63 assumeHotSpot7u12OrLater(getConnectionHandle()); 64 IRecordingDescriptor recording = startContinuousRecording(); 65 assertNotNull(getContinuousRecording()); 66 stopRecording(recording); 67 68 } 69 70 /** 71 * Also tests stop, close and getUpdatedRecordingDescriptor... 72 * 73 * @throws Exception 74 */ 75 @Test 76 public void testStartContinuousRecording() throws Exception { 77 assumeHotSpot7u12OrLater(getConnectionHandle()); 78 79 IRecordingDescriptor recording = startContinuousRecording(); 80 stopRecording(recording); 81 } 82 83 /** 84 * Also tests stop, close and getUpdatedRecordingDescriptor... 85 * 86 * @throws Exception 87 */ 88 @Test 89 public void testStartTimedRecording() throws Exception { 90 assumeHotSpot7u12OrLater(getConnectionHandle()); 91 92 IQuantity duration = MILLISECOND.quantity(5000); 93 Random rnd = new Random(); 94 String name = "test_recording_" + rnd.nextInt() % 4711; //$NON-NLS-1$ 95 IFlightRecorderService service = getFlightRecorderService(); 96 IConstrainedMap<String> recordingOptions = new RecordingOptionsBuilder(service).name(name).duration(duration) 97 .build(); 98 IRecordingDescriptor recording = service.start(recordingOptions, null); 99 IConstrainedMap<String> options = service.getRecordingOptions(recording); 100 Object durationOption = options.get(RecordingOptionsBuilder.KEY_DURATION); 101 assertNotNull(durationOption); 102 assertSame(duration, (IQuantity) durationOption); 103 assertEquals(IRecordingDescriptor.RecordingState.RUNNING, recording.getState()); 104 System.out.println("Started " + recording.getName()); //$NON-NLS-1$ 105 assertEquals(name, recording.getName()); 106 107 service.stop(recording); 108 for (int stopCount = 0; stopCount < 15; stopCount += 1) { 109 recording = service.getUpdatedRecordingDescription(recording); 110 if (!recording.getState().equals(IRecordingDescriptor.RecordingState.STOPPING)) { 111 break; 112 } 113 Thread.sleep(1000); 114 } 115 assertEquals(IRecordingDescriptor.RecordingState.STOPPED, recording.getState()); 116 System.out.println("Stopped " + recording.getName()); //$NON-NLS-1$ 117 service.close(recording); 118 recording = service.getUpdatedRecordingDescription(recording); 119 assertNull(recording); 120 } 121 122 @Test 123 public void testGetEventTypeSettings() throws Exception { 124 assumeHotSpot7u12OrLater(getConnectionHandle()); 125 126 IFlightRecorderService service = getFlightRecorderService(); 127 IConstrainedMap<EventOptionID> settings = service.getDefaultEventOptions(); 128 assertNotNull(settings); 129 } 130 131 @Test 132 public void testDumpContinuous() throws Exception { 133 assumeHotSpot7u12OrLater(getConnectionHandle()); 134 135 byte[] bytes = new byte[4096]; 136 int read = 0; 137 IFlightRecorderService service = getFlightRecorderService(); 138 IRecordingDescriptor recording = startContinuousRecording(); 139 IRecordingDescriptor descriptor = getContinuousRecording(); 140 InputStream stream = service.openStream(descriptor, false); 141 int lastRead = -1; 142 while ((lastRead = stream.read(bytes)) != -1) { 143 read += lastRead; 144 } 145 assertMin("Should have read something!", 1, read); //$NON-NLS-1$ 146 stream.close(); 147 stopRecording(recording); 148 } 149 }