示例#1
0
        public void TestSoftTimeout_ExceptionThrownOnStop()
        {
            var timeout = TimeSpan.FromMilliseconds(50);

            using (var target = new RpcApplicationContext(timeout, null))
                using (var cancellationEvent = new ManualResetEventSlim())
                {
                    var message = Guid.NewGuid().ToString();
                    RpcApplicationContext.SetCurrent(target);
#pragma warning disable 0618
                    target.DebugSoftTimeout += (sender, e) => cancellationEvent.Set();
#pragma warning restore 0618
                    target.CancellationToken.Register(() =>
                    {
                        throw new Exception(message);
                    }
                                                      );
                    target.StartTimeoutWatch();
                    Assert.That(target.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(1)));
                    // Mono sets WaitHandle eagerly (CLR might set after callback)
                    Assert.That(cancellationEvent.Wait(TimeSpan.FromSeconds(1)));
                    Assert.That(RpcApplicationContext.IsCanceled);
                    var exception = Assert.Throws <AggregateException>(() => target.StopTimeoutWatch());
                    Assert.That(exception.InnerException.Message, Is.EqualTo(message));
                }
        }
示例#2
0
        public void TestClear_CanReuse()
        {
            var timeout = TimeSpan.FromMilliseconds(50);

            using (var target = new RpcApplicationContext(timeout, timeout))
            {
                for (int i = 0; i < 2; i++)
                {
                    RpcApplicationContext.Clear();
                    RpcApplicationContext.SetCurrent(target);
                    target.StartTimeoutWatch();
                    try
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 3));
                        Assert.Fail("Attempt:{0}", i);
                    }
                    catch (ThreadAbortException ex)
                    {
                        Assert.That(ex.ExceptionState, Is.EqualTo(RpcApplicationContext.HardTimeoutToken));
                        Thread.ResetAbort();
                    }

                    Assert.That(RpcApplicationContext.IsCanceled);
                }
            }
        }
示例#3
0
		public void TestSoftTimeout_ExceptionThrownOnStop()
		{
			var timeout = TimeSpan.FromMilliseconds( 50 );
			using ( var target = new RpcApplicationContext( timeout, null ) )
			using ( var cancellationEvent = new ManualResetEventSlim() )
			{
				var message = Guid.NewGuid().ToString();
				RpcApplicationContext.SetCurrent( target );
#pragma warning disable 0618
				target.DebugSoftTimeout += ( sender, e ) => cancellationEvent.Set();
#pragma warning restore 0618
				target.CancellationToken.Register( () =>
					{
						throw new Exception( message );
					}
				);
				target.StartTimeoutWatch();
				Assert.That( target.CancellationToken.WaitHandle.WaitOne( TimeSpan.FromSeconds( 1 ) ) );
				// Mono sets WaitHandle eagerly (CLR might set after callback)
				Assert.That( cancellationEvent.Wait( TimeSpan.FromSeconds( 1 ) ) );
				Assert.That( RpcApplicationContext.IsCanceled );
				var exception = Assert.Throws<AggregateException>( () => target.StopTimeoutWatch() );
				Assert.That( exception.InnerException.Message, Is.EqualTo( message ) );
			}
		}
示例#4
0
		public void TestSoftTimeout_TokenCanceledAndIsCanceledToBeTrue()
		{
			var timeout = TimeSpan.FromMilliseconds( 50 );
			using ( var target = new RpcApplicationContext( timeout, null ) )
			{
				RpcApplicationContext.SetCurrent( target );
				target.StartTimeoutWatch();
				Assert.That( target.CancellationToken.WaitHandle.WaitOne( TimeSpan.FromSeconds( 1 ) ) );
				Assert.That( RpcApplicationContext.IsCanceled );
			}
		}
示例#5
0
        public void TestSoftTimeout_TokenCanceledAndIsCanceledToBeTrue()
        {
            var timeout = TimeSpan.FromMilliseconds(50);

            using (var target = new RpcApplicationContext(timeout, null))
            {
                RpcApplicationContext.SetCurrent(target);
                target.StartTimeoutWatch();
                Assert.That(target.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(1)));
                Assert.That(RpcApplicationContext.IsCanceled);
            }
        }
示例#6
0
		public void TestSoftTimeout_Stoped_Never()
		{
			var timeout = TimeSpan.FromMilliseconds( 50 );
			using ( var target = new RpcApplicationContext( timeout, null ) )
			{
				RpcApplicationContext.SetCurrent( target );
				target.StartTimeoutWatch();
				target.StopTimeoutWatch();
				Assert.That( target.CancellationToken.WaitHandle.WaitOne( TimeSpan.FromMilliseconds( timeout.TotalMilliseconds * 2 ) ), Is.False );
				Assert.That( RpcApplicationContext.IsCanceled, Is.False );
			}
		}
示例#7
0
        public void TestSoftTimeout_Stoped_Never()
        {
            var timeout = TimeSpan.FromMilliseconds(50);

            using (var target = new RpcApplicationContext(timeout, null))
            {
                RpcApplicationContext.SetCurrent(target);
                target.StartTimeoutWatch();
                target.StopTimeoutWatch();
                Assert.That(target.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 2)), Is.False);
                Assert.That(RpcApplicationContext.IsCanceled, Is.False);
            }
        }
