|
1 | 1 | package com.datadog.profiling.ddprof; |
2 | 2 |
|
| 3 | +import static com.datadog.profiling.controller.ProfilingSupport.isOldObjectSampleAvailable; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
4 | 7 |
|
| 8 | +import datadog.environment.JavaVirtualMachine; |
5 | 9 | import datadog.trace.api.config.ProfilingConfig; |
6 | 10 | import datadog.trace.bootstrap.config.provider.ConfigProvider; |
7 | 11 | import java.util.Properties; |
8 | 12 | import java.util.stream.Stream; |
9 | 13 | import org.junit.jupiter.api.AfterEach; |
10 | 14 | import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
11 | 16 | import org.junit.jupiter.params.ParameterizedTest; |
12 | 17 | import org.junit.jupiter.params.provider.Arguments; |
13 | 18 | import org.junit.jupiter.params.provider.MethodSource; |
@@ -126,4 +131,70 @@ private static Stream<Arguments> hotspotStackwalkerTestCases() { |
126 | 131 | Arguments.of("lbr", "lbr"), |
127 | 132 | Arguments.of("no", "no")); |
128 | 133 | } |
| 134 | + |
| 135 | + @Test |
| 136 | + void isMemoryLeakProfilingSafeConsistentWithComponentChecks() { |
| 137 | + boolean expected = DatadogProfilerConfig.isJmethodIDSafe() || isOldObjectSampleAvailable(); |
| 138 | + assertEquals(expected, DatadogProfilerConfig.isMemoryLeakProfilingSafe()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void unifiedFlagEnabledDdprofKeyDefaultReflectsSafety() { |
| 143 | + Properties props = new Properties(); |
| 144 | + props.put(ProfilingConfig.PROFILING_HEAP_ENABLED, "true"); |
| 145 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 146 | + // ddprof live heap requires Java 11+ (JVMTI Allocation Sampler) AND jmethodID safety |
| 147 | + boolean expectedDefault = |
| 148 | + JavaVirtualMachine.isJavaVersionAtLeast(11) && DatadogProfilerConfig.isJmethodIDSafe(); |
| 149 | + assertEquals(expectedDefault, DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void unifiedFlagDisabledOverridesDdprof() { |
| 154 | + Properties props = new Properties(); |
| 155 | + props.put(ProfilingConfig.PROFILING_HEAP_ENABLED, "false"); |
| 156 | + props.put(ProfilingConfig.PROFILING_DATADOG_PROFILER_LIVEHEAP_ENABLED, "true"); |
| 157 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 158 | + assertFalse(DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void unifiedFlagEnabledDdprofKeyDisabledReturnsFalse() { |
| 163 | + Properties props = new Properties(); |
| 164 | + props.put(ProfilingConfig.PROFILING_HEAP_ENABLED, "true"); |
| 165 | + props.put(ProfilingConfig.PROFILING_DATADOG_PROFILER_LIVEHEAP_ENABLED, "false"); |
| 166 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 167 | + assertFalse(DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void unifiedFlagEnabledDdprofKeyEnabledReturnsTrue() { |
| 172 | + Properties props = new Properties(); |
| 173 | + props.put(ProfilingConfig.PROFILING_HEAP_ENABLED, "true"); |
| 174 | + props.put(ProfilingConfig.PROFILING_DATADOG_PROFILER_LIVEHEAP_ENABLED, "true"); |
| 175 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 176 | + assertTrue(DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void oldMemleakAliasStillWorks() { |
| 181 | + Properties props = new Properties(); |
| 182 | + props.put(ProfilingConfig.PROFILING_HEAP_ENABLED, "true"); |
| 183 | + props.put(ProfilingConfig.PROFILING_DATADOG_PROFILER_MEMLEAK_ENABLED, "true"); |
| 184 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 185 | + assertTrue(DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void defaultBehaviorNoFlagsSetUsesAutoDetection() { |
| 190 | + Properties props = new Properties(); |
| 191 | + ConfigProvider config = ConfigProvider.withPropertiesOverride(props); |
| 192 | + // With no flags set: |
| 193 | + // unified default = isMemoryLeakProfilingSafe() |
| 194 | + // ddprof default = Java 11+ && isJmethodIDSafe() |
| 195 | + // result = isMemoryLeakProfilingSafe() && (Java 11+ && isJmethodIDSafe()) |
| 196 | + boolean expectedDefault = |
| 197 | + JavaVirtualMachine.isJavaVersionAtLeast(11) && DatadogProfilerConfig.isJmethodIDSafe(); |
| 198 | + assertEquals(expectedDefault, DatadogProfilerConfig.isMemoryLeakProfilingEnabled(config)); |
| 199 | + } |
129 | 200 | } |
0 commit comments