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); } } } } } }
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 ); } }
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); } }
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 ); } }
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); } }
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 })); } }
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); } }
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); } }
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); } }
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); } }
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 ); } }