diff --git a/ContribSentry/.Transaction/SentryTracing.cs b/ContribSentry/.Transaction/SentryTracing.cs index 5f85575..949ce0f 100644 --- a/ContribSentry/.Transaction/SentryTracing.cs +++ b/ContribSentry/.Transaction/SentryTracing.cs @@ -39,9 +39,7 @@ public SentryTracing(string name, int trackerId) } public ISpanBase GetSpan(string op) - { - return Spans.FirstOrDefault(s => s.Op == op); - } + => Spans.FirstOrDefault(s => s.Op == op) ?? DisabledSpan.Instance; public ISpanBase GetCurrentSpan() { @@ -68,7 +66,7 @@ public void Finish() if (ContribSentrySdk.IsTracingSdkEnabled && new Random().NextDouble() <= ContribSentrySdk.Options.TracesSampleRate) { var hasError = Spans.Any(p => p.Error); - Trace.SetStatus(Spans.Last().Status); + Trace.SetStatus(Spans.LastOrDefault()?.Status); var @event = new SentryTracingEvent(this, hasError); if (ContribSentrySdk.Options.RegisterTracingBreadcrumb) diff --git a/ContribSentry/ContribSentry.csproj b/ContribSentry/ContribSentry.csproj index 8842e90..78bcfe7 100644 --- a/ContribSentry/ContribSentry.csproj +++ b/ContribSentry/ContribSentry.csproj @@ -14,7 +14,7 @@ ContribSentry ContribSentry.SessionSdk MIT - 3.1.6 + 3.1.7 diff --git a/Testing/SentryTracingTests.cs b/Testing/SentryTracingTests.cs new file mode 100644 index 0000000..c3178ea --- /dev/null +++ b/Testing/SentryTracingTests.cs @@ -0,0 +1,122 @@ +using ContribSentry.Extensibility; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace ContribSentry.TracingTest +{ + public class SentryTracingTests + { + [Fact] + public void Ctor_ValidSentryTracing() + { + //Act + var sentryTracing = new SentryTracing(null, 0); + + //Assert + Assert.True(sentryTracing != null); + } + + [Fact] + public void StartChild_AddsChild() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + + //Act + var span = sentryTracing.StartChild("name","name"); + + //Assert + Assert.Contains(span, sentryTracing.Spans); + + } + + [Fact] + public void GetSpan_EmptySpan_DisabledSpan() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + + //Act + var span = sentryTracing.GetSpan("name"); + + //Assert + Assert.IsType(span); + } + + [Fact] + public void GetSpan_OpExist_SpanWithOp() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + sentryTracing.StartChild("name", "name"); + + //Act + var span = sentryTracing.GetSpan("name"); + + //Assert + Assert.IsType(span); + Assert.Equal("name", span.Op); + } + + [Fact] + public void GetCurrentSpan_NoSpan_DisabledSpan() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + + //Act + var span = sentryTracing.GetCurrentSpan(); + + //Assert + Assert.IsType(span); + } + + [Fact] + public void GetCurrentSpan_FirstSpanOpen_FirstSpan() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + sentryTracing.StartChild("name", "name"); + + //Act + var span = sentryTracing.GetCurrentSpan(); + + //Assert + Assert.IsType(span); + Assert.Equal("name", span.Op); + } + + [Fact] + public void GetCurrentSpan_FirstSpanOpenAndSecondIsChildofChild_FirstSpan() + { + //Arrange + var sentryTracing = new SentryTracing(null, 0); + var parent = sentryTracing.StartChild("name", "name"); + parent.StartChild("aaa"); + + //Act + var span = sentryTracing.GetCurrentSpan(); + + //Assert + Assert.IsType(span); + Assert.Equal("name", span.Op); + } + + [Fact] + public void Finish_NoSpan_CapturesEvent() + { + //Arrange + ContribSentrySdk.Init(new ContribSentryOptions()); + ContribSentrySdk.Options.TracesSampleRate = 1.0; + + var sentryTracing = new SentryTracing(null, 0); + + //Act + sentryTracing.Finish(); + + ContribSentrySdk.Close(); + } + } +}