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 BlockingStubTest() { // TODO: Should use a mock instead. var serviceImpl = new TestBlockingSimpleServiceImpl(); var binder = new TestMethodBinder(); CreateSimpleServiceStub <IBlockingService>(serviceImpl, binder); var callContext = CreateServerCallContext(CancellationToken.None); var objectId = RpcObjectId.NewId(); var setMethod = binder.methods.FirstOrDefault(p => p.Item1.Name == "SetValue"); Assert.NotNull(setMethod); var setValueHandler = (UnaryServerMethod <RpcObjectRequest <double>, RpcResponse>)setMethod.Item2; await setValueHandler(new RpcObjectRequest <double>(objectId, 123.456), callContext); //await (Task<RpcResponse>)setMethod.Invoke(serviceStub, new object[] { new RpcObjectRequest<double>(objectId, 123.456), callContext }); Assert.AreEqual(1, serviceImpl.nBlockingSetValue); var getMethod = binder.methods.FirstOrDefault(p => p.Item1.Name == "GetValue"); Assert.NotNull(getMethod); var getValueHandler = (UnaryServerMethod <RpcObjectRequest, RpcResponse <double> >)getMethod.Item2; var getResponse = await getValueHandler(new RpcObjectRequest(objectId), callContext); Assert.AreEqual(1, serviceImpl.nBlockingGetValue); Assert.AreEqual(123.456, getResponse.Result); var addMethod = binder.methods.FirstOrDefault(p => p.Item1.Name == "Add"); var addHandler = (UnaryServerMethod <RpcObjectRequest <int, int>, RpcResponse <int> >)addMethod.Item2; var addResponse = await addHandler(new RpcObjectRequest <int, int>(objectId, 8, 9), callContext); Assert.AreEqual(1, serviceImpl.nBlockingAdd); Assert.AreEqual(17, addResponse.Result); }
public async Task SimpleServiceServerTest() { Pipe requestPipe = new Pipe(); Pipe responsePipe = new Pipe(); var serializer = new ProtobufRpcSerializer(); var serviceImpl = new TestBlockingSimpleServiceImpl(); var hostMock = new Mock <IRpcServerCore>(); var serviceImplProviderMock = new Mock <IRpcServiceActivator>(); serviceImplProviderMock.Setup(p => p.GetActivatedService <ISimpleService>(It.IsAny <IServiceProvider>(), It.IsAny <RpcObjectId>())).Returns(new ActivatedService <ISimpleService>(serviceImpl, null)); hostMock.Setup(p => p.ServiceActivator).Returns(serviceImplProviderMock.Object); hostMock.Setup(p => p.CallInterceptors).Returns(ImmutableArrayList <RpcServerCallInterceptor> .Empty); var serviceRegistrator = new RpcServiceDefinitionsBuilder(); serviceRegistrator.RegisterService <ISimpleService>(); _ = RpcServerId.NewId(); using (var host = new LightweightRpcServer(Mock.Of <IRpcServicePublisher>(), serviceImplProviderMock.Object, serviceRegistrator, null, new RpcServerOptions { Serializer = serializer })) { host.AddEndPoint(new InprocRpcEndPoint(new DirectDuplexPipe(requestPipe.Reader, responsePipe.Writer))); host.Start(); var objectId = RpcObjectId.NewId(); var requestFrame = new LightweightRpcFrame(RpcFrameType.UnaryRequest, 1, "SciTech.Rpc.Tests.SimpleService.Add", ImmutableArray <KeyValuePair <string, ImmutableArray <byte> > > .Empty); using (var frameWriter = new BufferWriterStreamImpl()) { var writeState = requestFrame.BeginWrite(frameWriter); var request = new RpcObjectRequest <int, int>(objectId, 5, 6); serializer.Serialize(frameWriter, request, request.GetType()); int frameLength = checked ((int)frameWriter.Length); LightweightRpcFrame.EndWrite(frameLength, writeState); frameWriter.CopyTo(requestPipe.Writer); } await requestPipe.Writer.FlushAsync(); RpcResponse <int> response = null; while (response == null) { var readResult = await responsePipe.Reader.ReadAsync(); if (!readResult.IsCanceled) { var buffer = readResult.Buffer; if (LightweightRpcFrame.TryRead(ref buffer, LightweightRpcFrame.DefaultMaxFrameLength, out var responseFrame) == RpcFrameState.Full) { Assert.AreEqual(requestFrame.RpcOperation, responseFrame.RpcOperation); Assert.AreEqual(requestFrame.MessageNumber, responseFrame.MessageNumber); response = (RpcResponse <int>)serializer.Deserialize(responseFrame.Payload, typeof(RpcResponse <int>)); responsePipe.Reader.AdvanceTo(buffer.Start); } else { if (readResult.IsCompleted) { break; } responsePipe.Reader.AdvanceTo(buffer.Start, buffer.End); } }