Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return NoopScope / NoopSpan for ScopeManager.Active / Tracer.ActiveSpan #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenTracing/Noop/NoopScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal sealed class NoopScopeManager : IScopeManager
{
internal static readonly NoopScopeManager Instance = new NoopScopeManager();

public IScope Active => null;
public IScope Active => NoopScope.Instance;

private NoopScopeManager()
{
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTracing/Noop/NoopTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class NoopTracer : ITracer

public IScopeManager ScopeManager => NoopScopeManager.Instance;

public ISpan ActiveSpan => null;
public ISpan ActiveSpan => NoopSpan.Instance;

private NoopTracer()
{
Expand Down
17 changes: 17 additions & 0 deletions test/OpenTracing.Tests/Noop/NoopScopeManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OpenTracing.Noop;
using Xunit;

namespace OpenTracing.Tests.Noop
{
public class NoopScopeManagerTests
{

[Fact]
public void ActiveValueToleratesUse()
{
IScope active = NoopScopeManager.Instance.Active;
Assert.NotNull(active);
active.Dispose();
}
}
}
43 changes: 22 additions & 21 deletions test/OpenTracing.Tests/Noop/NoopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ public void Tracer_uses_NoopScopeManager()
{
var tracer = NoopTracerFactory.Create();

Assert.IsType<NoopScopeManager>(tracer.ScopeManager);
Assert.Same(NoopScopeManager.Instance, tracer.ScopeManager);
}

[Fact]
public void Tracer_initially_has_no_active_span()
public void Tracer_ActiveSpan_is_NoopSpan()
{
var tracer = NoopTracerFactory.Create();

Assert.Null(tracer.ActiveSpan);
var span = tracer.ActiveSpan;
Assert.Same(NoopSpan.Instance, span);
}

[Fact]
Expand All @@ -39,7 +40,7 @@ public void BuildSpan_returns_NoopSpanBuilder()

var spanBuilder = tracer.BuildSpan("noop");

Assert.IsType<NoopSpanBuilder>(spanBuilder);
Assert.Same(NoopSpanBuilder.Instance, spanBuilder);
}

[Fact]
Expand All @@ -61,7 +62,7 @@ public void StartActive_returns_NoopScope()
var scope = tracer.BuildSpan("noop")
.StartActive(finishSpanOnDispose: false);

Assert.IsType<NoopScopeManager.NoopScope>(scope);
Assert.Same(NoopScopeManager.NoopScope.Instance, scope);
}

[Fact]
Expand All @@ -72,29 +73,29 @@ public void NoopScope_from_StartActive_has_NoopSpan()
var scope = tracer.BuildSpan("noop")
.StartActive(finishSpanOnDispose: false);

Assert.IsType<NoopSpan>(scope.Span);
Assert.Same(NoopSpan.Instance, scope.Span);
}

[Fact]
public void StartActive_does_NOT_set_Tracer_ActiveSpan()
public void StartActive_sets_Tracer_ActiveSpan()
{
var tracer = NoopTracerFactory.Create();

var scope = tracer.BuildSpan("noop")
.StartActive(finishSpanOnDispose: false);

Assert.Null(tracer.ActiveSpan);
Assert.Same(NoopSpan.Instance, tracer.ActiveSpan);
}

[Fact]
public void StartActive_does_NOT_set_ScopeManager_Active()
public void StartActive_sets_ScopeManager_Active()
{
var tracer = NoopTracerFactory.Create();

var scope = tracer.BuildSpan("noop")
.StartActive(finishSpanOnDispose: false);

Assert.Null(tracer.ScopeManager.Active);
Assert.Same(NoopScopeManager.NoopScope.Instance, tracer.ScopeManager.Active);
}

[Fact]
Expand All @@ -104,7 +105,7 @@ public void Start_returns_NoopSpan()

var span = tracer.BuildSpan("noop").Start();

Assert.IsType<NoopSpan>(span);
Assert.Same(NoopSpan.Instance, span);
}

[Fact]
Expand All @@ -119,23 +120,23 @@ public void Start_always_returns_same_Span()
}

[Fact]
public void Start_does_NOT_set_Tracer_ActiveSpan()
public void Start_sets_Tracer_ActiveSpan()
{
var tracer = NoopTracerFactory.Create();

var span = tracer.BuildSpan("noop").Start();

Assert.Null(tracer.ActiveSpan);
Assert.Same(NoopSpan.Instance, tracer.ActiveSpan);
}

[Fact]
public void Start_does_NOT_set_ScopeManager_Active()
public void Start_sets_ScopeManager_Active()
{
var tracer = NoopTracerFactory.Create();

var span = tracer.BuildSpan("noop").Start();

Assert.Null(tracer.ScopeManager.Active);
Assert.Same(NoopScopeManager.NoopScope.Instance, tracer.ScopeManager.Active);
}

[Fact]
Expand All @@ -147,20 +148,20 @@ public void ScopeManager_returns_NoopScope_on_Activate()

var scope = tracer.ScopeManager.Activate(span, finishSpanOnDispose: false);

Assert.IsType<NoopScopeManager.NoopScope>(scope);
Assert.Same(NoopScopeManager.NoopScope.Instance, scope);
}

[Fact]
public void ScopeManager_does_NOT_set_Active_on_Activate()
public void ScopeManager_sets_Active_on_Activate()
{
var tracer = NoopTracerFactory.Create();

var span = tracer.BuildSpan("noop").Start();

tracer.ScopeManager.Activate(span, finishSpanOnDispose: false);

Assert.Null(tracer.ScopeManager.Active);
Assert.Null(tracer.ActiveSpan);
Assert.Same(NoopScopeManager.NoopScope.Instance, tracer.ScopeManager.Active);
Assert.Same(NoopSpan.Instance, tracer.ActiveSpan);
}

[Fact]
Expand All @@ -170,7 +171,7 @@ public void Span_has_NoopSpanContext()

var span = tracer.BuildSpan("noop").Start();

Assert.IsType<NoopSpanContext>(span.Context);
Assert.Same(NoopSpanContext.Instance, span.Context);
}

[Fact]
Expand All @@ -182,7 +183,7 @@ public void Extract_returns_NoopSpanContext()

var spanContext = tracer.Extract(BuiltinFormats.TextMap, new TextMapExtractAdapter(carrier));

Assert.IsType<NoopSpanContext>(spanContext);
Assert.Same(NoopSpanContext.Instance, spanContext);
}

[Fact]
Expand Down
18 changes: 18 additions & 0 deletions test/OpenTracing.Tests/Noop/NoopTracerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using OpenTracing.Noop;
using OpenTracing.Tag;
using Xunit;

namespace OpenTracing.Tests.Noop
{
public class NoopTracerTest
{

[Fact]
public void ActiveSpanValueToleratesUse()
{
ISpan activeSpan = NoopTracer.Instance.ActiveSpan;
Assert.NotNull(activeSpan);
Tags.Error.Set(activeSpan, true);
}
}
}