|
| 1 | +package io.sentry.asyncprofiler.init |
| 2 | + |
| 3 | +import io.sentry.ILogger |
| 4 | +import io.sentry.ISentryExecutorService |
| 5 | +import io.sentry.NoOpContinuousProfiler |
| 6 | +import io.sentry.NoOpProfileConverter |
| 7 | +import io.sentry.SentryOptions |
| 8 | +import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler |
| 9 | +import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider |
| 10 | +import io.sentry.util.InitUtil |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertNotNull |
| 13 | +import kotlin.test.assertSame |
| 14 | +import org.mockito.kotlin.mock |
| 15 | + |
| 16 | +class AsyncProfilerInitUtilTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + fun `initialize Profiler returns no-op profiler if profiling disabled`() { |
| 20 | + val options = SentryOptions() |
| 21 | + val profiler = InitUtil.initializeProfiler(options) |
| 22 | + assert(profiler is NoOpContinuousProfiler) |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `initialize Converter returns no-op converter if profiling disabled`() { |
| 27 | + val options = SentryOptions() |
| 28 | + val converter = InitUtil.initializeProfileConverter(options) |
| 29 | + assert(converter is NoOpProfileConverter) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `initialize profiler returns the existing profiler from options if already initialized`() { |
| 34 | + val initialProfiler = |
| 35 | + JavaContinuousProfiler(mock<ILogger>(), "", 10, mock<ISentryExecutorService>()) |
| 36 | + val options = |
| 37 | + SentryOptions().also { |
| 38 | + it.setProfileSessionSampleRate(1.0) |
| 39 | + it.setContinuousProfiler(initialProfiler) |
| 40 | + } |
| 41 | + |
| 42 | + val profiler = InitUtil.initializeProfiler(options) |
| 43 | + assertSame(initialProfiler, profiler) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `initialize converter returns the existing converter from options if already initialized`() { |
| 48 | + val initialConverter = AsyncProfilerProfileConverterProvider.AsyncProfilerProfileConverter() |
| 49 | + val options = |
| 50 | + SentryOptions().also { |
| 51 | + it.setProfileSessionSampleRate(1.0) |
| 52 | + it.profilerConverter = initialConverter |
| 53 | + } |
| 54 | + |
| 55 | + val converter = InitUtil.initializeProfileConverter(options) |
| 56 | + assertSame(initialConverter, converter) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `initialize Profiler returns JavaContinuousProfiler if profiling enabled but profiler not yet initialized`() { |
| 61 | + val options = SentryOptions().also { it.setProfileSessionSampleRate(1.0) } |
| 62 | + val profiler = InitUtil.initializeProfiler(options) |
| 63 | + assertSame(profiler, options.continuousProfiler) |
| 64 | + assert(profiler is JavaContinuousProfiler) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `initialize Converter returns AsyncProfilerProfileConverterProvider if profiling enabled but profiler not yet initialized`() { |
| 69 | + val options = SentryOptions().also { it.setProfileSessionSampleRate(1.0) } |
| 70 | + val converter = InitUtil.initializeProfileConverter(options) |
| 71 | + assertSame(converter, options.profilerConverter) |
| 72 | + assert(converter is AsyncProfilerProfileConverterProvider.AsyncProfilerProfileConverter) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `initialize profiler uses existing profilingTracesDirPath when set`() { |
| 77 | + val customPath = "/custom/path/to/traces" |
| 78 | + val options = |
| 79 | + SentryOptions().also { |
| 80 | + it.setProfileSessionSampleRate(1.0) |
| 81 | + it.profilingTracesDirPath = customPath |
| 82 | + } |
| 83 | + val profiler = InitUtil.initializeProfiler(options) |
| 84 | + assert(profiler is JavaContinuousProfiler) |
| 85 | + assertSame(customPath, options.profilingTracesDirPath) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `initialize profiler creates and sets profilingTracesDirPath when null`() { |
| 90 | + val options = SentryOptions().also { it.setProfileSessionSampleRate(1.0) } |
| 91 | + val profiler = InitUtil.initializeProfiler(options) |
| 92 | + assert(profiler is JavaContinuousProfiler) |
| 93 | + assertNotNull(options.profilingTracesDirPath) |
| 94 | + assert(options.profilingTracesDirPath!!.contains("sentry_profiling_traces")) |
| 95 | + } |
| 96 | +} |
0 commit comments