Skip to content

Commit

Permalink
Update TagsTest to run builds on Java 19 (#3437)
Browse files Browse the repository at this point in the history
`ofEmptyDoesNotAllocate()` and `andEmptyDoesNotAllocate()` in TagsTest were failing as their allocated bytes have been changed from 0 to 16 somehow. I'm not sure what makes the difference, so it might be better to investigate further with a dedicated issue if possible.

See gh-3431
See gh-3436
  • Loading branch information
izeye committed Sep 27, 2022
1 parent 9425188 commit d99e554
Showing 1 changed file with 37 additions and 3 deletions.
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

0 comments on commit d99e554

Please sign in to comment.