Skip to content

Commit

Permalink
Feature/Dedicated project for Tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-dig authored Sep 6, 2022
1 parent 6a00bb2 commit 80bcf5e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 163 deletions.
6 changes: 6 additions & 0 deletions OpenTelemetry.Instrumentation.Digma.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Instrumentati
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Digma.MassTransit.Integration", "src\Digma.MassTransit.Integration\Digma.MassTransit.Integration.csproj", "{61EA0936-F7BE-4A78-85D7-4C312038CDE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Instrumentation.Digma.Tests", "src\OpenTelemetry.Instrumentation.Digma.Tests\OpenTelemetry.Instrumentation.Digma.Tests.csproj", "{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61EA0936-F7BE-4A78-85D7-4C312038CDE4}.Release|Any CPU.Build.0 = Release|Any CPU
{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79CE5248-0B20-488A-9BEB-2ECC5D936CAB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
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>
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 src/OpenTelemetry.Instrumentation.Digma.Tests/TestTracingDecorator.cs
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));
}
}
}

This file was deleted.

106 changes: 0 additions & 106 deletions src/OpenTelemetry.Instrumentation.Digma/Tests/TestTracingDecorator.cs

This file was deleted.

0 comments on commit 80bcf5e

Please sign in to comment.