-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Dedicated project for Tests (#14)
- Loading branch information
Showing
6 changed files
with
191 additions
and
163 deletions.
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
16 changes: 16 additions & 0 deletions
16
...penTelemetry.Instrumentation.Digma.Tests/OpenTelemetry.Instrumentation.Digma.Tests.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MSTest" Version="2.2.10" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\OpenTelemetry.Instrumentation.Digma\OpenTelemetry.Instrumentation.Digma.csproj" /> | ||
</ItemGroup> | ||
</Project> |
60 changes: 60 additions & 0 deletions
60
src/OpenTelemetry.Instrumentation.Digma.Tests/Stubs/DecoratedService.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,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using OpenTelemetry.Instrumentation.Digma.Helpers.Attributes; | ||
|
||
namespace OpenTelemetry.Instrumentation.Digma.Tests.Stubs; | ||
|
||
public interface IDecoratedService | ||
{ | ||
public void MethodExplicitlyMarkedForTracing(Action stateValidation); | ||
|
||
public Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation); | ||
|
||
public void MethodNotExplicitlyMarkedForTracing(Action stateValidation); | ||
|
||
public void MethodWithStrangeParams1(Action stateValidation, | ||
IList<string>[] arrayOfList, ISet<int[]> setOfArray, IDictionary<int, ICollection<string>> dict, | ||
ref int intVal); | ||
|
||
public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, | ||
bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, | ||
long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs | ||
); | ||
} | ||
|
||
public class DecoratedService : IDecoratedService | ||
{ | ||
[TraceActivity()] | ||
public void MethodExplicitlyMarkedForTracing(Action stateValidation) | ||
{ | ||
var v = Activity.Current; | ||
stateValidation(); | ||
} | ||
|
||
[TraceActivity()] | ||
public async Task AsyncMethodExplicitlyMarkedForTracing(Action stateValidation) | ||
{ | ||
stateValidation(); | ||
} | ||
|
||
public void MethodNotExplicitlyMarkedForTracing(Action stateValidation) | ||
{ | ||
stateValidation(); | ||
} | ||
|
||
public void MethodWithStrangeParams1(Action stateValidation, | ||
IList<string>[] arrayOfList, ISet<int[]> setOfArray, IDictionary<int, ICollection<string>> dict, ref int intVal) | ||
{ | ||
stateValidation(); | ||
} | ||
|
||
public void MethodJaggedAndMultiDimArraysParams(Action stateValidation, out string strVal, | ||
bool[][][] jaggedArrayOfBools, short[,,,][,][,,] multiDimArrayOfShorts, | ||
long[,,][][,][] mixMultiDimAndJaggedArraysOfLongs) | ||
{ | ||
strVal = "hello"; | ||
stateValidation(); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/OpenTelemetry.Instrumentation.Digma.Tests/TestTracingDecorator.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,109 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using OpenTelemetry.Instrumentation.Digma.Helpers; | ||
using OpenTelemetry.Instrumentation.Digma.Tests.Stubs; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Instrumentation.Digma.Tests; | ||
|
||
[TestClass] | ||
public class TestTracingDecorator | ||
{ | ||
private static readonly string ServiceInterfaceFqn = | ||
"OpenTelemetry.Instrumentation.Digma.Tests.Stubs.IDecoratedService"; | ||
|
||
[TestMethod] | ||
public void Activity_Created_For_Attribute_Marked_Method() | ||
{ | ||
DecoratedService service = new DecoratedService(); | ||
IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service); | ||
tracingDecorator.MethodExplicitlyMarkedForTracing(() => | ||
{ | ||
Assert.IsNotNull(Activity.Current); | ||
AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, | ||
"MethodExplicitlyMarkedForTracing", "Action"); | ||
}); | ||
} | ||
|
||
[TestInitialize] | ||
public void SetupOtel() | ||
{ | ||
Sdk.CreateTracerProviderBuilder() | ||
.AddSource("*") | ||
.SetResourceBuilder( | ||
ResourceBuilder.CreateDefault() | ||
.AddService(serviceName: "test", serviceVersion: "2.2")) | ||
.Build(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Activity_Created_For_Async_Attribute_Marked_Method() | ||
{ | ||
DecoratedService service = new DecoratedService(); | ||
IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service); | ||
await tracingDecorator.AsyncMethodExplicitlyMarkedForTracing(() => | ||
{ | ||
Assert.IsNotNull(Activity.Current); | ||
AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, | ||
"AsyncMethodExplicitlyMarkedForTracing", "Action"); | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void Activity_Created_MethodWithStrangeParams1() | ||
{ | ||
DecoratedService service = new DecoratedService(); | ||
IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service); | ||
int intVal = 5; | ||
tracingDecorator.MethodWithStrangeParams1(() => | ||
{ | ||
Assert.IsNotNull(Activity.Current); | ||
AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodWithStrangeParams1", | ||
"Action|IList`1[]|ISet`1|IDictionary`2|Int32&"); | ||
}, | ||
new List<string>[] { }, new HashSet<int[]>(), new Dictionary<int, ICollection<string>>(), ref intVal | ||
); | ||
} | ||
|
||
[TestMethod] | ||
public void Activity_Created_MethodJaggedAndMultiDimArraysParams() | ||
{ | ||
DecoratedService service = new DecoratedService(); | ||
IDecoratedService tracingDecorator = TraceDecorator<IDecoratedService>.Create(service); | ||
string strVal; | ||
tracingDecorator.MethodJaggedAndMultiDimArraysParams(() => | ||
{ | ||
Assert.IsNotNull(Activity.Current); | ||
AssertHasCommonTags(Activity.Current, ServiceInterfaceFqn, "MethodJaggedAndMultiDimArraysParams", | ||
"Action|String&|Boolean[][][]|Int16[,,][,][,,,]|Int64[][,][][,,]"); | ||
}, | ||
out strVal, new bool[][][] { }, new short[,,,][,][,,] { }, new long[,,][][,][] { } | ||
); | ||
} | ||
|
||
[TestMethod] | ||
public void Activity_Not_Created_For_Non_Attribute_Marked_Method_If_All_Methods_False() | ||
{ | ||
DecoratedService service = new DecoratedService(); | ||
IDecoratedService tracingDecorator = | ||
TraceDecorator<IDecoratedService>.Create(service, decorateAllMethods: false); | ||
tracingDecorator.MethodNotExplicitlyMarkedForTracing(() => { Assert.IsNull(Activity.Current); }); | ||
} | ||
|
||
private void AssertHasCommonTags(Activity? activity, | ||
string expectedClassName, string expectedMethodName, string expectedParameterTypes) | ||
{ | ||
var kvpTags = activity.Tags.ToArray(); | ||
CollectionAssert.Contains(kvpTags, new KeyValuePair<string, string>("code.namespace", expectedClassName)); | ||
CollectionAssert.Contains(kvpTags, new KeyValuePair<string, string>("code.function", expectedMethodName)); | ||
if (!string.IsNullOrWhiteSpace(expectedParameterTypes)) | ||
{ | ||
CollectionAssert.Contains(kvpTags, | ||
new KeyValuePair<string, string>("code.function.parameter.types", expectedParameterTypes)); | ||
} | ||
} | ||
} |
57 changes: 0 additions & 57 deletions
57
src/OpenTelemetry.Instrumentation.Digma/Tests/Stubs/DecoratedService.cs
This file was deleted.
Oops, something went wrong.
106 changes: 0 additions & 106 deletions
106
src/OpenTelemetry.Instrumentation.Digma/Tests/TestTracingDecorator.cs
This file was deleted.
Oops, something went wrong.