Skip to content

Commit

Permalink
Add tests for CreateSliceProxy & CreateProtobufClient (#3835)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Dec 1, 2023
1 parent 84d416a commit 6937105
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
38 changes: 38 additions & 0 deletions tests/IceRpc.Protobuf.Tests/ServiceProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) ZeroC, Inc.

using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace IceRpc.Protobuf.Tests;

[Parallelizable(scope: ParallelScope.All)]
public partial class ServiceProviderExtensionsTests
{
[Test]
public void Create_protobuf_client_with_no_params()
{
var serviceCollection =
new ServiceCollection()
.AddSingleton(InvalidInvoker.Instance)
.AddSingleton<IMyOperations>(provider => provider.CreateProtobufClient<MyOperationsClient>());

var provider = serviceCollection.BuildServiceProvider(validateScopes: true);

var client = (IProtobufClient?)provider.GetService<IMyOperations>();
Assert.That(client, Is.Not.Null);
Assert.That(client.Invoker, Is.EqualTo(InvalidInvoker.Instance));
Assert.That(client.ServiceAddress.Path, Is.EqualTo(MyOperationsClient.DefaultServicePath));
Assert.That(client.EncodeOptions, Is.Null);
}

[Test]
public void Create_protobuf_client_without_invoker_fails()
{
var serviceCollection =
new ServiceCollection()
.AddSingleton<IMyOperations>(provider => provider.CreateProtobufClient<MyOperationsClient>());

var provider = serviceCollection.BuildServiceProvider(validateScopes: true);
Assert.That(() => provider.GetService<IMyOperations>(), Throws.InvalidOperationException);
}
}
38 changes: 38 additions & 0 deletions tests/IceRpc.Slice.Tests/ServiceProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) ZeroC, Inc.

using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace IceRpc.Slice.Tests;

[Parallelizable(scope: ParallelScope.All)]
public partial class ServiceProviderExtensionsTests
{
[Test]
public void Create_slice_proxy_with_no_params()
{
var serviceCollection =
new ServiceCollection()
.AddSingleton(InvalidInvoker.Instance)
.AddSingleton<IMyOperationsA>(provider => provider.CreateSliceProxy<MyOperationsAProxy>());

var provider = serviceCollection.BuildServiceProvider(validateScopes: true);

var proxy = (IProxy?)provider.GetService<IMyOperationsA>();
Assert.That(proxy, Is.Not.Null);
Assert.That(proxy.Invoker, Is.EqualTo(InvalidInvoker.Instance));
Assert.That(proxy.ServiceAddress.Path, Is.EqualTo(MyOperationsAProxy.DefaultServicePath));
Assert.That(proxy.EncodeOptions, Is.Null);
}

[Test]
public void Create_slice_proxy_without_invoker_fails()
{
var serviceCollection =
new ServiceCollection()
.AddSingleton<IMyOperationsA>(provider => provider.CreateSliceProxy<MyOperationsAProxy>());

var provider = serviceCollection.BuildServiceProvider(validateScopes: true);
Assert.That(() => provider.GetService<IMyOperationsA>(), Throws.InvalidOperationException);
}
}
11 changes: 9 additions & 2 deletions tools/IceRpc.ProtocGen/ClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ internal static string GenerateImplementation(ServiceDescriptor service)
public ProtobufEncodeOptions? EncodeOptions {{ get; init; }}
/// <summary>Gets or initializes the invoker of this client.</summary>
public IceRpc.IInvoker Invoker {{ get; init; }}
public required IceRpc.IInvoker Invoker {{ get; init; }}
/// <summary>Gets or initializes the address of the remote service.</summary>
public IceRpc.ServiceAddress ServiceAddress {{ get; init; }}
public IceRpc.ServiceAddress ServiceAddress {{ get; init; }} = _defaultServiceAddress;
private static IceRpc.ServiceAddress _defaultServiceAddress =
new(IceRpc.Protocol.IceRpc) {{ Path = DefaultServicePath }};
Expand All @@ -131,6 +131,7 @@ internal static string GenerateImplementation(ServiceDescriptor service)
/// <param name=""serviceAddress"">The service address. <see langword=""null"" /> is equivalent to an icerpc service
/// address with path <see cref=""DefaultServicePath"" />.</param>
/// <param name=""encodeOptions"">The encode options, used to customize the encoding of request payloads.</param>
[System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute]
public {clientImplementationName}(
IceRpc.IInvoker invoker,
IceRpc.ServiceAddress? serviceAddress = null,
Expand All @@ -145,6 +146,7 @@ internal static string GenerateImplementation(ServiceDescriptor service)
/// <param name=""invoker"">The invocation pipeline of the proxy.</param>
/// <param name=""serviceAddressUri"">A URI that represents a service address.</param>
/// <param name=""encodeOptions"">The encode options, used to customize the encoding of request payloads.</param>
[System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute]
public {clientImplementationName}(
IceRpc.IInvoker invoker,
System.Uri serviceAddressUri,
Expand All @@ -153,6 +155,11 @@ internal static string GenerateImplementation(ServiceDescriptor service)
{{
}}
/// <summary>Constructs a client with an icerpc service address with path <see cref=""DefaultServicePath"" />.</summary>
public {clientImplementationName}()
{{
}}
{methods.Trim()}
}}";
return clientImplementation;
Expand Down

0 comments on commit 6937105

Please sign in to comment.