From b1d57e9b0e0eec31767c2e28cc524ebdb9573f3d Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Wed, 9 Feb 2022 14:26:27 -0800 Subject: [PATCH] Add failing test for StartRootSpan (#2878) --- test/OpenTelemetry.Tests/Trace/TracerTest.cs | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/OpenTelemetry.Tests/Trace/TracerTest.cs b/test/OpenTelemetry.Tests/Trace/TracerTest.cs index 5df66f9138d..afe0c8ee849 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerTest.cs @@ -15,7 +15,9 @@ // using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Threading.Tasks; using Xunit; namespace OpenTelemetry.Trace.Tests @@ -74,6 +76,45 @@ public void Tracer_StartRootSpan_BadArgs_NullSpanName() Assert.Null(span3.Activity.DisplayName); } + [Fact(Skip = "See https://github.com/open-telemetry/opentelemetry-dotnet/issues/2803")] + public async Task Tracer_StartRootSpan_StartsNewTrace() + { + var exportedItems = new List(); + + using var tracer = Sdk.CreateTracerProviderBuilder() + .AddSource("tracername") + .AddInMemoryExporter(exportedItems) + .Build(); + + async Task DoSomeAsyncWork() + { + await Task.Delay(10); + using (tracer.GetTracer("tracername").StartRootSpan("RootSpan2")) + { + await Task.Delay(10); + } + } + + using (tracer.GetTracer("tracername").StartActiveSpan("RootSpan1")) + { + await DoSomeAsyncWork(); + } + + Assert.Equal(2, exportedItems.Count); + + var rootSpan2 = exportedItems[0]; + var rootSpan1 = exportedItems[1]; + Assert.Equal("RootSpan2", rootSpan2.DisplayName); + Assert.Equal("RootSpan1", rootSpan1.DisplayName); + Assert.Equal(default(ActivitySpanId), rootSpan1.ParentSpanId); + + // This is where this test currently fails + // rootSpan2 should be a root span of a new trace and not a child of rootSpan1 + Assert.Equal(default(ActivitySpanId), rootSpan2.ParentSpanId); + Assert.NotEqual(rootSpan2.TraceId, rootSpan1.TraceId); + Assert.NotEqual(rootSpan2.ParentSpanId, rootSpan1.SpanId); + } + [Fact] public void Tracer_StartSpan_BadArgs_NullSpanName() {