4040 */
4141class JvmThreadDeadlockMetricsTest {
4242
43- private static class DeadlockedThread {
44-
45- private final Thread thread ;
46-
47- DeadlockedThread (CountDownLatch lock1IsLocked , Lock lock1 , CountDownLatch lock2IsLocked , Lock lock2 ) {
48- this .thread = new Thread (() -> {
49- try {
50- lock1 .lock ();
51- lock1IsLocked .countDown ();
52- lock2IsLocked .await ();
53- lock2 .lockInterruptibly ();
54- }
55- catch (InterruptedException ignored ) {
56- }
57- });
58- }
59-
60- void start () {
61- thread .start ();
62- }
63-
64- void interrupt () {
65- thread .interrupt ();
66- }
67-
68- }
43+ MeterRegistry registry = new SimpleMeterRegistry ();
6944
7045 @ Test
7146 void deadlockedThreadMetrics () {
72- MeterRegistry registry = new SimpleMeterRegistry ();
7347 new JvmThreadDeadlockMetrics ().bindTo (registry );
7448 final CountDownLatch lock1IsLocked = new CountDownLatch (1 );
7549 final CountDownLatch lock2IsLocked = new CountDownLatch (1 );
@@ -90,16 +64,14 @@ void deadlockedThreadMetrics() {
9064 }
9165
9266 @ Test
93- void whenJvmDoesntSupportObjectMonitorUsageJvmThreadsDeadlockedMetricShouldNotBeRegistered () {
94- MeterRegistry registry = new SimpleMeterRegistry ();
67+ void whenJvmDoesntSupportSynchronizerUsage_JvmThreadsDeadlockedMetricShouldNotBeRegistered () {
9568 try (MockedStatic <ManagementFactory > mockedStatic = Mockito .mockStatic (ManagementFactory .class )) {
9669 ThreadMXBean threadBean = mock (ThreadMXBean .class );
97- when (threadBean .isObjectMonitorUsageSupported ()).thenReturn (false );
70+ when (threadBean .isSynchronizerUsageSupported ()).thenReturn (false );
9871 mockedStatic .when (ManagementFactory ::getThreadMXBean ).thenReturn (threadBean );
9972 new JvmThreadDeadlockMetrics ().bindTo (registry );
10073
101- // ObjectMonitorUsage is not supported, so this metric should not be
102- // registered
74+ // synchronizer usage is not monitored, so this should not be registered
10375 assertThat (registry .find ("jvm.threads.deadlocked" ).gauge ()).isNull ();
10476 // but this one is still supported
10577 assertThat (registry .find ("jvm.threads.deadlocked.monitor" ).gauge ()).isNotNull ();
@@ -120,4 +92,31 @@ void getDeadlockedThreadCountWhenFindMonitorDeadlockedThreadsIsNullShouldWork()
12092 assertThat (JvmThreadDeadlockMetrics .getDeadlockedMonitorThreadCount (threadBean )).isEqualTo (0 );
12193 }
12294
95+ private static class DeadlockedThread {
96+
97+ private final Thread thread ;
98+
99+ DeadlockedThread (CountDownLatch lock1IsLocked , Lock lock1 , CountDownLatch lock2IsLocked , Lock lock2 ) {
100+ this .thread = new Thread (() -> {
101+ try {
102+ lock1 .lock ();
103+ lock1IsLocked .countDown ();
104+ lock2IsLocked .await ();
105+ lock2 .lockInterruptibly ();
106+ }
107+ catch (InterruptedException ignored ) {
108+ }
109+ });
110+ }
111+
112+ void start () {
113+ thread .start ();
114+ }
115+
116+ void interrupt () {
117+ thread .interrupt ();
118+ }
119+
120+ }
121+
123122}
0 commit comments