From 0cad7497d0ad59693956591ebd9bf133eac3e0ee Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 11 Jan 2024 20:18:14 +0800 Subject: [PATCH] Add key services tests for service and interceptor (#2356) --- .../DefaultGrpcInterceptorActivatorTests.cs | 37 +++++++++++++++++++ .../DefaultGrpcServiceActivatorTests.cs | 36 ++++++++++++++++++ 2 files changed, 73 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..dbf8ed8d7 100644 --- a/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs +++ b/test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs @@ -18,6 +18,7 @@ using Grpc.AspNetCore.Server.Internal; using Grpc.Tests.Shared; +using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; @@ -46,6 +47,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 +78,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() {