Exemplo n.º 1
0
		public void TestConnectAsync_Null()
		{
			using ( var target = new TcpClientTransportManager( new RpcClientConfiguration() ) )
			{
				target.ConnectAsync( null );
			}
		}
Exemplo n.º 2
0
		public void TestConnectAsync_Success()
		{
			var endPoint = new IPEndPoint( IPAddress.Loopback, 57319 );

			var listener = new TcpListener( endPoint );
			try
			{
				listener.Start();

				using ( var target = new TcpClientTransportManager( new RpcClientConfiguration() ) )
				using ( var result = target.ConnectAsync( endPoint ) )
				{
					Assert.That( result.Wait( TimeSpan.FromSeconds( 1 ) ) );
					try
					{
						Assert.That( result.Result.BoundSocket.RemoteEndPoint, Is.EqualTo( endPoint ) );
					}
					finally
					{
						result.Result.Dispose();
					}
				}
			}
			finally
			{
				listener.Stop();
			}
		}
Exemplo n.º 3
0
		private static void TestSendReceiveRequest( Action<IPEndPoint, TcpClientTransportManager> test )
		{
			using ( var server = MsgPack.Rpc.Server.CallbackServer.Create( ( id, args ) => args, isDebugMode: true ) )
			{
				var ipEndPoint = new IPEndPoint( IPAddress.Loopback, MsgPack.Rpc.Server.CallbackServer.PortNumber );

				using ( var clientTransportManager = new TcpClientTransportManager( new RpcClientConfiguration() ) )
				{
					test( ipEndPoint, clientTransportManager );
				}
			}
		}
Exemplo n.º 4
0
		public void TestConnectAsync_Timeout()
		{
			var testNetworkIPEndPont = new IPEndPoint( IPAddress.Parse( "198.51.100.1" ), 12345 ); // c.f. http://tools.ietf.org/html/rfc5737)
			var configuration = new RpcClientConfiguration();
			configuration.ConnectTimeout = TimeSpan.FromMilliseconds( 20 );
			using ( var target = new TcpClientTransportManager( configuration ) )
			{
				var actual = Assert.Throws<AggregateException>( () => target.ConnectAsync( testNetworkIPEndPont ).Wait( TimeSpan.FromSeconds( 1 ) ) );
				Assert.That( actual.InnerExceptions.Count, Is.EqualTo( 1 ) );
				Assert.That( actual.InnerException, Is.InstanceOf<RpcException>() );
				Assert.That( ( actual.InnerException as RpcException ).RpcError, Is.EqualTo( RpcError.ConnectionTimeoutError ), actual.ToString() );
			}
		}
Exemplo n.º 5
0
 public TcpTestEnvironment()
 {
     this._server = new TcpListener(IPAddress.Loopback, MsgPack.Rpc.Server.CallbackServer.PortNumber);
     this._clientTransportManager = new TcpClientTransportManager(new RpcClientConfiguration());
     this._server.Start();
 }
Exemplo n.º 6
0
			public TcpTestEnvironment()
			{
				this._server = new TcpListener( IPAddress.Loopback, MsgPack.Rpc.Server.CallbackServer.PortNumber );
				this._clientTransportManager = new TcpClientTransportManager( new RpcClientConfiguration() );
				this._server.Start();
			}
Exemplo n.º 7
0
		public void TestConnectAsync_ImplicitTimeout_TranslationOk()
		{
			if ( Environment.OSVersion.Platform != PlatformID.Win32NT )
			{
				Assert.Inconclusive( "This test dependes on WinSock2" );
			}

			var testNetworkIPEndPont = new IPEndPoint( IPAddress.Parse( "198.51.100.1" ), 12345 ); // c.f. http://tools.ietf.org/html/rfc5737)
			var configuration = new RpcClientConfiguration();
			using ( var target = new TcpClientTransportManager( configuration ) )
			{
				// WinSock TCP/IP has 20sec timeout...
				var actual = Assert.Throws<AggregateException>( () => target.ConnectAsync( testNetworkIPEndPont ).Result.Dispose() );
				Assert.That( actual.InnerExceptions.Count, Is.EqualTo( 1 ) );
				Assert.That( actual.InnerException, Is.InstanceOf<RpcException>() );
				Assert.That( ( actual.InnerException as RpcException ).RpcError, Is.EqualTo( RpcError.ConnectionTimeoutError ), actual.ToString() );
			}
		}
