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 ) ); } }
/// <summary> /// Sets the current context for this thread. /// </summary> /// <param name="context">The context.</param> internal static void SetCurrent( RpcApplicationContext context ) { _current = context; context._boundThread = new WeakReference( Thread.CurrentThread ); #if !SILVERLIGHT context._hardTimeoutWatcher.Reset(); #endif context._softTimeoutWatcher.Reset(); }
/// <summary> /// Sets the current context for this thread. /// </summary> /// <param name="context">The context.</param> internal static void SetCurrent(RpcApplicationContext context) { _current = context; context._boundThread = new WeakReference(Thread.CurrentThread); #if !SILVERLIGHT context._hardTimeoutWatcher.Reset(); #endif context._softTimeoutWatcher.Reset(); }
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 ); } }
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 ); } }
/// <summary> /// Clears current instance. /// </summary> internal static void Clear() { var current = _current; if ( current != null ) { try { current.StopTimeoutWatch(); } finally { current._boundThread = null; Interlocked.Exchange( ref current._state, StateActive ); _current = null; } } }
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 ); } }
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 ); } } }
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 ); } }
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 ); } }