Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for tracing, and make sure the top-level operation is corre…
…ctly identified.
- Loading branch information
1 parent
f990379
commit c5c6850
Showing
5 changed files
with
298 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
test/Autofac.Specification.Test/Diagnostics/DefaultDiagnosticTracerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using Autofac.Core.Diagnostics; | ||
using Xunit; | ||
|
||
namespace Autofac.Specification.Test.Diagnostics | ||
{ | ||
public class DefaultDiagnosticTracerTests | ||
{ | ||
[Fact] | ||
public void DiagnosticTracerRaisesEvents() | ||
{ | ||
var tracer = new DefaultDiagnosticTracer(); | ||
|
||
var containerBuilder = new ContainerBuilder(); | ||
containerBuilder.Register(ctxt => "Hello"); | ||
|
||
var container = containerBuilder.Build(); | ||
|
||
container.AttachTrace(tracer); | ||
|
||
string lastOpResult = null; | ||
|
||
tracer.OperationCompleted += (sender, args) => | ||
{ | ||
Assert.Same(tracer, sender); | ||
lastOpResult = args.TraceContent; | ||
}; | ||
|
||
container.Resolve<string>(); | ||
|
||
Assert.Contains("Hello", lastOpResult); | ||
} | ||
|
||
[Fact] | ||
public void DiagnosticTracerRaisesEventsOnError() | ||
{ | ||
var tracer = new DefaultDiagnosticTracer(); | ||
|
||
var containerBuilder = new ContainerBuilder(); | ||
containerBuilder.Register<string>(ctxt => throw new InvalidOperationException()); | ||
|
||
var container = containerBuilder.Build(); | ||
|
||
container.AttachTrace(tracer); | ||
|
||
string lastOpResult = null; | ||
|
||
tracer.OperationCompleted += (sender, args) => | ||
{ | ||
Assert.Same(tracer, sender); | ||
lastOpResult = args.TraceContent; | ||
}; | ||
|
||
try | ||
{ | ||
container.Resolve<string>(); | ||
} | ||
catch | ||
{ | ||
} | ||
|
||
Assert.Contains(nameof(InvalidOperationException), lastOpResult); | ||
} | ||
|
||
[Fact] | ||
public void DiagnosticTracerDoesNotRaiseAnEventOnNestedOperations() | ||
{ | ||
var tracer = new DefaultDiagnosticTracer(); | ||
|
||
var containerBuilder = new ContainerBuilder(); | ||
containerBuilder.RegisterType<Implementor>().As<IService>(); | ||
containerBuilder.RegisterDecorator<Decorator, IService>(); | ||
|
||
var container = containerBuilder.Build(); | ||
|
||
container.AttachTrace(tracer); | ||
|
||
int traceCount = 0; | ||
string lastOpResult = null; | ||
|
||
tracer.OperationCompleted += (sender, args) => | ||
{ | ||
Assert.Same(tracer, sender); | ||
lastOpResult = args.TraceContent; | ||
traceCount++; | ||
}; | ||
|
||
container.Resolve<IService>(); | ||
|
||
// Only a single trace (despite the nested operations). | ||
Assert.Equal(1, traceCount); | ||
Assert.Contains("Decorator", lastOpResult); | ||
} | ||
|
||
private interface IService | ||
{ | ||
} | ||
|
||
private class Decorator : IService | ||
{ | ||
public Decorator(IService decorated) | ||
{ | ||
this.Decorated = decorated; | ||
} | ||
|
||
public IService Decorated { get; } | ||
} | ||
|
||
private class Implementor : IService | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters