Example #1
0
        public void FailUnpublishedServiceProviderStubTest()
        {
            var binder = new TestLightweightMethodBinder();
            var definitionsProviderMock = new Mock <IRpcServiceDefinitionsProvider>(MockBehavior.Strict);

            definitionsProviderMock.Setup(p => p.IsServiceRegistered(It.IsAny <Type>())).Returns(true);
            definitionsProviderMock.Setup(p => p.GetServiceOptions(It.IsAny <Type>())).Returns((RpcServerOptions)null);

            RpcServicePublisher servicePublisher = new RpcServicePublisher(definitionsProviderMock.Object);
            var serviceImpl = new AutoPublishServiceProviderServiceImpl();

            var publishedServiceScope = servicePublisher.PublishInstance(serviceImpl);

            CreateSimpleServiceStub <IImplicitServiceProviderService>(servicePublisher, binder, false);

            LightweightMethodStub getServiceStub = binder.GetHandler <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >(
                "SciTech.Rpc.Tests.ImplicitServiceProviderService.GetSimpleService");

            Assert.NotNull(getServiceStub);

            var objectId = publishedServiceScope.Value.ObjectId;

            Assert.ThrowsAsync <RpcFailureException>(async() =>
                                                     await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <int>, RpcResponse <RpcObjectRef <ISimpleService> > >(
                                                         getServiceStub, new RpcObjectRequest <int>(objectId, 1), DefaultSerializer));
        }
Example #2
0
        public async Task GenerateSimpleServiceStubTest()
        {
            var binder = new TestLightweightMethodBinder();

            CreateSimpleServiceStub <ISimpleService>(new TestSimpleServiceImpl(), binder);

            LightweightMethodStub addStub = binder.GetHandler <RpcObjectRequest <int, int>, RpcResponse <int> >("SciTech.Rpc.Tests.SimpleService.Add");

            Assert.NotNull(addStub);

            var objectId = RpcObjectId.NewId();
            var request  = new RpcObjectRequest <int, int>(objectId, 5, 6);
            RpcResponse <int> addResponse = await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <int, int>, RpcResponse <int> >(addStub, request, DefaultSerializer);

            Assert.AreEqual(11, addResponse.Result);

            LightweightMethodStub setStub = binder.GetHandler <RpcObjectRequest <double>, RpcResponse>("SciTech.Rpc.Tests.SimpleService.SetValue");

            Assert.NotNull(setStub);
            var setResponse = await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <double>, RpcResponse>(setStub, new RpcObjectRequest <double>(objectId, 20), DefaultSerializer);

            Assert.NotNull(setResponse);

            LightweightMethodStub getStub = binder.GetHandler <RpcObjectRequest, RpcResponse <double> >("SciTech.Rpc.Tests.SimpleService.GetValue");

            Assert.NotNull(getStub);
            var getResponse = await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest, RpcResponse <double> >(getStub, new RpcObjectRequest(objectId), DefaultSerializer);

            Assert.AreEqual(20, getResponse.Result);
        }
