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); } }
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); }); }
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); }); } } }
public void Begin_AsynchronousCompletion() { // Arrange AsyncCallback capturedCallback = null; IAsyncResult resultGivenToCallback = null; using (MockAsyncResult innerResult = new MockAsyncResult()) { // Act IAsyncResult outerResult = AsyncResultWrapper.Begin <object>( ar => { resultGivenToCallback = ar; }, null, (callback, callbackState, state) => { capturedCallback = callback; return(innerResult); }, (ar, state) => { }, null); capturedCallback(innerResult); // Assert Assert.Equal(outerResult, resultGivenToCallback); } }
protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { IHttpHandler httpHandler = GetHttpHandler(httpContext); IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler; if (httpAsyncHandler != null) { // asynchronous handler // Ensure delegates continue to use the C# Compiler static delegate caching optimization. BeginInvokeDelegate <IHttpAsyncHandler> beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, IHttpAsyncHandler innerHandler) { return(innerHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState)); }; EndInvokeVoidDelegate <IHttpAsyncHandler> endDelegate = delegate(IAsyncResult asyncResult, IHttpAsyncHandler innerHandler) { innerHandler.EndProcessRequest(asyncResult); }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, httpAsyncHandler, _processRequestTag)); } else { // synchronous handler Action action = delegate { httpHandler.ProcessRequest(HttpContext.Current); }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag)); } }
protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) { if (DisableAsyncSupport) { // For backwards compat, we can disallow async support and just chain to the sync Execute() function. Action action = () => { Execute(requestContext); }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeTag)); } else { if (requestContext == null) { throw new ArgumentNullException("requestContext"); } // Support Asynchronous behavior. // Execute/ExecuteCore are no longer called. VerifyExecuteCalledOnce(); Initialize(requestContext); return(AsyncResultWrapper.Begin(callback, state, BeginExecuteCore, EndExecuteCore, _executeTag)); } }
public void Begin_ReturnsAsyncResultWhichWrapsInnerResult() { // Arrange MockAsyncResult innerResult = new MockAsyncResult() { AsyncState = "inner state", CompletedSynchronously = true, IsCompleted = true }; using (innerResult) { // Act IAsyncResult outerResult = AsyncResultWrapper.Begin <object>( null, "outer state", (callback, callbackState, state) => innerResult, (ar, state) => { }, null); // Assert Assert.Equal(innerResult.AsyncState, outerResult.AsyncState); Assert.Null(outerResult.AsyncWaitHandle); Assert.Equal(innerResult.CompletedSynchronously, outerResult.CompletedSynchronously); Assert.Equal(innerResult.IsCompleted, outerResult.IsCompleted); } }
protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { IHttpHandler httpHandler = GetHttpHandler(httpContext); IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler; if (httpAsyncHandler != null) { // asynchronous handler BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { return(httpAsyncHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState)); }; EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) { httpAsyncHandler.EndProcessRequest(asyncResult); }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _processRequestTag)); } else { // synchronous handler Action action = delegate { httpHandler.ProcessRequest(HttpContext.Current); }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag)); } }
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); } }
protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { return(SecurityUtil.ProcessInApplicationTrust(() => { IController controller; IControllerFactory factory; ProcessRequestInit(httpContext, out controller, out factory); IAsyncController asyncController = controller as IAsyncController; if (asyncController != null) { // asynchronous controller BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { try { return asyncController.BeginExecute(RequestContext, asyncCallback, asyncState); } catch { factory.ReleaseController(asyncController); throw; } }; EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) { try { asyncController.EndExecute(asyncResult); } finally { factory.ReleaseController(asyncController); } }; SynchronizationContext syncContext = SynchronizationContextUtil.GetSynchronizationContext(); AsyncCallback newCallback = AsyncUtil.WrapCallbackForSynchronizedExecution(callback, syncContext); return AsyncResultWrapper.Begin(newCallback, state, beginDelegate, endDelegate, _processRequestTag); } else { // synchronous controller Action action = delegate { try { controller.Execute(RequestContext); } finally { factory.ReleaseController(controller); } }; return AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag); } })); }
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" ); } }
protected virtual IAsyncResult BeginExecute( RequestContext requestContext, AsyncCallback callback, object state ) { if (DisableAsyncSupport) { // For backwards compat, we can disallow async support and just chain to the sync Execute() function. Action action = () => { Execute(requestContext); }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeTag)); } else { if (requestContext == null) { throw new ArgumentNullException("requestContext"); } // Support Asynchronous behavior. // Execute/ExecuteCore are no longer called. VerifyExecuteCalledOnce(); Initialize(requestContext); // Ensure delegates continue to use the C# Compiler static delegate caching optimization. BeginInvokeDelegate <Controller> beginDelegate = ( AsyncCallback asyncCallback, object callbackState, Controller controller ) => { return(controller.BeginExecuteCore(asyncCallback, callbackState)); }; EndInvokeVoidDelegate <Controller> endDelegate = ( IAsyncResult asyncResult, Controller controller ) => { controller.EndExecuteCore(asyncResult); }; return(AsyncResultWrapper.Begin( callback, state, beginDelegate, endDelegate, this, _executeTag )); } }
protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) { if (requestContext == null) { throw new ArgumentNullException("requestContext"); } VerifyExecuteCalledOnce(); Initialize(requestContext); return(AsyncResultWrapper.Begin(callback, state, BeginExecuteCore, EndExecuteCore, _executeTag)); }
public void Begin_WithCallbackSyncContext_ThrowsSynchronous() { // Arrange InvalidOperationException exception = new InvalidOperationException( "Some exception text." ); CapturingSynchronizationContext capturingSyncContext = new CapturingSynchronizationContext(); MockAsyncResult asyncResult = new MockAsyncResult() { CompletedSynchronously = true, IsCompleted = true }; using (asyncResult) { bool originalCallbackCalled = false; IAsyncResult passedAsyncResult = null; AsyncCallback originalCallback = ar => { passedAsyncResult = ar; originalCallbackCalled = true; throw exception; }; // Act & Assert InvalidOperationException thrownException = Assert.Throws <InvalidOperationException>( delegate { AsyncResultWrapper.Begin <object>( originalCallback, null, (callback, callbackState, state) => { asyncResult.AsyncState = callbackState; return(asyncResult); }, (ar, state) => { }, null, callbackSyncContext: capturingSyncContext ); }, exception.Message ); // Assert Assert.Equal(exception, thrownException); Assert.True(originalCallbackCalled); Assert.True(passedAsyncResult.CompletedSynchronously); Assert.False(capturingSyncContext.SendCalled); } }
protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) { // 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. PossiblyLoadTempData(); try { string actionName = RouteData.GetRequiredString("action"); IActionInvoker invoker = ActionInvoker; IAsyncActionInvoker asyncInvoker = invoker as IAsyncActionInvoker; if (asyncInvoker != null) { // asynchronous invocation // Ensure delegates continue to use the C# Compiler static delegate caching optimization. BeginInvokeDelegate <ExecuteCoreState> beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, ExecuteCoreState innerState) { return(innerState.AsyncInvoker.BeginInvokeAction(innerState.Controller.ControllerContext, innerState.ActionName, asyncCallback, asyncState)); }; EndInvokeVoidDelegate <ExecuteCoreState> endDelegate = delegate(IAsyncResult asyncResult, ExecuteCoreState innerState) { if (!innerState.AsyncInvoker.EndInvokeAction(asyncResult)) { innerState.Controller.HandleUnknownAction(innerState.ActionName); } }; ExecuteCoreState executeState = new ExecuteCoreState() { Controller = this, AsyncInvoker = asyncInvoker, ActionName = actionName }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, executeState, _executeCoreTag)); } else { // synchronous invocation Action action = () => { if (!invoker.InvokeAction(ControllerContext, actionName)) { HandleUnknownAction(actionName); } }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeCoreTag)); } } catch { PossiblySaveTempData(); throw; } }
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); }
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"); }
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_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."); }
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"); }
protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) { // 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. PossiblyLoadTempData(); try { string actionName = RouteData.GetRequiredString("action"); IActionInvoker invoker = ActionInvoker; IAsyncActionInvoker asyncInvoker = invoker as IAsyncActionInvoker; if (asyncInvoker != null) { // asynchronous invocation BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { return(asyncInvoker.BeginInvokeAction(ControllerContext, actionName, asyncCallback, asyncState)); }; EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) { if (!asyncInvoker.EndInvokeAction(asyncResult)) { HandleUnknownAction(actionName); } }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeCoreTag)); } else { // synchronous invocation Action action = () => { if (!invoker.InvokeAction(ControllerContext, actionName)) { HandleUnknownAction(actionName); } }; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeCoreTag)); } } catch { PossiblySaveTempData(); throw; } }
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."); } }
public void Begin_SynchronousCompletion() { // Arrange IAsyncResult resultGivenToCallback = null; IAsyncResult innerResult = new MockAsyncResult(); // Act IAsyncResult outerResult = AsyncResultWrapper.Begin( ar => { resultGivenToCallback = ar; }, null, (callback, state) => { callback(innerResult); return(innerResult); }, ar => { }); // Assert Assert.AreEqual(outerResult, resultGivenToCallback); }
public void Begin_ReturnsAsyncResultWhichWrapsInnerResult() { // Arrange IAsyncResult innerResult = new MockAsyncResult() { AsyncState = "inner state", CompletedSynchronously = true, IsCompleted = true }; // Act IAsyncResult outerResult = AsyncResultWrapper.Begin(null, "outer state", (callback, state) => innerResult, ar => { }); // Assert Assert.AreEqual(innerResult.AsyncState, outerResult.AsyncState); Assert.AreEqual(innerResult.AsyncWaitHandle, outerResult.AsyncWaitHandle); Assert.AreEqual(innerResult.CompletedSynchronously, outerResult.CompletedSynchronously); Assert.AreEqual(innerResult.IsCompleted, outerResult.IsCompleted); }
public void Begin_AsynchronousButAlreadyCompleted() { // Arrange Mock <IAsyncResult> innerResultMock = new Mock <IAsyncResult>(); innerResultMock.Setup(ir => ir.CompletedSynchronously).Returns(false); innerResultMock.Setup(ir => ir.IsCompleted).Returns(true); // Act IAsyncResult outerResult = AsyncResultWrapper.Begin( null, null, (callback, state) => { callback(innerResultMock.Object); return(innerResultMock.Object); }, ar => { }); // Assert Assert.True(outerResult.CompletedSynchronously); }
protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { BeginInvokeDelegate delegate4 = null; EndInvokeDelegate delegate5 = null; Action action2 = null; IHttpHandler httpHandler = GetHttpHandler(httpContext); IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler; if (httpAsyncHandler != null) { if (delegate4 == null) { delegate4 = (asyncCallback, asyncState) => httpAsyncHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState); } BeginInvokeDelegate beginDelegate = delegate4; if (delegate5 == null) { delegate5 = delegate(IAsyncResult asyncResult) { httpAsyncHandler.EndProcessRequest(asyncResult); }; } EndInvokeDelegate endDelegate = delegate5; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _processRequestTag)); } if (action2 == null) { action2 = delegate { httpHandler.ProcessRequest(HttpContext.Current); } } ; Action action = action2; return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag)); }
protected internal virtual IAsyncResult BeginProcessRequest( HttpContextBase httpContext, AsyncCallback callback, object state ) { IController controller; IControllerFactory factory; ProcessRequestInit(httpContext, out controller, out factory); IAsyncController asyncController = controller as IAsyncController; if (asyncController != null) { // asynchronous controller // Ensure delegates continue to use the C# Compiler static delegate caching optimization. BeginInvokeDelegate <ProcessRequestState> beginDelegate = delegate( AsyncCallback asyncCallback, object asyncState, ProcessRequestState innerState ) { try { return(innerState.AsyncController.BeginExecute( innerState.RequestContext, asyncCallback, asyncState )); } catch { innerState.ReleaseController(); throw; } }; EndInvokeVoidDelegate <ProcessRequestState> endDelegate = delegate( IAsyncResult asyncResult, ProcessRequestState innerState ) { try { innerState.AsyncController.EndExecute(asyncResult); } finally { innerState.ReleaseController(); } }; ProcessRequestState outerState = new ProcessRequestState() { AsyncController = asyncController, Factory = factory, RequestContext = RequestContext }; SynchronizationContext callbackSyncContext = SynchronizationContextUtil.GetSynchronizationContext(); return(AsyncResultWrapper.Begin( callback, state, beginDelegate, endDelegate, outerState, _processRequestTag, callbackSyncContext: callbackSyncContext )); } else { // synchronous controller Action action = delegate { try { controller.Execute(RequestContext); } finally { factory.ReleaseController(controller); } }; return(AsyncResultWrapper.BeginSynchronous( callback, state, action, _processRequestTag )); } }