public (IRpcServer, RpcConnection) CreateServerAndConnection( RpcServiceDefinitionsBuilder serviceDefinitionsBuilder, Action <RpcServerOptions> configServerOptions = null, Action <RpcClientOptions> configClientOptions = null) { throw new NotImplementedException(); }
public async Task DirectServiceProviderServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <IServiceProviderService>() .RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(serverBuilder); var servicePublisher = host.ServicePublisher; _ = servicePublisher.ServerId; host.Start(); try { var serviceImpl = new ServiceProviderServiceImpl(host.ServicePublisher); using (servicePublisher.PublishSingleton <IServiceProviderService>(serviceImpl)) { var clientService = connection.GetServiceSingleton <IServiceProviderServiceClient>(); var serviceRef = await clientService.GetSimpleServiceAsync(); var simpleService = connection.GetServiceInstance(serviceRef); int res = await simpleService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task UnavailableService_ShouldThrowRpcServiceUnavailableException(TestOperationType operationType) { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IBlockingService>() .RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); using (var publishedInstanceScope = host.ServicePublisher.PublishInstance <IBlockingService>(new TestBlockingServiceImpl())) { var objectId = RpcObjectId.NewId(); var blockingClientService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); var simpleService = connection.GetServiceInstance <ISimpleService>(objectId); host.Start(); try { // Invoke unknown service instance switch (operationType) { case TestOperationType.BlockingBlocking: Assert.Throws <RpcServiceUnavailableException>(() => blockingClientService.Add(12, 13)); break; case TestOperationType.AsyncBlocking: // Async/blocking (client/host) Assert.ThrowsAsync <RpcServiceUnavailableException>(() => blockingClientService.AddAsync(12, 13)); break; case TestOperationType.BlockingBlockingVoid: Assert.Throws <RpcServiceUnavailableException>(() => blockingClientService.Value = 23); break; case TestOperationType.AsyncBlockingVoid: // Async/blocking void (client/host) Assert.ThrowsAsync <RpcServiceUnavailableException>(() => blockingClientService.SetValueAsync(13)); break; case TestOperationType.AsyncAsync: // Async/async (client/host) Assert.ThrowsAsync <RpcServiceUnavailableException>(() => simpleService.AddAsync(12, 13)); break; case TestOperationType.AsyncAsyncVoid: // Async/async void (client/host) Assert.ThrowsAsync <RpcServiceUnavailableException>(() => simpleService.SetValueAsync(12)); break; } var exisingService = connection.GetServiceInstance <IBlockingServiceClient>(publishedInstanceScope.Value); // Make sure that the connection is still usable after exception Assert.AreEqual(12 + 13, exisingService.Add(12, 13)); } finally { await host.ShutdownAsync(); } } }
public void TooLargeServerMessage_ShouldThrowException() { var definitionBuilder = new RpcServiceDefinitionsBuilder(); definitionBuilder.RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(definitionBuilder, options => { options.SendMaxMessageSize = 10000; }); host.Start(); try { using (host.ServicePublisher.PublishSingleton <ISimpleService>(new TestSimpleServiceImpl())) { var simpleService = connection.GetServiceSingleton <ISimpleService>(); // TODO: Shouldn't be RpcFailureException. Assert.ThrowsAsync <RpcFailureException>(() => simpleService.GetArrayAsync(10000).DefaultTimeout()); // Verify that service is still available. int res = simpleService.AddAsync(5, 6).Result; Assert.AreEqual(5 + 6, res); } } finally { host.ShutdownAsync(); } }
public async Task CustomExceptionConverterTest() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IDeclaredFaultsService>(); //.RegisterExceptionConverter(new DeclaredFaultExceptionConverter()); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator, so => so.ExceptionConverters.Add(new DeclaredFaultExceptionConverter()), co => co.ExceptionConverters.Add(new DeclaredFaultExceptionConverter()) ); var publishedInstanceScope = host.ServicePublisher.PublishInstance <IDeclaredFaultsService>(new FaultServiceImpl()); var faultService = connection.GetServiceInstance <IDeclaredFaultsService>(publishedInstanceScope.Value); host.Start(); try { var faultException = Assert.Throws <DeclaredFaultException>(() => faultService.GenerateCustomDeclaredExceptionAsync()); Assert.IsNotEmpty(faultException.Message); Assert.IsNotEmpty(faultException.DetailedMessage); } finally { await host.ShutdownAsync(); } }
public async Task CallbackMethod_Should_InvokeAction() { var builder = new RpcServiceDefinitionsBuilder(); builder.RegisterService <ICallbackService>(); var(server, connection) = this.CreateServerAndConnection(builder); var callbackServiceImpl = new CallbackServiceImpl(); using (var publishedService = server.PublishSingleton <ICallbackService>(callbackServiceImpl)) { try { server.Start(); var callbackService = connection.GetServiceSingleton <ICallbackService>(); int callCount = 0; await callbackService.PerformActionCallbacksAsync(2, 10, v => { Assert.AreEqual(2 + callCount, v.Value); callCount++; }); Assert.AreEqual(callCount, 10); } finally { await server.ShutdownAsync(); } } }
public async Task UndeclaredException_ShouldThrowRpcFailure(TestOperationType operationType) { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IDeclaredFaultsService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); var publishedInstanceScope = host.ServicePublisher.PublishInstance <IDeclaredFaultsService>(new FaultServiceImpl()); var faultService = connection.GetServiceInstance <IFaultServiceClient>(publishedInstanceScope.Value); host.Start(); try { switch (operationType) { case TestOperationType.AsyncAsyncVoid: Assert.ThrowsAsync <RpcFailureException>(() => faultService.GenerateUndeclaredAsyncExceptionAsync(false).DefaultTimeout()); break; case TestOperationType.AsyncAsync: Assert.ThrowsAsync <RpcFailureException>(() => faultService.GenerateUndeclaredAsyncExceptionWithReturnAsync(false).DefaultTimeout()); break; case TestOperationType.BlockingAsyncVoid: Assert.Throws <RpcFailureException>(() => faultService.GenerateUndeclaredAsyncException(false)); break; case TestOperationType.BlockingAsync: Assert.Throws <RpcFailureException>(() => faultService.GenerateUndeclaredAsyncExceptionWithReturn(false)); break; case TestOperationType.AsyncBlockingVoid: Assert.ThrowsAsync <RpcFailureException>(() => faultService.GenerateUndeclaredExceptionAsync().DefaultTimeout()); break; case TestOperationType.AsyncBlocking: Assert.ThrowsAsync <RpcFailureException>(() => faultService.GenerateUndeclaredExceptionWithReturnAsync().DefaultTimeout()); break; case TestOperationType.BlockingBlockingVoid: Assert.Throws <RpcFailureException>(() => faultService.GenerateUndeclaredException()); break; case TestOperationType.BlockingBlocking: Assert.Throws <RpcFailureException>(() => faultService.GenerateUndeclaredExceptionWithReturn()); break; } // TODO: Should this be allowed, or should the connection be closed on undeclared exceptions? int res = faultService.Add(5, 6); Assert.AreEqual(5 + 6, res); } finally { await host.ShutdownAsync(); } }
public async Task NoDetailsDeclaredFaultTest() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IDeclaredFaultsService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); try { host.Start(); using (var publishedInstanceScope = host.ServicePublisher.PublishInstance <IDeclaredFaultsService>(new FaultServiceImpl())) { var faultService = connection.GetServiceInstance <IFaultServiceClient>(publishedInstanceScope.Value); var faultException = Assert.Throws <RpcFaultException>(() => faultService.GenerateNoDetailsFault()); Assert.AreEqual("NoDetailsFault", faultException.FaultCode); // Make sure that the connection is still usable after exception Assert.AreEqual(12 + 13, faultService.Add(12, 13)); } } finally { await host.ShutdownAsync(); } }
public async Task SequenceEnumerableTest() { var builder = new RpcServiceDefinitionsBuilder(); builder.RegisterService <ISequenceService>(); var(server, connection) = this.CreateServerAndConnection(builder); var sequenceServiceImpl = new SequenceServiceImpl(); using (var publishedService = server.ServicePublisher.PublishSingleton <ISequenceService>(sequenceServiceImpl)) { try { server.Start(); var sequenceService = connection.GetServiceSingleton <ISequenceService>(); int lastNo = 0; await foreach (var sequenceData in sequenceService.GetSequenceAsEnumerable(5, TimeSpan.FromMilliseconds(0), 0)) { Assert.AreEqual(lastNo + 1, sequenceData.SequenceNo); lastNo = sequenceData.SequenceNo; } Assert.AreEqual(5, lastNo); } finally { await server.ShutdownAsync(); } } }
public async Task TooSlowExecution_ShouldThrowException() { var definitionBuilder = new RpcServiceDefinitionsBuilder(); definitionBuilder.RegisterService <ITimeoutTestService>(); var(host, connection) = this.CreateServerAndConnection(definitionBuilder, configClientOptions: options => { options.CallTimeout = TimeSpan.FromMilliseconds(300); }); host.Start(); try { using (var publishedInstanceScope = host.ServicePublisher.PublishInstance <ITimeoutTestService>(new TestTimeoutServiceImpl())) { var timeoutService = connection.GetServiceInstance <ITimeoutTestServiceClient>(publishedInstanceScope.Value); Assert.ThrowsAsync <TimeoutException>(() => timeoutService.AsyncAddWithDelayAsync(3, 4, TimeSpan.FromMilliseconds(400), default).DefaultTimeout()); Assert.ThrowsAsync <TimeoutException>(() => timeoutService.AddWithDelayAsync(3, 4, TimeSpan.FromMilliseconds(400)).DefaultTimeout()); Assert.Throws <TimeoutException>(() => timeoutService.AddWithDelay(3, 4, TimeSpan.FromMilliseconds(400))); Assert.Throws <TimeoutException>(() => timeoutService.AsyncAddWithDelay(3, 4, TimeSpan.FromMilliseconds(400))); } } finally { await host.ShutdownAsync(); } }
public async Task DeviceServiceTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <IThermostatService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new ThermostatServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; GrpcConnection connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <IThermostatServiceClient>(objectId); var acoId = clientService.DeviceAcoId; var baseClientService = (IDeviceServiceClient)clientService; var acoId2 = baseClientService.DeviceAcoId; Assert.AreEqual(acoId, acoId2); } } finally { await host.ShutdownAsync(); } }
public async Task SingletonServiceTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(serverBuilder); var servicePublisher = host.ServicePublisher; host.Start(); try { var serviceImpl = new TestSimpleServiceImpl(); using (servicePublisher.PublishSingleton <ISimpleService>(serviceImpl)) { var clientService = connection.GetServiceSingleton <ISimpleService>(); int res = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task SimpleObjectServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestSimpleServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <ISimpleService>(objectId); int res = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task ServiceProviderServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <IServiceProviderService>() .RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new ServiceProviderServiceImpl(host.ServicePublisher); using (var publishScope = host.PublishSingleton <IServiceProviderService>(serviceImpl)) { var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceSingleton <IServiceProviderServiceClient>(); var serviceRef = await clientService.GetSimpleServiceAsync(); var simpleService = connection.GetServiceInstance(serviceRef); int res = await simpleService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task SequenceEnumerableTimeoutTest() { var builder = new RpcServiceDefinitionsBuilder(); builder.RegisterService <ISequenceService>(); var(server, connection) = this.CreateServerAndConnection(builder, null, o => { o.StreamingCallTimeout = TimeSpan.FromSeconds(1); }); var sequenceServiceImpl = new SequenceServiceImpl(); using var publishedService = server.ServicePublisher.PublishSingleton <ISequenceService>(sequenceServiceImpl); try { server.Start(); var sequenceService = connection.GetServiceSingleton <ISequenceService>(); var exceptionExpression = Is.InstanceOf <TimeoutException>(); if (this.ConnectionType == RpcConnectionType.NetGrpc) { // It seems like NetGrpc throws OperationCancelledException instead of DeadlineExceeded RpcException (but // it also seems a bit timing dependent). Let's allow both exceptions. exceptionExpression = exceptionExpression.Or.InstanceOf <OperationCanceledException>(); } DateTime connectStart = DateTime.UtcNow; // Force connection // sequenceService.SimpleCall(); var connectDuration = DateTime.UtcNow - connectStart; Console.WriteLine("Connect duration: " + connectDuration); DateTime start = DateTime.UtcNow; int lastNo = 0; Assert.ThrowsAsync(exceptionExpression, async() => { async Task GetSequence() { await foreach (var sequenceData in sequenceService.GetSequenceAsEnumerable(2000, TimeSpan.FromMilliseconds(1), 1)) { Assert.AreEqual(lastNo + 1, sequenceData.SequenceNo); lastNo = sequenceData.SequenceNo; } } await GetSequence().DefaultTimeout(); }); TimeSpan duration = DateTime.UtcNow - start; Assert.GreaterOrEqual(duration, TimeSpan.FromSeconds(1)); Assert.Less(duration, TimeSpan.FromSeconds(2)); Assert.Greater(lastNo, 10); }
public async Task EventHandlersTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleServiceWithEvents>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestServiceWithEventsImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <ISimpleServiceWithEvents>(objectId); TaskCompletionSource <ValueChangedEventArgs> detailedTcs = new TaskCompletionSource <ValueChangedEventArgs>(); EventHandler <ValueChangedEventArgs> detailedHandler = (s, e) => { detailedTcs.SetResult(e); }; clientService.DetailedValueChanged += detailedHandler; await((IRpcProxy)clientService).WaitForPendingEventHandlersAsync(); clientService.SetValueAsync(12).Forget(); var completedTask = await Task.WhenAny(detailedTcs.Task, Task.Delay(1000)); Assert.AreEqual(detailedTcs.Task, completedTask); Assert.IsTrue(completedTask.IsCompletedSuccessfully()); var detailedArgs = detailedTcs.Task.Result; clientService.DetailedValueChanged -= detailedHandler; await((IRpcProxy)clientService).WaitForPendingEventHandlersAsync(); clientService.SetValueAsync(13).Forget(); await Task.Delay(200); Assert.IsFalse(serviceImpl.HasDetailedValueChangedHandler); Assert.IsFalse(serviceImpl.HasValueChangedHandler); Assert.AreEqual(12, detailedArgs.NewValue); Assert.AreEqual(0, detailedArgs.OldValue); } } finally { await host.ShutdownAsync(); } }
public void IncorrectServiceFault_ShouldThrowDefinitionException() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); var(_, connection) = this.CreateServerAndConnection(serviceRegistrator); Assert.Throws <RpcDefinitionException>(() => connection.GetServiceInstance <IIncorrectServiceFaultServiceClient>(RpcObjectId.NewId())); }
public async Task CallbackWithCancellationTest() { var builder = new RpcServiceDefinitionsBuilder(); builder.RegisterService <ICallbackService>(); var(server, connection) = this.CreateServerAndConnection(builder); var callbackServiceImpl = new CallbackServiceImpl(); using (var publishedService = server.ServicePublisher.PublishSingleton <ICallbackService>(callbackServiceImpl)) { try { server.Start(); var callbackService = connection.GetServiceSingleton <ICallbackService>(); int lastNo = 0; CancellationTokenSource cts = new CancellationTokenSource(); Assert.CatchAsync <OperationCanceledException>(async() => { await callbackService.PerformCancellableCallbacksAsync(1000, TimeSpan.FromMilliseconds(20), 10, true, callbackData => { Assert.AreEqual(lastNo + 1, callbackData.Value); lastNo = callbackData.Value; if (lastNo == 3) { cts.Cancel(); } }, cts.Token); }); if (this.RoundTripCancellation) { Assert.Less(lastNo, 100); Assert.GreaterOrEqual(lastNo, 3); } else { Assert.AreEqual(3, lastNo); } // // Assert.IsTrue(await sequenceServiceImpl.GetIsCancelledAsync().DefaultTimeout()); } finally { await server.ShutdownAsync(); } } }
public static async Task Main(string[] args) { // This example shows how to explicitly setup a gRPC RPC server and a lightweight RPC server // with a common service publisher. // // In a real scenario, it is probably more suitable to use the .NET generic host // (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host). // // The NetGrpcServer example shows what a host setup could look like. Console.WriteLine("Initializing gRPC server and lightweight RPC server."); var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); var definitionsBuilder = new RpcServiceDefinitionsBuilder(); var rpcPublisher = new RpcServicePublisher(definitionsBuilder); RegisterServiceDefinitions(definitionsBuilder); PublishServices(rpcPublisher); var grpcServer = new GrpcServer(rpcPublisher, serviceProvider); grpcServer.AddEndPoint(CreateGrpcEndPoint(50051)); var lightweightServer = new LightweightRpcServer(rpcPublisher, serviceProvider); var sslOptions = new SslServerOptions(new X509Certificate2(TestCertificates.ServerPFXPath, "1111")); lightweightServer.AddEndPoint( new TcpRpcEndPoint("127.0.0.1", 50052, false, sslOptions)); Console.WriteLine("Starting gRPC server and lightweight RPC server."); // Once the gRPC server is started, it is no longer possible to register service definitions. grpcServer.Start(); lightweightServer.Start(); Console.WriteLine("gRPC server and lightweight RPC server are running, press any key to shutdown."); Console.ReadKey(); Console.WriteLine("Shutting down servers."); await Task.WhenAll(grpcServer.ShutdownAsync(), lightweightServer.ShutdownAsync()); Console.WriteLine("Ended"); }
public void GlobalSetup() { var serverId = RpcServerId.NewId(); var definitionsProvider = new RpcServiceDefinitionsBuilder(); // var serializer = new JsonRpcSerializer(); var serializer = new ProtobufRpcSerializer(RuntimeTypeModel.Create()); var serverOptions = new RpcServerOptions { Serializer = serializer }; this.clientOptions = new RpcClientOptions { Serializer = serializer }.AsImmutable(); switch (this.ConnectionType) { //case RpcConnectionType.LightweightInproc: // { // var connector = new InprocRpcConnector(clientOptions); // this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions); // this.server.AddEndPoint(connector.EndPoint); // this.server.ServicePublisher.PublishSingleton<ISimpleService>(new SimpleServiceImpl()); // this.server.Start(); // //this.clientConnection = connector.Connection; // //clientService = this.clientConnection.GetServiceSingleton<ISimpleServiceClient>(); // break; // } case RpcConnectionType.LightweightTcp: { this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions); this.server.AddEndPoint(new TcpRpcEndPoint("127.0.0.1", 50051, false)); this.server.ServicePublisher.PublishSingleton <ISimpleService>(new SimpleServiceImpl()); this.server.Start(); this.connectionInfo = new RpcConnectionInfo(new Uri("lightweight.tcp://localhost:50051")); break; } case RpcConnectionType.LightweightNamedPipe: { this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions); this.server.AddEndPoint(new NamedPipeRpcEndPoint("RpcBenchmark")); this.server.ServicePublisher.PublishSingleton <ISimpleService>(new SimpleServiceImpl()); this.server.Start(); this.connectionInfo = new RpcConnectionInfo(new Uri($"{WellKnownRpcSchemes.LightweightPipe}://./RpcBenchmark")); break; } } }
protected async Task TestLargeResponse( ExpectedRpcSizeException expectedException, int sizeParameter, Action <RpcServerOptions> serverConfig = null, Action <RpcClientOptions> clientConfig = null) { var definitionBuilder = new RpcServiceDefinitionsBuilder(); definitionBuilder.RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(definitionBuilder, configServerOptions: serverConfig, configClientOptions: clientConfig); var servicePublisher = host.ServicePublisher; using (servicePublisher.PublishSingleton <ISimpleService>(new TestSimpleServiceImpl())) { var serviceClient = connection.GetServiceSingleton <ISimpleService>(); host.Start(); try { Task <int[]> RequestFunc() => serviceClient.GetArrayAsync(sizeParameter).DefaultTimeout(); switch (expectedException) { case ExpectedRpcSizeException.Communication: Assert.ThrowsAsync <RpcCommunicationException>(() => RequestFunc()); break; case ExpectedRpcSizeException.Failure: var e = Assert.ThrowsAsync <RpcFailureException>(() => RequestFunc()); Assert.AreEqual(RpcFailure.SizeLimitExceeded, e.Failure); break; case ExpectedRpcSizeException.None: Assert.AreEqual(sizeParameter, (await RequestFunc()).Length); break; } Assert.AreEqual(5, (await serviceClient.GetArrayAsync(5).DefaultTimeout()).Length); } finally { await host.ShutdownAsync().DefaultTimeout(); } } }
public void HostNotFoundTest() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IBlockingService>();; var(_, connection) = this.CreateServerAndConnection(serviceRegistrator); var objectId = RpcObjectId.NewId(); var clientService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); // Invoke client operation without starting host Assert.Throws <RpcCommunicationException>(() => clientService.Add(12, 13)); }
public async Task BlockingServiceCallTest() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IBlockingService>() .RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); var servicePublisher = host.ServicePublisher; //RpcServerId rpcServerId = servicePublisher.HostId; host.Start(); try { var serviceImpl = new TestBlockingSimpleServiceImpl(); using (var publishScope = servicePublisher.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var clientService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); int blockingRes = clientService.Add(12, 13); Assert.AreEqual(12 + 13, blockingRes); int asyncRes = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, asyncRes); clientService.Value = 123.45; Assert.AreEqual(123.45, await clientService.GetValueAsync()); await clientService.SetValueAsync(543.21).ConfigureAwait(false); Assert.AreEqual(543.21, clientService.Value); await connection.ShutdownAsync(); } } finally { await host.ShutdownAsync(); } }
public async Task BlockingServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder .RegisterService <IBlockingService>() .RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestBlockingSimpleServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); int blockingRes = clientService.Add(12, 13); Assert.AreEqual(12 + 13, blockingRes); int asyncRes = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, asyncRes); clientService.Value = 123.45; Assert.AreEqual(123.45, await clientService.GetValueAsync()); await clientService.SetValueAsync(543.21); Assert.AreEqual(543.21, clientService.Value); } } finally { await host.ShutdownAsync(); } }
public async Task TooLargeClientMessage_ShouldThrowException() { var definitionBuilder = new RpcServiceDefinitionsBuilder(); definitionBuilder.RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(definitionBuilder, options => { options.ReceiveMaxMessageSize = 10000; }); host.Start(); try { using (var publishedInstanceScope = host.ServicePublisher.PublishInstance <ISimpleService>(new TestSimpleServiceImpl())) { var simpleService = connection.GetServiceInstance(publishedInstanceScope.Value); int[] data = new int[10000]; for (int i = 0; i < data.Length; i++) { data[i] = i; } // TODO: Lightweight will throw RpcCommunicationException (due to disconnect) and // gRPC will throw RpcFailureException. What's the correct exception? var sumTask = simpleService.SumAsync(data).DefaultTimeout(); Assert.ThrowsAsync(Is.TypeOf <RpcFailureException>().Or.TypeOf <RpcCommunicationException>(), () => sumTask); // TODO: gRPC keeps the connection alive. Should the Lightweight connection // also be kept alive (far from trivial)? if (this.ConnectionType == RpcConnectionType.Grpc) { // Verify that service is still available. int res = simpleService.AddAsync(5, 6).AwaiterResult(); Assert.AreEqual(5 + 6, res); } } } finally { await host.ShutdownAsync(); } }
public void GlobalSetup() { var definitionsProvider = new RpcServiceDefinitionsBuilder(); switch (this.ConnectionType) { case RpcConnectionType.Grpc: this.server = new GrpcCore.Server { Services = { SimpleService.BindService(new GrpcSimpleService()) }, Ports = { new GrpcCore.ServerPort("localhost", 50051, GrpcCore.ServerCredentials.Insecure) } }; this.server.Start(); this.channel = new GrpcCore.Channel("127.0.0.1:50051", GrpcCore.ChannelCredentials.Insecure); this.clientService = new SimpleService.SimpleServiceClient(this.channel); break; #if COREFX case RpcConnectionType.NetGrpc: this.host = CreateNetGrpcHost(); host.Start(); var handler = new System.Net.Http.HttpClientHandler(); handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => { return(true); }; var channelOptions = new GrpcNet.Client.GrpcChannelOptions() { HttpClient = new System.Net.Http.HttpClient(handler), DisposeHttpClient = true }; this.channel = GrpcChannel.ForAddress("https://localhost:50051", channelOptions); this.clientService = new SimpleService.SimpleServiceClient(channel); break; #endif } }
public async Task MultiSingletonServicesTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleService>(); serverBuilder.RegisterService <IBlockingService>(); var(host, connection) = this.CreateServerAndConnection(serverBuilder); var servicePublisher = host.ServicePublisher; host.Start(); try { var simpleServiceImpl = new TestSimpleServiceImpl(); var blockingServiceImpl = new TestBlockingServiceImpl(); using (servicePublisher.PublishSingleton <ISimpleService>(simpleServiceImpl)) { using (servicePublisher.PublishSingleton <IBlockingService>(blockingServiceImpl)) { var clientService = connection.GetServiceSingleton <ISimpleService>(); _ = connection.GetServiceSingleton <IBlockingService>(); var clientService2 = connection.GetServiceSingleton <ISimpleService>(); Assert.AreSame(clientService, clientService2); int res = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); int res2 = await clientService.AddAsync(12, 13); Assert.AreEqual(12 + 13, res2); } } } finally { await host.ShutdownAsync(); } }
public async Task FastExecution_ShouldReturn() { var definitionBuilder = new RpcServiceDefinitionsBuilder(); definitionBuilder.RegisterService <ITimeoutTestService>(); var(host, connection) = this.CreateServerAndConnection(definitionBuilder, configClientOptions: options => { options.CallTimeout = TimeSpan.FromMilliseconds(400); }); host.Start(); try { using (var publishedInstanceScope = host.ServicePublisher.PublishInstance <ITimeoutTestService>(new TestTimeoutServiceImpl())) { var timeoutService = connection.GetServiceInstance <ITimeoutTestServiceClient>(publishedInstanceScope.Value); var res = await timeoutService.AddWithDelayAsync(3, 4, TimeSpan.FromMilliseconds(50)).DefaultTimeout(); Assert.AreEqual(3 + 4, res); res = await timeoutService.AsyncAddWithDelayAsync(5, 6, TimeSpan.FromMilliseconds(50), default).DefaultTimeout(); Assert.AreEqual(5 + 6, res); res = timeoutService.AsyncAddWithDelay(7, 8, TimeSpan.FromMilliseconds(50)); Assert.AreEqual(7 + 8, res); res = timeoutService.AddWithDelay(9, 10, TimeSpan.FromMilliseconds(50)); Assert.AreEqual(9 + 10, res); } } finally { await host.ShutdownAsync(); } }
public async Task ServiceFaultConverter_ShouldConvert() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IMathFaultsService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); var publishedInstanceScope = host.ServicePublisher.PublishInstance <IMathFaultsService>(new MathFaultsServiceImpl()); var faultService = connection.GetServiceInstance(publishedInstanceScope.Value); host.Start(); try { Assert.Throws <MathException>(() => faultService.Sqrt(-5)); } finally { await host.ShutdownAsync(); } }
public async Task MultiInstanceServicesTest() { var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator .RegisterService <IBlockingService>() .RegisterService <ISimpleService>(); var(host, connection) = this.CreateServerAndConnection(serviceRegistrator); var servicePublisher = host.ServicePublisher; //RpcServerId rpcServerId = servicePublisher.HostId; host.Start(); try { var serviceImpl = new TestBlockingSimpleServiceImpl(); using (var publishScope = servicePublisher.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var blockingService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); var simpleService = connection.GetServiceInstance <ISimpleService>(objectId); // Service proxies should be equal, but not necessarily the same Assert.AreEqual(blockingService, simpleService); var blockingService2 = connection.GetServiceInstance <IBlockingServiceClient>(objectId); // Service proxies for the object and service should be the same Assert.AreSame(blockingService, blockingService2); await connection.ShutdownAsync().DefaultTimeout(); } } finally { await host.ShutdownAsync().DefaultTimeout(); } }