Skip to content
Permalink
Browse files
8273651: JFR: onMetadata(), setStartTime(), setEndTime() lacks test c…
…overage

Reviewed-by: mgronlun, mseledtsov
  • Loading branch information
egahlin committed Sep 20, 2021
1 parent 1bd11a7 commit 7ce60c6ff6950bbe07fbc363303e7e6ce5e1b696
Showing with 108 additions and 2 deletions.
  1. +32 −2 test/jdk/jdk/jfr/jmx/streaming/TestDelegated.java
  2. +76 −0 test/jdk/jdk/jfr/jmx/streaming/TestMetadataEvent.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Oracle and/or its affiliates. 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
@@ -25,6 +25,7 @@

import java.lang.management.ManagementFactory;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CountDownLatch;

import javax.management.MBeanServerConnection;
@@ -63,6 +64,8 @@ public static void main(String[] args) throws Exception {
testSetMaxAge();
testAwaitTermination();
testAwaitTerminationWithDuration();
testSetStartTime();
testSetEndTime();
}

private static void testSetMaxAge() throws Exception {
@@ -166,7 +169,6 @@ private static void testOnEvent() throws Exception {
e.commit();
latch.await();
}

}

private static void testOrdered() throws Exception {
@@ -198,4 +200,32 @@ private static void testRemove() throws Exception {
}
}
}

private static void testSetEndTime() throws Exception {
Instant t = Instant.now().plus(Duration.ofDays(1));
try (RemoteRecordingStream stream = new RemoteRecordingStream(CONNECTION)) {
stream.setEndTime(t);
stream.onEvent(e -> {
stream.close();
});
stream.startAsync();
TestDelegatedEvent e = new TestDelegatedEvent();
e.commit();
stream.awaitTermination();
}
}

private static void testSetStartTime() throws Exception {
Instant t = Instant.now().minus(Duration.ofDays(1));
try (RemoteRecordingStream stream = new RemoteRecordingStream(CONNECTION)) {
stream.setStartTime(t);
stream.onEvent(e -> {
stream.close();
});
stream.startAsync();
TestDelegatedEvent e = new TestDelegatedEvent();
e.commit();
stream.awaitTermination();
}
}
}
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jfr.jmx.streaming;

import java.lang.management.ManagementFactory;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Function;

import jdk.jfr.Configuration;
import jdk.jfr.EventType;
import jdk.jfr.FlightRecorder;
import jdk.jfr.consumer.MetadataEvent;
import jdk.management.jfr.RemoteRecordingStream;

/**
* @test
* @key jfr
* @summary Sanity tests RemoteRecordingStream::onMetadata
* @requires vm.hasJFR
* @library /test/lib /test/jdk
* @run main/othervm jdk.jfr.jmx.streaming.TestMetadataEvent
*/
public class TestMetadataEvent {

public static void main(String... args) throws Exception {
var conn = ManagementFactory.getPlatformMBeanServer();
var q = new ArrayBlockingQueue<MetadataEvent>(1);
try (RemoteRecordingStream e = new RemoteRecordingStream(conn)) {
e.onMetadata(q::offer);
e.startAsync();
MetadataEvent event = q.take();
assertEventTypes(FlightRecorder.getFlightRecorder().getEventTypes(), event.getEventTypes());
assertConfigurations(Configuration.getConfigurations(), event.getConfigurations());
}
}

private static void assertEventTypes(List<EventType> expected, List<EventType> eventTypes) throws Exception {
assertListProperty(expected, eventTypes, EventType::getName);
}

private static void assertConfigurations(List<Configuration> expected, List<Configuration> configurations) throws Exception {
assertListProperty(expected, configurations, Configuration::getName);
}

private static <T, R> void assertListProperty(List<T> expected, List<T> result, Function<T, R> mapper) throws Exception {
var a1 = new HashSet<R>();
a1.addAll(expected.stream().map(mapper).toList());
var a2 = new HashSet<R>();
a2.addAll(result.stream().map(mapper).toList());
if (!a1.equals(a2)) {
throw new Exception("Result not as expected!\nexpected = " + a1 + "\nresult= " + a2);
}
}
}

1 comment on commit 7ce60c6

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 7ce60c6 Sep 20, 2021

Please sign in to comment.