Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key services tests for service and interceptor #2356

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<GrpcKeyServiceArgumentInterceptor>();
var services = new ServiceCollection();
services.AddKeyedSingleton("test", new KeyedClass { Key = "test" });
var serviceProvider = services.BuildServiceProvider();

// Act
var handle = activator.Create(serviceProvider, CreateRegistration<GrpcKeyServiceArgumentInterceptor>());

// Assert
var interceptor = (GrpcKeyServiceArgumentInterceptor)handle.Instance;
Assert.AreEqual("test", interceptor.C.Key);
}
#endif

[Test]
public void Create_ResolvedFromServiceProvider_NotCreatedByActivator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using Grpc.AspNetCore.Server.Internal;
using Grpc.Tests.Shared;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;

Expand Down Expand Up @@ -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()
Expand All @@ -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<GrpcServiceWithKeyedService>();
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()
{
Expand Down
Loading