|
| 1 | +/* |
| 2 | + * Copyright 2026 Datadog |
| 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 | + * http://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 com.datadog.profiling.controller; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import datadog.trace.api.profiling.ProfilingSnapshot; |
| 23 | +import datadog.trace.api.profiling.RecordingData; |
| 24 | +import datadog.trace.api.profiling.RecordingInputStream; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.time.Instant; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
| 32 | +import javax.annotation.Nonnull; |
| 33 | +import javax.annotation.Nullable; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for RecordingData reference counting with multiple handlers. The reference count starts at |
| 38 | + * 1 (the base reference). The primary listener releases the base reference; each additional |
| 39 | + * listener calls retain() then release(). |
| 40 | + */ |
| 41 | +public class RecordingDataRefCountingTest { |
| 42 | + |
| 43 | + /** Test RecordingData implementation that tracks release calls. */ |
| 44 | + private static class TestRecordingData extends RecordingData { |
| 45 | + private final AtomicInteger releaseCount = new AtomicInteger(0); |
| 46 | + private final CountDownLatch releaseLatch = new CountDownLatch(1); |
| 47 | + |
| 48 | + public TestRecordingData() { |
| 49 | + super(Instant.now(), Instant.now(), ProfilingSnapshot.Kind.PERIODIC); |
| 50 | + } |
| 51 | + |
| 52 | + @Nonnull |
| 53 | + @Override |
| 54 | + public RecordingInputStream getStream() throws IOException { |
| 55 | + return new RecordingInputStream(new ByteArrayInputStream(new byte[0])); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void doRelease() { |
| 60 | + releaseCount.incrementAndGet(); |
| 61 | + releaseLatch.countDown(); |
| 62 | + } |
| 63 | + |
| 64 | + @Nullable |
| 65 | + @Override |
| 66 | + public Path getPath() { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String getName() { |
| 72 | + return "test-recording"; |
| 73 | + } |
| 74 | + |
| 75 | + public int getReleaseCount() { |
| 76 | + return releaseCount.get(); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean awaitRelease(long timeout, TimeUnit unit) throws InterruptedException { |
| 80 | + return releaseLatch.await(timeout, unit); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testSingleHandler() throws InterruptedException { |
| 86 | + TestRecordingData data = new TestRecordingData(); |
| 87 | + |
| 88 | + // Single handler: just release the base reference (no retain needed) |
| 89 | + data.release(); |
| 90 | + |
| 91 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 92 | + assertEquals(1, data.getReleaseCount(), "doRelease() should be called exactly once"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testTwoHandlers() throws InterruptedException { |
| 97 | + TestRecordingData data = new TestRecordingData(); |
| 98 | + |
| 99 | + // Two handlers: retain once for the additional handler, release twice |
| 100 | + data.retain(); // Additional handler (e.g., OTLP) |
| 101 | + assertEquals(0, data.getReleaseCount(), "Should not be released yet"); |
| 102 | + |
| 103 | + // First handler releases (base reference) |
| 104 | + data.release(); |
| 105 | + assertEquals(0, data.getReleaseCount(), "Should not be released after first release"); |
| 106 | + |
| 107 | + // Second handler releases |
| 108 | + data.release(); |
| 109 | + |
| 110 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 111 | + assertEquals(1, data.getReleaseCount(), "doRelease() should be called exactly once"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testThreeHandlers() throws InterruptedException { |
| 116 | + TestRecordingData data = new TestRecordingData(); |
| 117 | + |
| 118 | + // Three handlers: retain twice, release three times |
| 119 | + data.retain(); // Additional handler 1 |
| 120 | + data.retain(); // Additional handler 2 |
| 121 | + assertEquals(0, data.getReleaseCount(), "Should not be released yet"); |
| 122 | + |
| 123 | + // First two handlers release |
| 124 | + data.release(); |
| 125 | + data.release(); |
| 126 | + assertEquals(0, data.getReleaseCount(), "Should not be released after two releases"); |
| 127 | + |
| 128 | + // Third handler releases |
| 129 | + data.release(); |
| 130 | + |
| 131 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 132 | + assertEquals(1, data.getReleaseCount(), "doRelease() should be called exactly once"); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testOverReleaseIsSilentNoOp() throws InterruptedException { |
| 137 | + TestRecordingData data = new TestRecordingData(); |
| 138 | + |
| 139 | + // Release the base reference |
| 140 | + data.release(); |
| 141 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 142 | + |
| 143 | + // Additional release after full release is a silent no-op — unlike retain() after full release |
| 144 | + data.release(); |
| 145 | + assertEquals(1, data.getReleaseCount(), "doRelease() should still be called exactly once"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testRetainAfterFullRelease() throws InterruptedException { |
| 150 | + TestRecordingData data = new TestRecordingData(); |
| 151 | + |
| 152 | + data.release(); |
| 153 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 154 | + |
| 155 | + // Cannot retain after full release |
| 156 | + assertThrows( |
| 157 | + IllegalStateException.class, data::retain, "Should throw when retaining after release"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testMultipleReleaseIdempotent() throws InterruptedException { |
| 162 | + TestRecordingData data = new TestRecordingData(); |
| 163 | + |
| 164 | + data.release(); |
| 165 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 166 | + |
| 167 | + // Additional release calls should be silent no-ops (released flag guards them) |
| 168 | + data.release(); |
| 169 | + data.release(); |
| 170 | + |
| 171 | + assertEquals(1, data.getReleaseCount(), "doRelease() should still be called exactly once"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testConcurrentHandlers() throws InterruptedException { |
| 176 | + TestRecordingData data = new TestRecordingData(); |
| 177 | + int numAdditionalHandlers = 9; // total 10 with the base reference |
| 178 | + |
| 179 | + // Retain for all additional handlers |
| 180 | + for (int i = 0; i < numAdditionalHandlers; i++) { |
| 181 | + data.retain(); |
| 182 | + } |
| 183 | + |
| 184 | + int totalHandlers = numAdditionalHandlers + 1; |
| 185 | + |
| 186 | + // Simulate concurrent release from multiple threads |
| 187 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 188 | + CountDownLatch doneLatch = new CountDownLatch(totalHandlers); |
| 189 | + |
| 190 | + for (int i = 0; i < totalHandlers; i++) { |
| 191 | + new Thread( |
| 192 | + () -> { |
| 193 | + try { |
| 194 | + startLatch.await(); |
| 195 | + data.release(); |
| 196 | + } catch (InterruptedException e) { |
| 197 | + Thread.currentThread().interrupt(); |
| 198 | + } finally { |
| 199 | + // count down even on interrupt, otherwise it masks as a liveness timeout below |
| 200 | + doneLatch.countDown(); |
| 201 | + } |
| 202 | + }) |
| 203 | + .start(); |
| 204 | + } |
| 205 | + |
| 206 | + // Start all threads |
| 207 | + startLatch.countDown(); |
| 208 | + |
| 209 | + // Wait for all threads to complete |
| 210 | + assertTrue(doneLatch.await(5, TimeUnit.SECONDS), "All threads should complete"); |
| 211 | + assertTrue(data.awaitRelease(1, TimeUnit.SECONDS), "Release should be called"); |
| 212 | + assertEquals(1, data.getReleaseCount(), "doRelease() should be called exactly once"); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testRetainChaining() { |
| 217 | + TestRecordingData data = new TestRecordingData(); |
| 218 | + |
| 219 | + // retain() should return this for chaining |
| 220 | + RecordingData result = data.retain(); |
| 221 | + assertEquals(data, result, "retain() should return the same instance"); |
| 222 | + } |
| 223 | +} |
0 commit comments