コード例 #1
0
        private void TestGetServiceInvokerCore <TArg1, TArg2, TResult>(
            EventHandler <ServiceInvokedEventArgs <TResult> > invoked,
            RpcServerConfiguration configuration,
            TArg1 arg1,
            TArg2 arg2,
            Action <ServerResponseContext> assertion
            )
        {
            using (var target = new ServiceInvokerGenerator(true))
                using (var server = new RpcServer())
                    using (var transportManager = new NullServerTransportManager(server))
                        using (var transport = new NullServerTransport(transportManager))
                        {
                            var service = new Service <TArg1, TArg2, TResult>();
                            service.Invoked += invoked;

                            var serviceDescription = new ServiceDescription("Service", () => service);
                            var targetOperation    = service.GetType().GetMethod("Invoke");

                            using (var requestContext = new ServerRequestContext())
                            {
                                requestContext.ArgumentsBufferPacker = Packer.Create(requestContext.ArgumentsBuffer, false);
                                requestContext.ArgumentsBufferPacker.PackArrayHeader(2);
                                requestContext.ArgumentsBufferPacker.Pack(arg1);
                                requestContext.ArgumentsBufferPacker.Pack(arg2);
                                requestContext.ArgumentsBuffer.Position = 0;
                                requestContext.MessageId         = 123;
                                requestContext.ArgumentsUnpacker = Unpacker.Create(requestContext.ArgumentsBuffer, false);

                                var responseContext = new ServerResponseContext();
                                responseContext.SetTransport(transport);
                                try
                                {
                                    var result = target.GetServiceInvoker(RpcServerRuntime.Create(configuration, this._serializationContext), serviceDescription, targetOperation);

                                    result.InvokeAsync(requestContext, responseContext).Wait(TimeSpan.FromSeconds(1));

                                    assertion(responseContext);
                                }
                                finally
                                {
                                    if (this._isDumpEnabled)
                                    {
                                        try
                                        {
                                            target.Dump();
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.Error.WriteLine("Failed to dump: {0}", ex);
                                        }
                                    }
                                }
                            }
                        }
        }
コード例 #2
0
		private static void TestCore( Action<ServerRequestContext, ServerTransport> test )
		{
			using ( var server = new RpcServer() )
			using ( var manager = new NullServerTransportManager( server ) )
			using ( var transport = new NullServerTransport( manager ) )
			using ( var target = new ServerRequestContext() )
			{
				test( target, transport );
			}
		}
コード例 #3
0
 public void TestSetException_ExceptionIsNull()
 {
     using (var server = new RpcServer())
         using (var transportManager = new NullServerTransportManager(server))
             using (var transport = new NullServerTransport(transportManager))
                 using (var responseContext = DispatchTestHelper.CreateResponseContext(transport))
                 {
                     var target = new Target(server);
                     target.InvokeSetException(responseContext, "Method", null);
                 }
 }
コード例 #4
0
        public void TestDispatch_MethodExists_Success()
        {
            var svcFile = ".\\Services.svc";

            File.WriteAllText(
                svcFile,
                String.Format(CultureInfo.InvariantCulture, "<% @ ServiceHost Service=\"{0}\" %>", typeof(TestService).FullName)
                );
            try
            {
                var configuration = new RpcServerConfiguration();
                configuration.ServiceTypeLocatorProvider = conf => new FileBasedServiceTypeLocator();

                using (var server = new RpcServer(configuration))
                    using (var transportManager = new NullServerTransportManager(server))
                        using (var transport = new NullServerTransport(transportManager))
                            using (var requestContext = DispatchTestHelper.CreateRequestContext())
                                using (var argumentsBuffer = new MemoryStream())
                                    using (var waitHandle = new ManualResetEventSlim())
                                    {
                                        var message = Guid.NewGuid().ToString();
                                        using (var argumentsPacker = Packer.Create(argumentsBuffer, false))
                                        {
                                            argumentsPacker.PackArrayHeader(1);
                                            argumentsPacker.Pack(message);
                                        }

                                        argumentsBuffer.Position = 0;

                                        var target = new LocatorBasedDispatcher(server);
                                        MessagePackObject response = MessagePackObject.Nil;
                                        requestContext.MethodName = "Echo:TestService:1";
                                        requestContext.MessageId  = 1;
                                        requestContext.SetTransport(transport);
                                        requestContext.ArgumentsUnpacker = Unpacker.Create(argumentsBuffer);
                                        transport.Sent +=
                                            (sender, e) =>
                                        {
                                            response = Unpacking.UnpackString(e.Context.GetReturnValueData()).Value;
                                            waitHandle.Set();
                                        };
                                        target.Dispatch(transport, requestContext);

                                        Assert.That(waitHandle.Wait(TimeSpan.FromSeconds(1)));

                                        Assert.That(message == response, "{0} != {1}", message, response);
                                    }
            }
            finally
            {
                File.Delete(svcFile);
            }
        }
