diff --git a/micrometer-java21/build.gradle b/micrometer-java21/build.gradle index c8700350f4..ffec0729c9 100644 --- a/micrometer-java21/build.gradle +++ b/micrometer-java21/build.gradle @@ -28,8 +28,13 @@ task reflectiveTests(type: Test) { includeTags 'reflective' } - // This hack is needed since VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang, see its javadoc - jvmArgs += ['--add-opens', 'java.base/java.lang=ALL-UNNAMED'] + // This hack is needed for the following tests: + // - VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang. + // - ExecutorServiceMetricsReflectiveTests utilizes reflection against java.util.concurrent. + jvmArgs += [ + '--add-opens', 'java.base/java.lang=ALL-UNNAMED', + '--add-opens', 'java.base/java.util.concurrent=ALL-UNNAMED' + ] } test { diff --git a/micrometer-java21/src/test/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetricsReflectiveTests.java b/micrometer-java21/src/test/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetricsReflectiveTests.java new file mode 100644 index 0000000000..0b79c6ac4c --- /dev/null +++ b/micrometer-java21/src/test/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetricsReflectiveTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2025 VMware, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.core.instrument.binder.jvm; + +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.*; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ExecutorServiceMetrics} with reflection enabled. + * + * @author Tommy Ludwig + */ +@Tag("reflective") +class ExecutorServiceMetricsReflectiveTests { + + SimpleMeterRegistry registry = new SimpleMeterRegistry(); + + @Test + void threadPoolMetricsWith_AutoShutdownDelegatedExecutorService() throws InterruptedException { + CountDownLatch latch = new CountDownLatch(1); + ExecutorService unmonitored = Executors.newSingleThreadExecutor(); + assertThat(unmonitored.getClass().getName()) + .isEqualTo("java.util.concurrent.Executors$AutoShutdownDelegatedExecutorService"); + ExecutorService monitored = ExecutorServiceMetrics.monitor(registry, unmonitored, "test"); + monitored.execute(latch::countDown); + assertThat(latch.await(100, TimeUnit.MILLISECONDS)).isTrue(); + assertThat(registry.get("executor.completed").tag("name", "test").functionCounter().count()).isEqualTo(1L); + } + +}