Skip to content

Commit 0cad749

Browse files
authored
Add key services tests for service and interceptor (#2356)
1 parent 4b0b358 commit 0cad749

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

test/Grpc.AspNetCore.Server.Tests/DefaultGrpcInterceptorActivatorTests.cs

+37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Grpc.AspNetCore.Server.Internal;
2020
using Grpc.Core.Interceptors;
2121
using Grpc.Tests.Shared;
22+
using Microsoft.Extensions.DependencyInjection;
2223
using Moq;
2324
using NUnit.Framework;
2425

@@ -49,6 +50,23 @@ public GrpcIntArgumentInterceptor(int x)
4950
public void Dispose() => Disposed = true;
5051
}
5152

53+
#if NET8_0_OR_GREATER
54+
public class GrpcKeyServiceArgumentInterceptor : Interceptor
55+
{
56+
public GrpcKeyServiceArgumentInterceptor([FromKeyedServices("test")] KeyedClass c)
57+
{
58+
C = c;
59+
}
60+
61+
public KeyedClass C { get; }
62+
}
63+
64+
public class KeyedClass
65+
{
66+
public required string Key { get; init; }
67+
}
68+
#endif
69+
5270
public class GrpcIntMutexArgumentInterceptor : Interceptor
5371
{
5472
public GrpcIntMutexArgumentInterceptor(int x, Mutex mutex)
@@ -94,6 +112,25 @@ public void Create_NotResolvedFromServiceProvider_CreatedByActivator()
94112
Assert.IsTrue(handle.Created);
95113
}
96114

115+
#if NET8_0_OR_GREATER
116+
[Test]
117+
public void Create_KeyService_CreatedByActivator()
118+
{
119+
// Arrange
120+
var activator = new DefaultGrpcInterceptorActivator<GrpcKeyServiceArgumentInterceptor>();
121+
var services = new ServiceCollection();
122+
services.AddKeyedSingleton("test", new KeyedClass { Key = "test" });
123+
var serviceProvider = services.BuildServiceProvider();
124+
125+
// Act
126+
var handle = activator.Create(serviceProvider, CreateRegistration<GrpcKeyServiceArgumentInterceptor>());
127+
128+
// Assert
129+
var interceptor = (GrpcKeyServiceArgumentInterceptor)handle.Instance;
130+
Assert.AreEqual("test", interceptor.C.Key);
131+
}
132+
#endif
133+
97134
[Test]
98135
public void Create_ResolvedFromServiceProvider_NotCreatedByActivator()
99136
{

test/Grpc.AspNetCore.Server.Tests/DefaultGrpcServiceActivatorTests.cs

+36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using Grpc.AspNetCore.Server.Internal;
2020
using Grpc.Tests.Shared;
21+
using Microsoft.Extensions.DependencyInjection;
2122
using Moq;
2223
using NUnit.Framework;
2324

@@ -46,6 +47,22 @@ public ValueTask DisposeAsync()
4647
return default;
4748
}
4849
}
50+
#if NET8_0_OR_GREATER
51+
public class GrpcServiceWithKeyedService
52+
{
53+
public GrpcServiceWithKeyedService([FromKeyedServices("test")] KeyedClass c)
54+
{
55+
C = c;
56+
}
57+
58+
public KeyedClass C { get; }
59+
}
60+
61+
public class KeyedClass
62+
{
63+
public required string Key { get; init; }
64+
}
65+
#endif
4966

5067
[Test]
5168
public void Create_NotResolvedFromServiceProvider_CreatedByActivator()
@@ -61,6 +78,25 @@ public void Create_NotResolvedFromServiceProvider_CreatedByActivator()
6178
Assert.IsTrue(handle.Created);
6279
}
6380

81+
#if NET8_0_OR_GREATER
82+
[Test]
83+
public void Create_KeyedService_CreatedByActivator()
84+
{
85+
// Arrange
86+
var activator = new DefaultGrpcServiceActivator<GrpcServiceWithKeyedService>();
87+
var services = new ServiceCollection();
88+
services.AddKeyedSingleton("test", new KeyedClass { Key = "test" });
89+
var serviceProvider = services.BuildServiceProvider();
90+
91+
// Act
92+
var handle = activator.Create(serviceProvider);
93+
94+
// Assert
95+
var interceptor = handle.Instance;
96+
Assert.AreEqual("test", interceptor.C.Key);
97+
}
98+
#endif
99+
64100
[Test]
65101
public void Create_ResolvedFromServiceProvider_NotCreatedByActivator()
66102
{

0 commit comments

Comments
 (0)