示例#8
0
        public void TestTimeout_SoftTimeoutIsNull_NeverTimeout()
        {
            var timeout = TimeSpan.FromMilliseconds(20);

            using (var waitHandle = new ManualResetEventSlim())
                using (var target = new RpcApplicationContext(null, timeout))
                {
                    RpcApplicationContext.SetCurrent(target);
                    target.StartTimeoutWatch();
                    Assert.That(waitHandle.Wait(TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 2)), Is.False);
                    Assert.That(RpcApplicationContext.IsCanceled, Is.False);
                }
        }
示例#9
0
        public void TestHardTimeout_Stopped_Never()
        {
            var timeout = TimeSpan.FromMilliseconds(20);

            using (var waitHandle = new ManualResetEventSlim())
                using (var target = new RpcApplicationContext(timeout, timeout))
                {
                    RpcApplicationContext.SetCurrent(target);
                    target.StartTimeoutWatch();
                    target.CancellationToken.Register(() => waitHandle.Set());

                    Assert.That(waitHandle.Wait(TimeSpan.FromSeconds(1)));
                    target.StopTimeoutWatch();
                    Thread.Sleep(TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 2));

                    Assert.That(RpcApplicationContext.IsCanceled);
                }
        }
示例#10
0
        public void TestHardTimeout_TokenCanceledAndIsCanceledToBeTrue()
        {
            var timeout = TimeSpan.FromMilliseconds(20);

            using (var target = new RpcApplicationContext(timeout, timeout))
            {
                RpcApplicationContext.SetCurrent(target);
                target.StartTimeoutWatch();
                try
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 3));
                    Assert.Fail();
                }
                catch (ThreadAbortException ex)
                {
                    Assert.That(ex.ExceptionState, Is.EqualTo(RpcApplicationContext.HardTimeoutToken));
                    Thread.ResetAbort();
                }

                Assert.That(RpcApplicationContext.IsCanceled);
            }
        }
示例#11
0
		public void TestHardTimeout_TokenCanceledAndIsCanceledToBeTrue()
		{
			var timeout = TimeSpan.FromMilliseconds( 20 );
			using ( var target = new RpcApplicationContext( timeout, timeout ) )
			{
				RpcApplicationContext.SetCurrent( target );
				target.StartTimeoutWatch();
				try
				{
					Thread.Sleep( TimeSpan.FromMilliseconds( timeout.TotalMilliseconds * 3 ) );
					Assert.Fail();
				}
				catch ( ThreadAbortException ex )
				{
					Assert.That( ex.ExceptionState, Is.EqualTo( RpcApplicationContext.HardTimeoutToken ) );
					Thread.ResetAbort();
				}

				Assert.That( RpcApplicationContext.IsCanceled );
			}
		}
示例#12
0
		public void TestClear_CanReuse()
		{
			var timeout = TimeSpan.FromMilliseconds( 50 );
			using ( var target = new RpcApplicationContext( timeout, timeout ) )
			{
				for ( int i = 0; i < 2; i++ )
				{
					RpcApplicationContext.Clear();
					RpcApplicationContext.SetCurrent( target );
					target.StartTimeoutWatch();
					try
					{
						Thread.Sleep( TimeSpan.FromMilliseconds( timeout.TotalMilliseconds * 3 ) );
						Assert.Fail( "Attempt:{0}", i );
					}
					catch ( ThreadAbortException ex )
					{
						Assert.That( ex.ExceptionState, Is.EqualTo( RpcApplicationContext.HardTimeoutToken ) );
						Thread.ResetAbort();
					}

					Assert.That( RpcApplicationContext.IsCanceled );
				}
			}
		}
示例#13
0
		public void TestTimeout_SoftTimeoutIsNull_NeverTimeout()
		{
			var timeout = TimeSpan.FromMilliseconds( 20 );
			using ( var waitHandle = new ManualResetEventSlim() )
			using ( var target = new RpcApplicationContext( null, timeout ) )
			{
				RpcApplicationContext.SetCurrent( target );
				target.StartTimeoutWatch();
				Assert.That( waitHandle.Wait( TimeSpan.FromMilliseconds( timeout.TotalMilliseconds * 2 ) ), Is.False );
				Assert.That( RpcApplicationContext.IsCanceled, Is.False );
			}
		}
示例#14
0
		public void TestHardTimeout_Stopped_Never()
		{
			var timeout = TimeSpan.FromMilliseconds( 20 );
			using ( var waitHandle = new ManualResetEventSlim() )
			using ( var target = new RpcApplicationContext( timeout, timeout ) )
			{
				RpcApplicationContext.SetCurrent( target );
				target.StartTimeoutWatch();
				target.CancellationToken.Register( () => waitHandle.Set() );

				Assert.That( waitHandle.Wait( TimeSpan.FromSeconds( 1 ) ) );
				target.StopTimeoutWatch();
				Thread.Sleep( TimeSpan.FromMilliseconds( timeout.TotalMilliseconds * 2 ) );

				Assert.That( RpcApplicationContext.IsCanceled );
			}
		}