From 0e265c147494c3543050cb442191d9ff7836e3f2 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 10 Jan 2024 11:56:38 +0800 Subject: [PATCH 1/2] Add key services tests for service and interceptor --- .../DefaultGrpcInterceptorActivatorTests.cs | 37 +++++++++++++++++++ .../DefaultGrpcServiceActivatorTests.cs | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcInterceptorActivatorTests.cs b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcInterceptorActivatorTests.cs index 9380c91f0..8350ba689 100644 --- a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcInterceptorActivatorTests.cs +++ b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcInterceptorActivatorTests.cs @@ -19,6 +19,7 @@ using Grpc.AspNetCore.Server.Internal; using Grpc.Core.Interceptors; using Grpc.Tests.Shared; +using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; @@ -49,6 +50,23 @@ public GrpcIntArgumentInterceptor(int x) public void Dispose() => Disposed = true; } +#if NET8_0_OR_GREATER + public class GrpcKeyServiceArgumentInterceptor : Interceptor + { + public GrpcKeyServiceArgumentInterceptor([FromKeyedServices("test")] KeyedClass c) + { + C = c; + } + + public KeyedClass C { get; } + } + + public class KeyedClass + { + public required string Key { get; init; } + } +#endif + public class GrpcIntMutexArgumentInterceptor : Interceptor { public GrpcIntMutexArgumentInterceptor(int x, Mutex mutex) @@ -94,6 +112,25 @@ public void Create_NotResolvedFromServiceProvider_CreatedByActivator() Assert.IsTrue(handle.Created); } +#if NET8_0_OR_GREATER + [Test] + public void Create_KeyService_CreatedByActivator() + { + // Arrange + var activator = new DefaultGrpcInterceptorActivator(); + var services = new ServiceCollection(); + services.AddKeyedSingleton("test", new KeyedClass { Key = "test" }); + var serviceProvider = services.BuildServiceProvider(); + + // Act + var handle = activator.Create(serviceProvider, CreateRegistration()); + + // Assert + var interceptor = (GrpcKeyServiceArgumentInterceptor)handle.Instance; + Assert.AreEqual("test", interceptor.C.Key); + } +#endif + [Test] public void Create_ResolvedFromServiceProvider_NotCreatedByActivator() { diff --git a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs index 9c6fe633a..425e5ef28 100644 --- a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs +++ b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs @@ -18,8 +18,10 @@ using Grpc.AspNetCore.Server.Internal; using Grpc.Tests.Shared; +using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; +using static Grpc.AspNetCore.Server.Tests.DefaultGrpcInterceptorActivatorTests; namespace Grpc.AspNetCore.Server.Tests; @@ -46,6 +48,22 @@ public ValueTask DisposeAsync() return default; } } +#if NET8_0_OR_GREATER + public class GrpcServiceWithKeyedService + { + public GrpcServiceWithKeyedService([FromKeyedServices("test")] KeyedClass c) + { + C = c; + } + + public KeyedClass C { get; } + } + + public class KeyedClass + { + public required string Key { get; init; } + } +#endif [Test] public void Create_NotResolvedFromServiceProvider_CreatedByActivator() @@ -61,6 +79,25 @@ public void Create_NotResolvedFromServiceProvider_CreatedByActivator() Assert.IsTrue(handle.Created); } +#if NET8_0_OR_GREATER + [Test] + public void Create_KeyedService_CreatedByActivator() + { + // Arrange + var activator = new DefaultGrpcServiceActivator(); + var services = new ServiceCollection(); + services.AddKeyedSingleton("test", new KeyedClass { Key = "test" }); + var serviceProvider = services.BuildServiceProvider(); + + // Act + var handle = activator.Create(serviceProvider); + + // Assert + var interceptor = handle.Instance; + Assert.AreEqual("test", interceptor.C.Key); + } +#endif + [Test] public void Create_ResolvedFromServiceProvider_NotCreatedByActivator() { From 401f55f99a73ce5e7c9c7d7f85d0d226c2ca1840 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 10 Jan 2024 11:57:45 +0800 Subject: [PATCH 2/2] Update --- .../DefaultGrpcServiceActivatorTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs index 425e5ef28..dbf8ed8d7 100644 --- a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs +++ b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs @@ -21,7 +21,6 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; -using static Grpc.AspNetCore.Server.Tests.DefaultGrpcInterceptorActivatorTests; namespace Grpc.AspNetCore.Server.Tests;