Exemplo n.º 1
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);
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 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);
        }
        public Task <TResponse> CallMethodAsync <TRequest, TResponse>(object method, TRequest request)
            where TRequest : class
            where TResponse : class
        {
            var lwMethod = (LightweightMethodStub)method;

            return(LightweightStubHelper.SendReceiveAsync <TRequest, TResponse>(lwMethod, request, this.serializer));
        }
        public async Task Multicast_Singletons_ShouldReturn_Published()
        {
            var server         = new LightweightRpcServer();
            var connectionInfo = new RpcConnectionInfo(new Uri("test://test"));
            var endPoint       = new LightweightDiscoveryEndPoint(connectionInfo);

            server.AddEndPoint(endPoint);

            server.PublishSingleton <ISimpleService>();
            server.PublishSingleton <ISimpleService2>();

            server.Start();
            try
            {
                using var client = new UdpClient(AddressFamily.InterNetwork);
                client.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
                client.JoinMulticastGroup(LightweightDiscoveryEndPoint.DefaultMulticastAddress);

                // Receive early
                var receiveTask = client.ReceiveAsync();

                // Build request
                byte[] requestData = LightweightStubHelper.GetRequestData(ServiceDiscoveryOperations.GetPublishedSingletons, 0, new RpcRequest(), this.serializer);

                // Send it
                var discoveryEp = new System.Net.IPEndPoint(LightweightDiscoveryEndPoint.DefaultMulticastAddress, LightweightDiscoveryEndPoint.DefaultDiscoveryPort);
                var bytesSent   = await client.SendAsync(requestData, requestData.Length, discoveryEp);

                Assert.AreEqual(requestData.Length, bytesSent);

                // Wait for response
                var receiveRes = await receiveTask.DefaultTimeout();

                var response = LightweightStubHelper.GetResponseFromData <RpcPublishedSingletonsResponse>(serializer, receiveRes.Buffer);
                Assert.NotNull(response);

                // And check it
                Assert.NotNull(response);
                Assert.AreEqual(2, response.Services.Length);
            }
            finally
            {
                await server.ShutdownAsync();
            }
        }