Example #3
0
        public async Task GenerateImplicitServiceProviderStubTest()
        {
            var binder = new TestLightweightMethodBinder();
            var definitionsProviderMock = new Mock <IRpcServiceDefinitionsProvider>(MockBehavior.Strict);

            definitionsProviderMock.Setup(p => p.IsServiceRegistered(It.IsAny <Type>())).Returns(true);
            definitionsProviderMock.Setup(p => p.GetServiceOptions(It.IsAny <Type>())).Returns((RpcServerOptions)null);

            RpcServicePublisher servicePublisher = new RpcServicePublisher(definitionsProviderMock.Object);
            var serviceImpl = new ImplicitServiceProviderServiceImpl(servicePublisher);

            var publishedServiceScope = servicePublisher.PublishInstance(serviceImpl);

            CreateSimpleServiceStub <IImplicitServiceProviderService>(servicePublisher, binder, false);

            var objectId = publishedServiceScope.Value.ObjectId;

            LightweightMethodStub getServiceStub = binder.GetHandler <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >(
                "SciTech.Rpc.Tests.ImplicitServiceProviderService.GetSimpleService");

            Assert.NotNull(getServiceStub);

            var getServiceResponse = await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >(
                getServiceStub, new RpcObjectRequest <int>(objectId, 1), DefaultSerializer);

            Assert.NotNull(getServiceResponse.Result);

            var actualServiceRef = servicePublisher.GetPublishedInstance(serviceImpl.GetSimpleService(1));

            Assert.AreEqual(actualServiceRef, getServiceResponse.Result);
        }
        internal void AddDiscoveryMethods(ILightweightMethodBinder binder)
        {
            var getPublishedSingletonsStub = new LightweightMethodStub <RpcDiscoveryRequest, RpcPublishedSingletonsResponse>(
                ServiceDiscoveryOperations.GetPublishedSingletons,
                (request, serviceProvider, context) => this.GetPublishedSingletonsAsync(request, serviceProvider, context),
                this.discoverySerializer, null, false);

            binder.AddMethod(getPublishedSingletonsStub);

            var getConnectionInfoStub = new LightweightMethodStub <RpcDiscoveryRequest, RpcConnectionInfoResponse>(
                ServiceDiscoveryOperations.GetConnectionInfo,
                (request, serviceProvider, context) => this.GetConnectionInfoAsync(request, context),
                this.discoverySerializer, null, false);

            binder.AddMethod(getConnectionInfoStub);
        }
        protected override void BuildServiceStubs()
        {
            var queryServicesStub = new LightweightMethodStub <RpcObjectRequest, RpcServicesQueryResponse>(
                "SciTech.Rpc.RpcService.QueryServices",
                (request, _, context) => new ValueTask <RpcServicesQueryResponse>(this.QueryServices(request.Id)),
                this.Serializer, null, false);

            this.AddMethodDef(queryServicesStub);

            if (this.AllowDiscovery)
            {
                var discovery = new ServiceDiscovery(this, this.CreateDefaultSerializer());
                discovery.AddDiscoveryMethods(new MethodBinder(this));
            }

            base.BuildServiceStubs();
        }
        protected override void AddGenericVoidBlockingMethodCore <TRequest>(
            Action <TService, TRequest, CancellationToken> serviceCaller,
            RpcServerFaultHandler faultHandler,
            RpcStub <TService> serviceStub,
            RpcOperationInfo operationInfo,
            ILightweightMethodBinder binder)
        {
            var serializer = operationInfo.SerializerOverride ?? serviceStub.Serializer;

            ValueTask <RpcResponse> HandleRequest(TRequest request, IServiceProvider?serviceProvider, LightweightCallContext context)
            => serviceStub.CallVoidBlockingMethod(request, serviceProvider, context, serviceCaller, faultHandler, serializer);

            var methodStub = new LightweightMethodStub <TRequest, RpcResponse>(operationInfo.FullName, HandleRequest, serializer, faultHandler,
                                                                               operationInfo.AllowInlineExecution);

            binder.AddMethod(methodStub);
        }
 public void AddMethod(LightweightMethodStub methodStub)
 {
     this.server.AddMethodDef(methodStub);
 }
        private void AddMethodDef(LightweightMethodStub methodStub)
        {
            this.Logger.LogInformation("Add service operation {Operation}.", methodStub.OperationName);

            this.methodDefinitions.Add(methodStub.OperationName, methodStub);
        }
 public void AddMethod(LightweightMethodStub methodStub)
 {
     this.methodStubs.Add(methodStub);
 }
Example #10
0
        internal static async Task <TResponse> SendReceiveAsync <TRequest, TResponse>(LightweightMethodStub methodStub, TRequest request, IRpcSerializer serializer)
            where TRequest : class
            where TResponse : class
        {
            TResponse response;

            var context      = new LightweightCallContext(new TestRpcEndPoint(), null, ImmutableArray <KeyValuePair <string, ImmutableArray <byte> > > .Empty, CancellationToken.None);
            var requestPipe  = new Pipe();
            var responsePipe = new Pipe();
            var duplexPipe   = new DirectDuplexPipe(requestPipe.Reader, responsePipe.Writer);

            await using (var pipeline = new TestPipeline(duplexPipe))
            {
                var payload = new ReadOnlySequence <byte>(serializer.Serialize(request));

                var frame = new LightweightRpcFrame(RpcFrameType.UnaryRequest, null, 1, methodStub.OperationName, RpcOperationFlags.None, 0, payload, null);

                await methodStub.HandleMessage(pipeline, frame, null, context);

                var readResult = await responsePipe.Reader.ReadAsync();

                var  buffer           = readResult.Buffer;
                bool hasResponseFrame = LightweightRpcFrame.TryRead(ref buffer, 65536, out var responseFrame) == RpcFrameState.Full;
                Assert.IsTrue(hasResponseFrame);

                response = (TResponse)serializer.Deserialize(responseFrame.Payload, typeof(TResponse));

                return(response);
            }
        }