Esempio n. 1
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);
                 }
 }
        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);
            }
        }
Esempio n. 3
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);
                        }
        }
Esempio n. 4
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 }));
                    }
        }
Esempio n. 5
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);
                        }
        }
Esempio n. 6
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);
                    }
        }
Esempio n. 7
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);
                    }
        }
Esempio n. 8
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);
                        }
        }
        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);
                                }
        }