コード例 #5
0
		public void TestDispatch_MethodExists_Success()
		{
			var svcFile = ".\\Services.svc";
			File.WriteAllText(
				svcFile,
				String.Format( CultureInfo.InvariantCulture, "<% @ ServiceHost Service=\"{0}\" %>", typeof( TestService ).FullName )
			);
			try
			{
				var configuration = new RpcServerConfiguration();
				configuration.ServiceTypeLocatorProvider = conf => new FileBasedServiceTypeLocator();

				using ( var server = new RpcServer( configuration ) )
				using ( var transportManager = new NullServerTransportManager( server ) )
				using ( var transport = new NullServerTransport( transportManager ) )
				using ( var requestContext = DispatchTestHelper.CreateRequestContext() )
				using ( var argumentsBuffer = new MemoryStream() )
				using ( var waitHandle = new ManualResetEventSlim() )
				{
					var message = Guid.NewGuid().ToString();
					using ( var argumentsPacker = Packer.Create( argumentsBuffer, false ) )
					{
						argumentsPacker.PackArrayHeader( 1 );
						argumentsPacker.Pack( message );
					}

					argumentsBuffer.Position = 0;

					var target = new LocatorBasedDispatcher( server );
					MessagePackObject response = MessagePackObject.Nil;
					requestContext.MethodName = "Echo:TestService:1";
					requestContext.MessageId = 1;
					requestContext.SetTransport( transport );
					requestContext.ArgumentsUnpacker = Unpacker.Create( argumentsBuffer );
					transport.Sent +=
						( sender, e ) =>
						{
							response = Unpacking.UnpackString( e.Context.GetReturnValueData() ).Value;
							waitHandle.Set();
						};
					target.Dispatch( transport, requestContext );

					Assert.That( waitHandle.Wait( TimeSpan.FromSeconds( 1 ) ) );

					Assert.That( message == response, "{0} != {1}", message, response );
				}
			}
			finally
			{
				File.Delete( svcFile );
			}
		}
コード例 #6
0
        public void TestSetException_ExceptionIsNotNull_Serialized()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var responseContext = DispatchTestHelper.CreateResponseContext(transport))
                        {
                            var target = new Target(server);
                            target.InvokeSetException(responseContext, "Method", new RpcMissingMethodException("Method"));

                            // Details should be tested in ServerResponseContextTest.TestSerialize...
                            Assert.That(Unpacking.UnpackString(responseContext.GetErrorData()).Value == RpcError.NoMethodError.Identifier);
                        }
        }
コード例 #7
0
        public void TestInvokeAsync_Success_TaskSetSerializedReturnValue()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                    {
                        ServerRequestContext  requestContext  = DispatchTestHelper.CreateRequestContext();
                        ServerResponseContext responseContext = DispatchTestHelper.CreateResponseContext(transport);
                        using (var result = new Target(null, RpcErrorMessage.Success).InvokeAsync(requestContext, responseContext))
                        {
                            result.Wait();
                        }

                        Assert.That(responseContext.GetReturnValueData(), Is.EqualTo(new byte[] { 123 }));
                        Assert.That(responseContext.GetErrorData(), Is.EqualTo(new byte[] { 0xC0 }));
                    }
        }
コード例 #8
0
        public void TestSetReturnValue_ReturnValueIsValueType_Serialized()
        {
            var returnValue = Environment.TickCount;

            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var responseContext = DispatchTestHelper.CreateResponseContext(transport))
                        {
                            var target = new Target(server);
                            target.InvokeSetReturnValue(responseContext, returnValue);

                            // Details should be tested in ServerResponseContextTest.TestSerialize...
                            Assert.That(Unpacking.UnpackObject(responseContext.GetErrorData()).Value.IsNil);
                            Assert.That(Unpacking.UnpackObject(responseContext.GetReturnValueData()).Value == returnValue);
                        }
        }
コード例 #9
0
        public void TestInvokeAsync_MethodError_TaskSetSerializedError()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                    {
                        ServerRequestContext  requestContext  = DispatchTestHelper.CreateRequestContext();
                        ServerResponseContext responseContext = DispatchTestHelper.CreateResponseContext(transport);
                        using (var result = new Target(null, new RpcErrorMessage(RpcError.ArgumentError, MessagePackObject.Nil)).InvokeAsync(requestContext, responseContext))
                        {
                            result.Wait();
                        }

                        var error       = Unpacking.UnpackObject(responseContext.GetErrorData());
                        var errorDetail = Unpacking.UnpackObject(responseContext.GetReturnValueData());
                        Assert.That(error.Value.Equals(RpcError.ArgumentError.Identifier));
                        Assert.That(errorDetail.Value.IsNil, Is.True);
                    }
        }
