Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.sun.management.ThreadMXBean;
import io.micrometer.core.Issue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.*;

import java.lang.management.ManagementFactory;
import java.util.*;
Expand Down Expand Up @@ -293,7 +293,26 @@ void emptyShouldNotContainTags() {
@Issue("#3313")
@DisabledIfSystemProperty(named = "java.vm.name", matches = JAVA_VM_NAME_J9_REGEX,
disabledReason = "Sun ThreadMXBean with allocation counter not available")
@EnabledForJreRange(max = JRE.JAVA_18)
void andEmptyDoesNotAllocate() {
andEmptyDoesNotAllocate(0);
}

// See https://github.com/micrometer-metrics/micrometer/issues/3436
@Test
@Issue("#3313")
@DisabledIfSystemProperty(named = "java.vm.name", matches = JAVA_VM_NAME_J9_REGEX,
disabledReason = "Sun ThreadMXBean with allocation counter not available")
@EnabledIf("java19")
void andEmptyDoesNotAllocateOnJava19() {
andEmptyDoesNotAllocate(16);
}

static boolean java19() {
return "19".equals(System.getProperty("java.version"));
}

private void andEmptyDoesNotAllocate(int expectedAllocatedBytes) {
ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
long currentThreadId = Thread.currentThread().getId();
Tags tags = Tags.of("a", "b");
Expand All @@ -304,14 +323,29 @@ void andEmptyDoesNotAllocate() {
long allocatedBytes = threadMXBean.getThreadAllocatedBytes(currentThreadId) - allocatedBytesBefore;

assertThat(combined).isEqualTo(tags);
assertThat(allocatedBytes).isZero();
assertThat(allocatedBytes).isEqualTo(expectedAllocatedBytes);
}

@Test
@Issue("#3313")
@DisabledIfSystemProperty(named = "java.vm.name", matches = JAVA_VM_NAME_J9_REGEX,
disabledReason = "Sun ThreadMXBean with allocation counter not available")
@EnabledForJreRange(max = JRE.JAVA_18)
void ofEmptyDoesNotAllocate() {
ofEmptyDoesNotAllocate(0);
}

// See https://github.com/micrometer-metrics/micrometer/issues/3436
@Test
@Issue("#3313")
@DisabledIfSystemProperty(named = "java.vm.name", matches = JAVA_VM_NAME_J9_REGEX,
disabledReason = "Sun ThreadMXBean with allocation counter not available")
@EnabledIf("java19")
void ofEmptyDoesNotAllocateOnJava19() {
ofEmptyDoesNotAllocate(16);
}

private void ofEmptyDoesNotAllocate(int expectedAllocatedBytes) {
ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
long currentThreadId = Thread.currentThread().getId();
Tags extraTags = Tags.empty();
Expand All @@ -321,7 +355,7 @@ void ofEmptyDoesNotAllocate() {
long allocatedBytes = threadMXBean.getThreadAllocatedBytes(currentThreadId) - allocatedBytesBefore;

assertThat(of).isEqualTo(Tags.empty());
assertThat(allocatedBytes).isZero();
assertThat(allocatedBytes).isEqualTo(expectedAllocatedBytes);
}

private void assertTags(Tags tags, String... keyValues) {
Expand Down