|
| 1 | +/* |
| 2 | + * Copyright 2024 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.java21.instrument.binder.jdk; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.Counter; |
| 19 | +import io.micrometer.core.instrument.Tags; |
| 20 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Tag; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.lang.reflect.Constructor; |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.concurrent.*; |
| 29 | +import java.util.concurrent.locks.LockSupport; |
| 30 | + |
| 31 | +import static java.lang.Thread.State.WAITING; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 33 | +import static org.awaitility.Awaitility.await; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link VirtualThreadMetrics}. If you run these tests from your IDE, |
| 37 | + * {@link #submitFailedEventsShouldBeRecorded()} might fail depending on your setup. This |
| 38 | + * is because the test (through {@link #virtualThreadFactoryFor(Executor)}) utilizes |
| 39 | + * reflection against the {@code java.lang} package which needs to be explicitly enabled. |
| 40 | + * If you run into such an issue you can either change your setup and let your IDE run the |
| 41 | + * tests utilizing the build system (Gradle) or add the following JVM arg to your test |
| 42 | + * config: {@code --add-opens java.base/java.lang=ALL-UNNAMED} |
| 43 | + * |
| 44 | + * @author Artyom Gabeev |
| 45 | + * @author Jonatan Ivanov |
| 46 | + */ |
| 47 | +@Tag("reflective") |
| 48 | +class VirtualThreadMetricsReflectiveTests { |
| 49 | + |
| 50 | + private static final Tags TAGS = Tags.of("k", "v"); |
| 51 | + |
| 52 | + private SimpleMeterRegistry registry; |
| 53 | + |
| 54 | + private VirtualThreadMetrics virtualThreadMetrics; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void setUp() { |
| 58 | + registry = new SimpleMeterRegistry(); |
| 59 | + virtualThreadMetrics = new VirtualThreadMetrics(TAGS); |
| 60 | + virtualThreadMetrics.bindTo(registry); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void tearDown() { |
| 65 | + virtualThreadMetrics.close(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Uses a similar approach as the JDK tests to make starting or unparking a virtual |
| 70 | + * thread fail, see {@link #virtualThreadFactoryFor(Executor)} and <a href= |
| 71 | + * "https://github.com/openjdk/jdk/blob/fdfe503d016086cf78b5a8c27dbe45f0261c68ab/test/jdk/java/lang/Thread/virtual/JfrEvents.java#L143-L187">JfrEvents.java</a> |
| 72 | + */ |
| 73 | + @Test |
| 74 | + void submitFailedEventsShouldBeRecorded() { |
| 75 | + try (ExecutorService cachedPool = Executors.newCachedThreadPool()) { |
| 76 | + ThreadFactory factory = virtualThreadFactoryFor(cachedPool); |
| 77 | + Thread thread = factory.newThread(LockSupport::park); |
| 78 | + thread.start(); |
| 79 | + |
| 80 | + await().atMost(Duration.ofSeconds(2)).until(() -> thread.getState() == WAITING); |
| 81 | + cachedPool.shutdown(); |
| 82 | + |
| 83 | + // unpark, the pool was shut down, this should fail |
| 84 | + assertThatThrownBy(() -> LockSupport.unpark(thread)).isInstanceOf(RejectedExecutionException.class); |
| 85 | + |
| 86 | + Counter counter = registry.get("jvm.threads.virtual.submit.failed").tags(TAGS).counter(); |
| 87 | + await().atMost(Duration.ofSeconds(2)).until(() -> counter.count() == 1); |
| 88 | + |
| 89 | + // park, the pool was shut down, this should fail |
| 90 | + assertThatThrownBy(() -> factory.newThread(LockSupport::park).start()) |
| 91 | + .isInstanceOf(RejectedExecutionException.class); |
| 92 | + await().atMost(Duration.ofSeconds(2)).until(() -> counter.count() == 2); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates a {@link ThreadFactory} for virtual threads. The created virtual threads |
| 98 | + * will be bound to the provided platform thread pool instead of a default |
| 99 | + * ForkJoinPool. At its current form, this is a hack, it utilizes reflection to supply |
| 100 | + * the platform thread pool. It seems though there is no other way of doing this, the |
| 101 | + * JDK tests are also utilizing reflection to do the same, see: <a href= |
| 102 | + * "https://github.com/openjdk/jdk/blob/fdfe503d016086cf78b5a8c27dbe45f0261c68ab/test/lib/jdk/test/lib/thread/VThreadScheduler.java#L71-L90">VThreadScheduler.java</a> |
| 103 | + * @param pool platform pool |
| 104 | + * @return virtual thread factory bound to the provided platform pool |
| 105 | + */ |
| 106 | + private static ThreadFactory virtualThreadFactoryFor(Executor pool) { |
| 107 | + try { |
| 108 | + Class<?> clazz = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder"); |
| 109 | + Constructor<?> constructor = clazz.getDeclaredConstructor(Executor.class); |
| 110 | + constructor.setAccessible(true); |
| 111 | + return ((Thread.Builder.OfVirtual) constructor.newInstance(pool)).factory(); |
| 112 | + } |
| 113 | + catch (Exception e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments