Skip to content

Commit

Permalink
Add DEFAULT to StackTrace Policy for scenarios when events have stack…
Browse files Browse the repository at this point in the history
… traces disabled by default

Signed-off-by: Tushar Badgu <tushar.badgu@gmail.com>
  • Loading branch information
tbadgu authored and gunnarmorling committed Feb 4, 2021
1 parent 0e3ace5 commit f665b10
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dev/morling/jfrunit/EnableEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
*/
String value() default "";

StacktracePolicy stackTrace() default StacktracePolicy.INCLUDED;
StacktracePolicy stackTrace() default StacktracePolicy.DEFAULT;

int threshold() default -1;

enum StacktracePolicy {
INCLUDED, EXCLUDED
DEFAULT, INCLUDED, EXCLUDED
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
48 changes: 45 additions & 3 deletions src/test/java/dev/morling/jfrunit/JfrUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package dev.morling.jfrunit;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.Duration;
Expand All @@ -31,6 +32,7 @@ public class JfrUnitTest {
@Test
@EnableEvent("jdk.GarbageCollection")
@EnableEvent("jdk.ThreadSleep")
@DisplayName("Should have Gc and Sleep events recorded when explicitly enabled individually with @EnableEvent")
public void shouldHaveGcAndSleepEvents() throws Exception {
System.gc();
Thread.sleep(1000);
Expand All @@ -48,6 +50,7 @@ public void shouldHaveGcAndSleepEvents() throws Exception {

@Test
@EnableConfiguration("profile")
@DisplayName("Should have Gc and Sleep events recorded when enabled with configuration 'profile'")
public void shouldHaveGcAndSleepEventsWithDefaultConfiguration() throws Exception {
System.gc();
Thread.sleep(1000);
Expand All @@ -73,31 +76,70 @@ public void shouldHaveGcAndSleepEventsWithDefaultConfiguration() throws Exceptio

@Test
@EnableEvent("jdk.ThreadSleep")
public void shouldHaveStackTraceCapturedWithNoStackTracePolicyDefined() throws Exception {
@DisplayName("Should have StackTrace captured for StackTrace-Enabled Events by default with StackTrace policy Default")
public void captureTracesWhenEnabledWithPolicyDefault() throws Exception {
Thread.sleep(1000);

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jdk.ThreadSleep").has("stackTrace"));
}

@Test
@EnableEvent("jfrunit.test.StackTraceDisabledSampleEvent")
@DisplayName("Should not have StackTrace captured for StackTrace-Disabled Events by default with StackTrace policy Default")
public void doNotCaptureTracesWhenDisabledWithPolicyDefault() {
StackTraceDisabledSampleEvent event = new StackTraceDisabledSampleEvent();
event.commit();

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jfrunit.test.StackTraceDisabledSampleEvent").hasNot("stackTrace"));
}

@Test
@EnableEvent(value = "jdk.ThreadSleep", stackTrace = EnableEvent.StacktracePolicy.INCLUDED)
public void shouldHaveStackTraceCapturedWithStackTracePolicyIncluded() throws Exception {
@DisplayName("Should have StackTrace captured irrespective of Event StackTrace Configuration(Enabled) with StackTrace policy Included")
public void captureTraceWhenEnabledWithStackTracePolicyIncluded() throws Exception {
Thread.sleep(1000);

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jdk.ThreadSleep").has("stackTrace"));
}

@Test
@EnableEvent(value = "jfrunit.test.StackTraceDisabledSampleEvent", stackTrace = EnableEvent.StacktracePolicy.INCLUDED)
@DisplayName("Should have StackTrace captured irrespective of Event StackTrace Configuration(Disabled) with StackTrace policy Included")
public void captureTraceWhenDisabledWithStackTracePolicyIncluded() {
StackTraceDisabledSampleEvent event = new StackTraceDisabledSampleEvent();
event.commit();

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jfrunit.test.StackTraceDisabledSampleEvent").has("stackTrace"));
}

@Test
@EnableEvent(value = "jdk.ThreadSleep", stackTrace = EnableEvent.StacktracePolicy.EXCLUDED)
public void shouldNotHaveStackTraceCapturedWithStackTracePolicyExcluded() throws Exception {
@DisplayName("Should not have StackTrace captured irrespective of Event StackTrace Configuration(Enabled) with StackTrace policy Excluded")
public void doNotCaptureTraceWhenEnabledWithStackTracePolicyExcluded() throws Exception {
Thread.sleep(1000);

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jdk.ThreadSleep").hasNot("stackTrace"));
}

@Test
@EnableEvent(value = "jfrunit.test.StackTraceDisabledSampleEvent", stackTrace = EnableEvent.StacktracePolicy.EXCLUDED)
@DisplayName("Should not have StackTrace captured irrespective of Event StackTrace Configuration(Disabled) with StackTrace policy Excluded")
public void doNotCaptureTraceWhenDisabledWithStackTracePolicyExcluded() {
StackTraceDisabledSampleEvent event = new StackTraceDisabledSampleEvent();
event.commit();

jfrEvents.awaitEvents();

assertThat(jfrEvents).contains(event("jfrunit.test.StackTraceDisabledSampleEvent").hasNot("stackTrace"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.morling.jfrunit;

import jdk.jfr.Category;
import jdk.jfr.Event;
import jdk.jfr.Name;
import jdk.jfr.StackTrace;

@Name(StackTraceDisabledSampleEvent.EVENT_NAME)
@Category("JfrUnit")
@StackTrace(false)
public class StackTraceDisabledSampleEvent extends Event {

public static final String EVENT_NAME = "jfrunit.test.StackTraceDisabledSampleEvent";

}

0 comments on commit f665b10

Please sign in to comment.