Exemplo n.º 8
0
		private static void TestSendReceiveRequestCore( IPEndPoint endPoint, int count, int concurrency )
		{
			_SetUpFixture.EnsureThreadPoolCapacity();
			using ( var clientTransportManager = new TcpClientTransportManager( new RpcClientConfiguration() ) )
			{
				 var connectTask = clientTransportManager.ConnectAsync( endPoint );

				if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
				{
					throw new TimeoutException();
				}

				using ( var clientTransport = connectTask.Result )
				{
					for ( int i = 0; i < count; i++ )
					{
						using ( var latch = new CountdownEvent( concurrency ) )
						{
							var ids = Enumerable.Range( i * concurrency, concurrency ).ToArray();
							var args = Enumerable.Repeat( 0, concurrency ).Select( _ => Guid.NewGuid().ToString() ).ToArray();
							var idAndArgs = ids.Zip( args, ( id, arg ) => new { MessageId = id, Guid = arg.ToString() } );
							var requestTable = new ConcurrentDictionary<int, string>();
							var responseTable = new ConcurrentDictionary<int, string>();
							var exceptions = new ConcurrentBag<Exception>();

							if ( !Task.Factory.ContinueWhenAll(
								idAndArgs.Select(
									idAndArg =>
										Task.Factory.StartNew(
											() =>
											{
												var requestContext = clientTransport.GetClientRequestContext();
												requestTable[ idAndArg.MessageId ] = idAndArg.Guid;
												requestContext.SetRequest(
													idAndArg.MessageId,
													"Dummy",
													( responseContext, exception, completedSynchronously ) =>
													{
														try
														{
															if ( exception != null )
															{
																exceptions.Add( exception );
															}
															else
															{
																// Server returns args as array, so store only first element.
																responseTable[ responseContext.MessageId.Value ] = Unpacking.UnpackArray( responseContext.ResultBuffer )[ 0 ].AsString();
															}
														}
														finally
														{
															latch.Signal();
														}
													}
												);
												requestContext.ArgumentsPacker.PackArrayHeader( 1 );
												requestContext.ArgumentsPacker.Pack( idAndArg.Guid );

												return requestContext;
											}
										)
								).ToArray(),
								previouses =>
								{
									var contexts = previouses.Select( previous => previous.Result ).ToArray();
									foreach ( var context in contexts )
									{
										clientTransport.Send( context );
									}
								}
							).ContinueWith(
								previous =>
								{
									if ( previous.IsFaulted )
									{
										throw previous.Exception;
									}

									// receive
									if ( !latch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
									{
										throw new TimeoutException( "Receive" );
									}

									if ( exceptions.Any() )
									{
										throw new AggregateException( exceptions );
									}

									Assert.That( requestTable.Count, Is.EqualTo( concurrency ) );
									Assert.That( requestTable, Is.EquivalentTo( responseTable ) );
								}
							).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
							{
								throw new TimeoutException();
							}
						}
					}
				}
			}
		}
Exemplo n.º 9
0
		private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count )
		{
			using ( var clientTransportManager = new TcpClientTransportManager( new RpcClientConfiguration() ) )
			using ( var connectTask = clientTransportManager.ConnectAsync( endPoint ) )
			{
				if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
				{
					throw new TimeoutException();
				}

				using ( var clientTransport = connectTask.Result )
				{
					for ( int i = 0; i < count; i++ )
					{
						if ( arrivalLatch != null )
						{
							arrivalLatch.Reset();
						}

						var args = Enumerable.Repeat( 0, arrivalLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray();
						var exceptions = new ConcurrentBag<Exception>();

						if ( !Task.Factory.ContinueWhenAll(
							args.Select(
								arg =>
									Task.Factory.StartNew(
										() =>
										{
											var requestContext = clientTransport.GetClientRequestContext();
											requestContext.SetNotification(
												"Dummy",
												( exception, completedSynchronously ) =>
												{
													if ( exception != null )
													{
														exceptions.Add( exception );
													}

													arrivalLatch.Signal();
												}
											);
											requestContext.ArgumentsPacker.PackArrayHeader( 1 );
											requestContext.ArgumentsPacker.Pack( arg );

											return requestContext;
										}
									)
							).ToArray(),
							previouses =>
							{
								var contexts = previouses.Select( previous => previous.Result ).ToArray();
								foreach ( var context in contexts )
								{
									clientTransport.Send( context );
								}
							}
						).ContinueWith(
							previous =>
							{
								if ( previous.IsFaulted )
								{
									throw previous.Exception;
								}

								// receive
								if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
								{
									throw new TimeoutException( "Receive" );
								}

								if ( exceptions.Any() )
								{
									throw new AggregateException( exceptions );
								}
							}
						).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
						{
							throw new TimeoutException();
						}
					}
				}
			}
		}
Exemplo n.º 10
0
		/// <summary>
		///		Initializes a new instance of the <see cref="TcpClientTransport"/> class.
		/// </summary>
		/// <param name="manager">The manager which will manage this instance.</param>
		/// <exception cref="ArgumentNullException">
		///		<paramref name="manager"/> is <c>null</c>.
		/// </exception>
		public TcpClientTransport( TcpClientTransportManager manager )
			: base( manager ) { }