|
| 1 | +namespace Sentry.Profiling.Tests; |
| 2 | + |
| 3 | +#nullable enable |
| 4 | + |
| 5 | +[UsesVerify] |
| 6 | + |
| 7 | +public class ProfilingSentryOptionsExtensionsTests |
| 8 | +{ |
| 9 | + private readonly InMemoryDiagnosticLogger _logger = new(); |
| 10 | + private readonly SentryOptions _options = new() |
| 11 | + { |
| 12 | + Dsn = ValidDsn, |
| 13 | + AutoSessionTracking = false, |
| 14 | + IsGlobalModeEnabled = true, |
| 15 | + BackgroundWorker = Substitute.For<IBackgroundWorker>(), |
| 16 | + Debug = true, |
| 17 | + |
| 18 | + // Set explicitly for this test in case the defaults change in the future. |
| 19 | + TracesSampleRate = 0.0, |
| 20 | + TracesSampler = null |
| 21 | + }; |
| 22 | + |
| 23 | + public ProfilingSentryOptionsExtensionsTests() |
| 24 | + { |
| 25 | + _options.DiagnosticLogger = _logger; |
| 26 | + _options.AddProfilingIntegration(); |
| 27 | + } |
| 28 | + |
| 29 | + private Hub GetSut() => new(_options, Substitute.For<ISentryClient>()); |
| 30 | + |
| 31 | + private static IEnumerable<ISdkIntegration> GetIntegrations(ISentryClient hub) => |
| 32 | + hub.GetSentryOptions()?.Integrations ?? Enumerable.Empty<ISdkIntegration>(); |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Integration_DisabledWithDefaultOptions() |
| 36 | + { |
| 37 | + using var hub = GetSut(); |
| 38 | + var integrations = GetIntegrations(hub); |
| 39 | + Assert.Contains(_logger.Entries, x => x.Message == "Profiling Integration is disabled because profiling is disabled by configuration." |
| 40 | + && x.Level == SentryLevel.Info); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void Integration_EnabledBySampleRate() |
| 45 | + { |
| 46 | + _options.TracesSampleRate = 1.0; |
| 47 | + _options.ProfilesSampleRate = 1.0; |
| 48 | + |
| 49 | + using var hub = GetSut(); |
| 50 | + var integrations = GetIntegrations(hub); |
| 51 | + Assert.Contains(integrations, i => i is ProfilingIntegration); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void DisableProfilingIntegration_RemovesProfilingIntegration() |
| 56 | + { |
| 57 | + _options.TracesSampleRate = 1.0; |
| 58 | + _options.ProfilesSampleRate = 1.0; |
| 59 | + _options.DisableProfilingIntegration(); |
| 60 | + |
| 61 | + using var hub = GetSut(); |
| 62 | + var integrations = GetIntegrations(hub); |
| 63 | + Assert.DoesNotContain(integrations, i => i is ProfilingIntegration); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void AddProfilingIntegration_DoesntDuplicate() |
| 68 | + { |
| 69 | + var options = new SentryOptions(); |
| 70 | + |
| 71 | + options.AddProfilingIntegration(); |
| 72 | + options.AddProfilingIntegration(); |
| 73 | + |
| 74 | + Assert.Single(options.Integrations, x => x is ProfilingIntegration); |
| 75 | + } |
| 76 | +} |
0 commit comments