コード例 #10
0
        public void TestInvokeAsync_FatalError_TaskSetSerializedError()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                    {
                        ServerRequestContext  requestContext  = DispatchTestHelper.CreateRequestContext();
                        ServerResponseContext responseContext = DispatchTestHelper.CreateResponseContext(transport);
                        using (var result = new Target(new Exception("FAIL"), RpcErrorMessage.Success).InvokeAsync(requestContext, responseContext))
                        {
                            result.Wait();
                        }

                        var error       = Unpacking.UnpackObject(responseContext.GetErrorData());
                        var errorDetail = Unpacking.UnpackObject(responseContext.GetReturnValueData());
                        Assert.That(error.Value.Equals(RpcError.CallError.Identifier));
                        Assert.That(errorDetail.Value.IsNil, Is.False);
                    }
        }
コード例 #11
0
        public void TestDispatch_NonNull_Dispatch_String_Invoked()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var requestContext = DispatchTestHelper.CreateRequestContext())
                        {
                            var  target  = new Target(server);
                            bool invoked = false;
                            target.Dispatching       += (sender, e) => { invoked = true; };
                            requestContext.MethodName = "Method";
                            requestContext.MessageId  = 1;
                            requestContext.SetTransport(transport);

                            target.Dispatch(transport, requestContext);

                            Assert.That(invoked, Is.True);
                        }
        }
コード例 #12
0
        public void TestDispatch_MethodNotExists_NoMethodError()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var requestContext = DispatchTestHelper.CreateRequestContext())
                            using (var argumentsBuffer = new MemoryStream())
                                using (var waitHandle = new ManualResetEventSlim())
                                {
                                    var message = Guid.NewGuid().ToString();
                                    using (var argumentsPacker = Packer.Create(argumentsBuffer, false))
                                    {
                                        argumentsPacker.PackArrayHeader(1);
                                        argumentsPacker.Pack(message);
                                    }

                                    argumentsBuffer.Position = 0;

                                    var target = new LocatorBasedDispatcher(server);
                                    MessagePackObject response = MessagePackObject.Nil;
                                    requestContext.MethodName = "Echo:TestServices:1";
                                    requestContext.MessageId  = 1;
                                    requestContext.SetTransport(transport);
                                    requestContext.ArgumentsUnpacker = Unpacker.Create(argumentsBuffer);
                                    transport.Sent +=
                                        (sender, e) =>
                                    {
                                        response = Unpacking.UnpackString(e.Context.GetErrorData()).Value;
                                        waitHandle.Set();
                                    };
                                    target.Dispatch(transport, requestContext);

                                    Assert.That(waitHandle.Wait(TimeSpan.FromSeconds(1)));

                                    Assert.That(RpcError.NoMethodError.Identifier == response, "{0} != {1}", message, response);
                                }
        }
コード例 #13
0
		public void TestDispatch_MethodNotExists_NoMethodError()
		{
			using ( var server = new RpcServer() )
			using ( var transportManager = new NullServerTransportManager( server ) )
			using ( var transport = new NullServerTransport( transportManager ) )
			using ( var requestContext = DispatchTestHelper.CreateRequestContext() )
			using ( var argumentsBuffer = new MemoryStream() )
			using ( var waitHandle = new ManualResetEventSlim() )
			{
				var message = Guid.NewGuid().ToString();
				using ( var argumentsPacker = Packer.Create( argumentsBuffer, false ) )
				{
					argumentsPacker.PackArrayHeader( 1 );
					argumentsPacker.Pack( message );
				}

				argumentsBuffer.Position = 0;

				var target = new LocatorBasedDispatcher( server );
				MessagePackObject response = MessagePackObject.Nil;
				requestContext.MethodName = "Echo:TestServices:1";
				requestContext.MessageId = 1;
				requestContext.SetTransport( transport );
				requestContext.ArgumentsUnpacker = Unpacker.Create( argumentsBuffer );
				transport.Sent +=
					( sender, e ) =>
					{
						response = Unpacking.UnpackString( e.Context.GetErrorData() ).Value;
						waitHandle.Set();
					};
				target.Dispatch( transport, requestContext );

				Assert.That( waitHandle.Wait( TimeSpan.FromSeconds( 1 ) ) );

				Assert.That( RpcError.NoMethodError.Identifier == response, "{0} != {1}", message, response );
			}
		}