Beispiel #1
0
        public void TimedOut()
        {
            // Arrange
            using (ManualResetEvent waitHandle = new ManualResetEvent(false /* initialState */))
            {
                AsyncCallback callback = ar => { waitHandle.Set(); };

                // Act & assert
                using (var mockResult = new MockAsyncResult())
                {
                    IAsyncResult asyncResult = AsyncResultWrapper.Begin <object>(
                        callback, null,
                        (innerCallback, callbackState, state) => mockResult,
                        (ar, state) => { Assert.True(false, "This callback should never execute since we timed out."); },
                        null,
                        null, 0);

                    // wait for the timeout
                    waitHandle.WaitOne();

                    Assert.True(asyncResult.IsCompleted);
                    Assert.Throws <TimeoutException>(
                        delegate { AsyncResultWrapper.End(asyncResult); });
                }
            }
        }
Beispiel #2
0
        public void Begin_WithCallbackSyncContext_ThrowsAsyncEvenIfSendContextCaptures()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException(
                "Some exception text."
                );
            CapturingSynchronizationContext capturingSyncContext =
                new CapturingSynchronizationContext();
            MockAsyncResult asyncResult = new MockAsyncResult()
            {
                CompletedSynchronously = false,
                IsCompleted            = true
            };

            using (asyncResult)
            {
                bool          originalCallbackCalled = false;
                IAsyncResult  passedAsyncResult      = null;
                AsyncCallback passedCallback         = null;
                AsyncCallback originalCallback       = ar =>
                {
                    passedAsyncResult      = ar;
                    originalCallbackCalled = true;
                    throw exception;
                };

                // Act & Assert
                IAsyncResult outerResult = AsyncResultWrapper.Begin <object>(
                    originalCallback,
                    null,
                    (callback, callbackState, state) =>
                {
                    passedCallback         = callback;
                    asyncResult.AsyncState = callbackState;
                    return(asyncResult);
                },
                    (ar, state) =>
                {
                    asyncResult.IsCompleted = true;
                    passedCallback(ar);
                },
                    null,
                    callbackSyncContext: capturingSyncContext
                    );
                SynchronousOperationException thrownException =
                    Assert.Throws <SynchronousOperationException>(
                        delegate
                {
                    AsyncResultWrapper.End(outerResult);
                },
                        @"An operation that crossed a synchronization context failed. See the inner exception for more information."
                        );

                // Assert
                Assert.Equal(exception, thrownException.InnerException);
                Assert.True(originalCallbackCalled);
                Assert.False(passedAsyncResult.CompletedSynchronously);
                Assert.True(capturingSyncContext.SendCalled);
            }
        }
 protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)
 {
     SecurityUtil.ProcessInApplicationTrust(() =>
     {
         AsyncResultWrapper.End(asyncResult, _processRequestTag);
     });
 }
Beispiel #4
0
        public void End_ThrowsIfAsyncResultTagMismatch()
        {
            // Arrange
            using (var mockResult = new MockAsyncResult())
            {
                IAsyncResult asyncResult = AsyncResultWrapper.Begin <object>(
                    null,
                    null,
                    (callback, callbackState, state) => mockResult,
                    (ar, state) => { },
                    null,
                    "some tag"
                    );

                // Act & assert
                Assert.Throws <ArgumentException>(
                    delegate
                {
                    AsyncResultWrapper.End(asyncResult, "some other tag");
                },
                    "The provided IAsyncResult is not valid for this method."
                    + Environment.NewLine
                    + "Parameter name: asyncResult"
                    );
            }
        }
        public void TimedOut()
        {
            // Arrange
            ManualResetEvent waitHandle = new ManualResetEvent(false /* initialState */);

            AsyncCallback callback = ar => {
                waitHandle.Set();
            };

            // Act & assert
            IAsyncResult asyncResult = AsyncResultWrapper.Begin(callback, null,
                                                                (innerCallback, innerState) => new MockAsyncResult(),
                                                                ar => {
                Assert.Fail("This callback should never execute since we timed out.");
            },
                                                                null, 0);

            // wait for the timeout
            waitHandle.WaitOne();

            ExceptionHelper.ExpectException <TimeoutException>(
                delegate {
                AsyncResultWrapper.End(asyncResult);
            });
        }
