Skip to content

Commit

Permalink
Add key services tests for service and interceptor (#2356)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 11, 2024
1 parent 4b0b358 commit 0cad749
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
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

0 comments on commit 0cad749

Please sign in to comment.