|
| 1 | +package datadog.trace.instrumentation.locksupport; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import datadog.trace.instrumentation.locksupport.LockSupportProfilingInstrumentation.State; |
| 10 | +import java.util.concurrent.CountDownLatch; |
| 11 | +import java.util.concurrent.atomic.AtomicLong; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for {@link LockSupportProfilingInstrumentation}. |
| 19 | + * |
| 20 | + * <p>These tests exercise the {@link State} map directly, verifying the mechanism used to |
| 21 | + * communicate the unblocking span ID from {@code UnparkAdvice} to {@code ParkAdvice}. |
| 22 | + */ |
| 23 | +class LockSupportProfilingInstrumentationTest { |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void clearState() { |
| 27 | + State.UNPARKING_SPAN.clear(); |
| 28 | + } |
| 29 | + |
| 30 | + @AfterEach |
| 31 | + void cleanupState() { |
| 32 | + State.UNPARKING_SPAN.clear(); |
| 33 | + } |
| 34 | + |
| 35 | + // ------------------------------------------------------------------------- |
| 36 | + // State map — basic contract |
| 37 | + // ------------------------------------------------------------------------- |
| 38 | + |
| 39 | + @Test |
| 40 | + void state_put_and_remove() { |
| 41 | + Thread t = Thread.currentThread(); |
| 42 | + long spanId = 12345L; |
| 43 | + |
| 44 | + State.UNPARKING_SPAN.put(t, spanId); |
| 45 | + Long retrieved = State.UNPARKING_SPAN.remove(t); |
| 46 | + |
| 47 | + assertNotNull(retrieved); |
| 48 | + assertEquals(spanId, (long) retrieved); |
| 49 | + // After removal the entry should be gone |
| 50 | + assertNull(State.UNPARKING_SPAN.get(t)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void state_remove_returns_null_when_absent() { |
| 55 | + Thread t = new Thread(() -> {}); |
| 56 | + assertNull(State.UNPARKING_SPAN.remove(t)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void state_is_initially_empty() { |
| 61 | + assertTrue(State.UNPARKING_SPAN.isEmpty()); |
| 62 | + } |
| 63 | + |
| 64 | + // ------------------------------------------------------------------------- |
| 65 | + // Multithreaded: unpark thread populates map, parked thread reads it |
| 66 | + // ------------------------------------------------------------------------- |
| 67 | + |
| 68 | + /** |
| 69 | + * Simulates the UnparkAdvice → ParkAdvice handoff: |
| 70 | + * |
| 71 | + * <ol> |
| 72 | + * <li>Thread A (the "parked" thread) blocks on a latch. |
| 73 | + * <li>Thread B (the "unparking" thread) places its span ID in {@code State.UNPARKING_SPAN} for |
| 74 | + * Thread A and then releases the latch. |
| 75 | + * <li>Thread A wakes up, reads and removes the span ID from the map. |
| 76 | + * </ol> |
| 77 | + */ |
| 78 | + @Test |
| 79 | + void unparking_spanId_is_visible_to_parked_thread() throws InterruptedException { |
| 80 | + long unparkingSpanId = 99887766L; |
| 81 | + |
| 82 | + CountDownLatch ready = new CountDownLatch(1); |
| 83 | + CountDownLatch go = new CountDownLatch(1); |
| 84 | + AtomicLong capturedSpanId = new AtomicLong(-1L); |
| 85 | + AtomicReference<Thread> parkedThreadRef = new AtomicReference<>(); |
| 86 | + |
| 87 | + Thread parkedThread = |
| 88 | + new Thread( |
| 89 | + () -> { |
| 90 | + parkedThreadRef.set(Thread.currentThread()); |
| 91 | + ready.countDown(); |
| 92 | + try { |
| 93 | + go.await(); |
| 94 | + } catch (InterruptedException e) { |
| 95 | + Thread.currentThread().interrupt(); |
| 96 | + } |
| 97 | + |
| 98 | + // Simulate what ParkAdvice.after does: read and remove unblocking span id |
| 99 | + Long unblockingId = State.UNPARKING_SPAN.remove(Thread.currentThread()); |
| 100 | + capturedSpanId.set(unblockingId != null ? unblockingId : 0L); |
| 101 | + }); |
| 102 | + |
| 103 | + parkedThread.start(); |
| 104 | + ready.await(); // wait for parked thread to register itself |
| 105 | + |
| 106 | + // Simulate what UnparkAdvice.before does: record unparking span id |
| 107 | + State.UNPARKING_SPAN.put(parkedThread, unparkingSpanId); |
| 108 | + go.countDown(); // unblock parked thread |
| 109 | + |
| 110 | + parkedThread.join(2_000); |
| 111 | + assertFalse(parkedThread.isAlive(), "Test thread did not finish in time"); |
| 112 | + assertEquals( |
| 113 | + unparkingSpanId, |
| 114 | + capturedSpanId.get(), |
| 115 | + "Parked thread should have read the unblocking span id placed by unparking thread"); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Verifies that if no entry exists for the parked thread (i.e. the thread was unblocked by a |
| 120 | + * non-traced thread), the {@code remove} returns {@code null} and the code falls back to 0. |
| 121 | + */ |
| 122 | + @Test |
| 123 | + void no_unparking_entry_yields_zero() throws InterruptedException { |
| 124 | + AtomicLong capturedSpanId = new AtomicLong(-1L); |
| 125 | + |
| 126 | + Thread parkedThread = |
| 127 | + new Thread( |
| 128 | + () -> { |
| 129 | + Long unblockingId = State.UNPARKING_SPAN.remove(Thread.currentThread()); |
| 130 | + capturedSpanId.set(unblockingId != null ? unblockingId : 0L); |
| 131 | + }); |
| 132 | + parkedThread.start(); |
| 133 | + parkedThread.join(2_000); |
| 134 | + |
| 135 | + assertEquals( |
| 136 | + 0L, capturedSpanId.get(), "Should fall back to 0 when no unparking span id is recorded"); |
| 137 | + } |
| 138 | + |
| 139 | + // ------------------------------------------------------------------------- |
| 140 | + // ParkAdvice.after — null state is a no-op |
| 141 | + // ------------------------------------------------------------------------- |
| 142 | + |
| 143 | + /** |
| 144 | + * When {@code ParkAdvice.before} returns {@code null} (profiler not active or no active span), |
| 145 | + * {@code ParkAdvice.after} must be a no-op and must not throw. |
| 146 | + */ |
| 147 | + @Test |
| 148 | + void parkAdvice_after_null_state_isNoOp() { |
| 149 | + // Should not throw and should not touch State.UNPARKING_SPAN |
| 150 | + LockSupportProfilingInstrumentation.ParkAdvice.after(null); |
| 151 | + assertTrue(State.UNPARKING_SPAN.isEmpty()); |
| 152 | + } |
| 153 | +} |
0 commit comments