Beispiel #6
0
        public void Begin_AsynchronousCompletionWithStateAndResult()
        {
            // Arrange
            using (MockAsyncResult innerResult = new MockAsyncResult())
            {
                object invokeState        = new object();
                object capturedBeginState = null;
                object capturedEndState   = null;
                object expectedRetun      = new object();

                // Act
                IAsyncResult outerResult = AsyncResultWrapper.Begin(
                    null,
                    null,
                    (AsyncCallback callback, object callbackState, object innerInvokeState) =>
                {
                    capturedBeginState = innerInvokeState;
                    return(innerResult);
                },
                    (IAsyncResult result, object innerInvokeState) =>
                {
                    capturedEndState = innerInvokeState;
                    return(expectedRetun);
                },
                    invokeState,
                    null,
                    Timeout.Infinite);
                object endResult = AsyncResultWrapper.End <object>(outerResult);

                // Assert
                Assert.Same(expectedRetun, endResult);
                Assert.Same(invokeState, capturedBeginState);
                Assert.Same(invokeState, capturedEndState);
            }
        }
 public void End_ThrowsIfAsyncResultIsNull()
 {
     // Act & assert
     ExceptionHelper.ExpectArgumentNullException(
         delegate {
         AsyncResultWrapper.End(null);
     }, "asyncResult");
 }
Beispiel #8
0
 public void End_ThrowsIfAsyncResultIsNull()
 {
     // Act & assert
     Assert.ThrowsArgumentNull(
         delegate
     {
         AsyncResultWrapper.End(null);
     },
         "asyncResult"
         );
 }
        public void BeginSynchronous_Func()
        {
            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, () => 42);
            int          retVal      = AsyncResultWrapper.End <int>(asyncResult);

            // Assert
            Assert.Equal(42, retVal);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
        }
Beispiel #10
0
        protected virtual void EndExecuteCore(IAsyncResult asyncResult)
        {
            // If code in this method needs to be updated, please also check the ExecuteCore() method
            // of Controller to see if that code also must be updated.

            try {
                AsyncResultWrapper.End(asyncResult, _executeCoreTag);
            }
            finally {
                PossiblySaveTempData();
            }
        }
        public void End_ExecutesStoredDelegateAndReturnsValue()
        {
            // Arrange
            IAsyncResult asyncResult = AsyncResultWrapper.Begin(null, null,
                                                                (callback, state) => new MockAsyncResult(),
                                                                ar => 42);

            // Act
            int returned = AsyncResultWrapper.End <int>(asyncResult);

            // Assert
            Assert.AreEqual(42, returned);
        }
Beispiel #12
0
        /// <summary>
        /// Ends the wrapped operation.
        /// </summary>
        /// <param name="result">The result.</param>
        public static void EndWrap(IAsyncResult result)
        {
            Requires.NotNull(result, "result");

            AsyncResultWrapper wrapper = result as AsyncResultWrapper;

            if (wrapper == null)
            {
                throw ExceptionBuilder.ArgumentNotExpectedType("result", typeof(AsyncResultWrapper));
            }

            wrapper.End();
        }
Beispiel #13
0
        /// <summary>
        /// Ends the wrapped operation.
        /// </summary>
        /// <typeparam name="T">The type of the operation result.</typeparam>
        /// <param name="result">The result.</param>
        /// <returns>The operation result.</returns>
        public static T EndWrap <T>(IAsyncResult result)
        {
            Requires.NotNull(result, "result");

            AsyncResultWrapper <T> wrapper = result as AsyncResultWrapper <T>;

            if (wrapper == null)
            {
                throw ExceptionBuilder.ArgumentNotExpectedType("result", typeof(AsyncResultWrapper <T>));
            }

            return(wrapper.End());
        }
        public void End_ThrowsIfCalledTwiceOnSameAsyncResult()
        {
            // Arrange
            IAsyncResult asyncResult = AsyncResultWrapper.Begin(
                null, null,
                (callback, state) => new MockAsyncResult(),
                ar => { });

            // Act & assert
            AsyncResultWrapper.End(asyncResult);
            Assert.Throws <InvalidOperationException>(
                delegate { AsyncResultWrapper.End(asyncResult); },
                "The provided IAsyncResult has already been consumed.");
        }
