Skip to content

Commit

Permalink
Remove Moq from Instrumentation.GrpcCore.Tests (#1468)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
  • Loading branch information
ngruson and cijothomas authored Dec 8, 2023
1 parent 2ba2ddd commit 707d9c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
3 changes: 0 additions & 3 deletions src/OpenTelemetry.Instrumentation.GrpcCore/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("OpenTelemetry.Instrumentation.GrpcCore.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2" + AssemblyInfo.MoqPublicKey)]

#if SIGNED
internal static class AssemblyInfo
{
public const string PublicKey = ", PublicKey=002400000480000094000000060200000024000052534131000400000100010051C1562A090FB0C9F391012A32198B5E5D9A60E9B80FA2D7B434C9E5CCB7259BD606E66F9660676AFC6692B8CDC6793D190904551D2103B7B22FA636DCBB8208839785BA402EA08FC00C8F1500CCEF28BBF599AA64FFB1E1D5DC1BF3420A3777BADFE697856E9D52070A50C3EA5821C80BEF17CA3ACFFA28F89DD413F096F898";
public const string MoqPublicKey = ", PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7";
}
#else
internal static class AssemblyInfo
{
public const string PublicKey = "";
public const string MoqPublicKey = "";
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace OpenTelemetry.Tests;

internal class TestTextMapPropagator : TextMapPropagator
{
public Action<PropagationContext, object, Action<object, string, string>>? OnInject { get; set; }

public Action? Extracted { get; set; }

public override ISet<string> Fields => throw new NotImplementedException();
Expand All @@ -21,6 +23,10 @@ public override PropagationContext Extract<T>(PropagationContext context, T carr

public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
{
throw new NotImplementedException();
var newAction = new Action<T, string, string>((c, k, v) => setter(c, k, v));
this.OnInject?.Invoke(
context,
carrier,
new Action<object, string, string>((c, k, v) => setter((T)c, k, v)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Tests;
using Xunit;

namespace OpenTelemetry.Instrumentation.GrpcCore.Tests;
Expand Down Expand Up @@ -365,40 +365,33 @@ static void ValidateCommonEventAttributes(ActivityEvent activityEvent)
/// <returns>A Task.</returns>
private static async Task TestHandlerSuccess(Func<Foobar.FoobarClient, Metadata, Task> clientRequestFunc, Metadata additionalMetadata)
{
var mockPropagator = new Mock<TextMapPropagator>();
var propagator = new TestTextMapPropagator();
PropagationContext capturedPropagationContext = default;
Metadata capturedCarrier = null;
var propagatorCalled = 0;
var originalMetadataCount = additionalMetadata.Count;

mockPropagator
.Setup(
x => x.Inject(
It.IsAny<PropagationContext>(),
It.IsAny<Metadata>(),
It.IsAny<Action<Metadata, string, string>>()))
.Callback<PropagationContext, Metadata, Action<Metadata, string, string>>(
(propagation, carrier, setter) =>
{
propagatorCalled++;
capturedPropagationContext = propagation;
capturedCarrier = carrier;

// Make sure the original metadata make it through
if (additionalMetadata != null)
{
Assert.Equal(capturedCarrier, additionalMetadata);
}

// Call the actual setter to ensure it updates the carrier.
// It doesn't matter what we put in
setter(capturedCarrier, "bar", "baz");
});
propagator.OnInject = (propagation, carrier, setter) =>
{
propagatorCalled++;
capturedPropagationContext = propagation;
capturedCarrier = (Metadata)carrier;

// Make sure the original metadata make it through
if (additionalMetadata != null)
{
Assert.Equal(capturedCarrier, additionalMetadata);
}

// Call the actual setter to ensure it updates the carrier.
// It doesn't matter what we put in
setter(capturedCarrier, "bar", "baz");
};

using var server = FoobarService.Start();
var interceptorOptions = new ClientTracingInterceptorOptions
{
Propagator = mockPropagator.Object,
Propagator = propagator,
RecordMessageEvents = true,
ActivityIdentifierValue = Guid.NewGuid(),
};
Expand Down Expand Up @@ -485,8 +478,8 @@ private static async Task TestHandlerFailure(
new ClientTracingInterceptor(clientInterceptorOptions),
new List<Metadata.Entry>
{
new Metadata.Entry(FoobarService.RequestHeaderFailWithStatusCode, statusCode.ToString()),
new Metadata.Entry(FoobarService.RequestHeaderErrorDescription, "fubar"),
new(FoobarService.RequestHeaderFailWithStatusCode, statusCode.ToString()),
new(FoobarService.RequestHeaderErrorDescription, "fubar"),
});

using var activityListener = new InterceptorActivityListener(clientInterceptorOptions.ActivityIdentifierValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="Grpc.Tools" Version="1.17.0" />
<PackageReference Include="Grpc" Version="[2.46.6,3.0)" />
</ItemGroup>
Expand All @@ -19,4 +18,8 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcCore\OpenTelemetry.Instrumentation.GrpcCore.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\TestTextMapPropagator.cs" Link="Includes\TestTextMapPropagator.cs" />
</ItemGroup>

</Project>

0 comments on commit 707d9c0

Please sign in to comment.