Beispiel #15
0
        public void Begin_WithCallbackSyncContext_CallsSendIfOperationCompletedAsynchronously()
        {
            // Arrange
            MockAsyncResult asyncResult = new MockAsyncResult()
            {
                CompletedSynchronously = false,
                IsCompleted            = false
            };

            using (asyncResult)
            {
                bool          originalCallbackCalled = false;
                IAsyncResult  passedAsyncResult      = null;
                AsyncCallback passedCallback         = null;
                AsyncCallback originalCallback       = ar =>
                {
                    originalCallbackCalled = true;
                    passedAsyncResult      = ar;
                };
                object originalState = new object();
                DummySynchronizationContext syncContext = new DummySynchronizationContext();

                // Act
                IAsyncResult outerResult = AsyncResultWrapper.Begin <object>(
                    originalCallback,
                    originalState,
                    (callback, callbackState, state) =>
                {
                    passedCallback         = callback;
                    asyncResult.AsyncState = callbackState;
                    return(asyncResult);
                },
                    (ar, state) =>
                {
                    asyncResult.IsCompleted = true;
                    passedCallback(ar);
                },
                    null,
                    callbackSyncContext: syncContext
                    );
                AsyncResultWrapper.End(outerResult);

                // Assert
                Assert.True(originalCallbackCalled);
                Assert.False(passedAsyncResult.CompletedSynchronously);
                Assert.True(passedAsyncResult.IsCompleted);
                Assert.Same(originalState, passedAsyncResult.AsyncState);
                Assert.True(syncContext.SendCalled);
            }
        }
        public void End_ThrowsIfAsyncResultIsIncorrectType()
        {
            // Arrange
            IAsyncResult asyncResult = AsyncResultWrapper.Begin(
                null, null,
                (callback, state) => new MockAsyncResult(),
                ar => { });

            // Act & assert
            Assert.Throws <ArgumentException>(
                delegate { AsyncResultWrapper.End <int>(asyncResult); },
                "The provided IAsyncResult is not valid for this method." + Environment.NewLine
                + "Parameter name: asyncResult");
        }
Beispiel #17
0
        public void End_ThrowsIfAsyncResultTagMismatch()
        {
            // Arrange
            IAsyncResult asyncResult = AsyncResultWrapper.Begin(
                null, null,
                (callback, state) => new MockAsyncResult(),
                ar => { },
                "some tag");

            // Act & assert
            Assert.Throws <ArgumentException>(
                delegate { AsyncResultWrapper.End(asyncResult, "some other tag"); },
                @"The provided IAsyncResult is not valid for this method.
Parameter name: asyncResult");
        }
        public void BeginSynchronous_Action()
        {
            // Arrange
            bool actionCalled = false;

            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, delegate { actionCalled = true; });

            AsyncResultWrapper.End(asyncResult);

            // Assert
            Assert.True(actionCalled);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
        }
Beispiel #19
0
        public void End_ThrowsIfCalledTwiceOnSameAsyncResult()
        {
            // Arrange
            using (var mockResult = new MockAsyncResult())
            {
                IAsyncResult asyncResult = AsyncResultWrapper.Begin <object>(
                    null, null,
                    (callback, callbackState, state) => mockResult,
                    (ar, state) => { },
                    null);

                // Act & assert
                AsyncResultWrapper.End(asyncResult);
                Assert.Throws <InvalidOperationException>(
                    delegate { AsyncResultWrapper.End(asyncResult); },
                    "The provided IAsyncResult has already been consumed.");
            }
        }
Beispiel #20
0
        public void BeginSynchronous_Func()
        {
            object expectedReturn        = new object();
            object expectedState         = new object();
            object expectedCallbackState = new object();
            bool   funcCalled            = false;
            bool   asyncCalledbackCalled = false;

            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(
                callback: (innerIAsyncResult) =>
            {
                asyncCalledbackCalled = true;
                Assert.NotNull(innerIAsyncResult);
                Assert.Same(expectedCallbackState, innerIAsyncResult.AsyncState);
                Assert.True(innerIAsyncResult.IsCompleted);
                Assert.True(innerIAsyncResult.CompletedSynchronously);
            },
                callbackState: expectedCallbackState,
                func: (innerIAsyncResult, innerState) =>
            {
                funcCalled = true;
                Assert.NotNull(innerIAsyncResult);
                Assert.Same(expectedCallbackState, innerIAsyncResult.AsyncState);
                Assert.Same(expectedState, innerState);
                Assert.True(innerIAsyncResult.IsCompleted);
                Assert.True(innerIAsyncResult.CompletedSynchronously);
                return(expectedReturn);
            },
                funcState: expectedState,
                tag: null
                );
            object retVal = AsyncResultWrapper.End <object>(asyncResult);

            // Assert
            Assert.Same(expectedReturn, retVal);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
            Assert.True(funcCalled);
            Assert.True(asyncCalledbackCalled);
        }
Beispiel #21
0
 protected virtual void EndExecute(IAsyncResult asyncResult)
 {
     AsyncResultWrapper.End(asyncResult, _executeTag);
 }
Beispiel #22
0
 protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)
 {
     AsyncResultWrapper.End(asyncResult, _processRequestTag);
 }