Example #1
0
        public void TryEnter()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall  = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall  = gate.TryEnter();

            // Assert
            Assert.IsTrue(firstCall, "TryEnter() should return TRUE on first call.");
            Assert.IsFalse(secondCall, "TryEnter() should return FALSE on each subsequent call.");
            Assert.IsFalse(thirdCall, "TryEnter() should return FALSE on each subsequent call.");
        }
        public void TryEnterShouldBeTrueForFirstCallAndFalseForSubsequentCalls()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall = gate.TryEnter();

            // Assert
            Assert.True(firstCall);
            Assert.False(secondCall);
            Assert.False(thirdCall);
        }
Example #3
0
        public void TryEnterShouldBeTrueForFirstCallAndFalseForSubsequentCalls()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall  = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall  = gate.TryEnter();

            // Assert
            Assert.True(firstCall);
            Assert.False(secondCall);
            Assert.False(thirdCall);
        }
 protected void VerifyExecuteCalledOnce()
 {
     if (!_executeGate.TryEnter())
     {
         throw Error.ControllerCannotHandleMultipleRequests(GetType());
     }
 }
Example #5
0
            private void HandleTimeout(object state)
            {
                if (!setResultGate.TryEnter())
                {
                    return;
                }

                taskCompletionSource.SetResult(TimeoutResult);

                if (callback != null)
                {
                    callback(sourceId);
                }

                timer.Dispose();
            }
        public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state) {
            if (controllerContext == null) {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null) {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager asyncHelper = GetAsyncManager(controllerContext.Controller);
            SingleEntryGate setupCompletedGate = new SingleEntryGate();

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult() {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos = SetupMethod.GetParameters();
                var rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                              select ExtractParameterFromDictionary(parameterInfo, parameters, SetupMethod);
                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedGate.TryEnter()) {
                        ThreadPool.QueueUserWorkItem(o => {
                            asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(SetupMethod);
                asyncHelper.OutstandingOperations.Increment();
                setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                asyncHelper.OutstandingOperations.Decrement();
                return asyncResult;
            };

            AsyncCallback<object> endCallback = ar => {
                if (setupCompletedGate.TryEnter()) {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                // Get parameters for action method, then execute
                ParameterInfo[] completionParametersInfos = CompletionMethod.GetParameters();
                var rawCompletionParameterValues = from parameterInfo in completionParametersInfos
                                                   select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncHelper.Parameters);
                object[] completionParametersArray = rawCompletionParameterValues.ToArray();

                MethodDispatcher completionDispatcher = DispatcherCache.GetDispatcher(CompletionMethod);
                object actionReturnValue = completionDispatcher.Execute(controllerContext.Controller, completionParametersArray);
                return actionReturnValue;
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;
            return AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag);
        }
Example #7
0
 internal void VerifyExecuteCalledOnce()
 {
     if (!_executeWasCalledGate.TryEnter())
     {
         string message = String.Format(CultureInfo.CurrentUICulture, MvcResources.ControllerBase_CannotHandleMultipleRequests, GetType());
         throw new InvalidOperationException(message);
     }
 }
Example #8
0
        private void ExecuteAsynchronousCallback(bool timedOut)
        {
            WaitForBeginToCompleteAndDestroyTimer();

            if (_handleCallbackGate.TryEnter())
            {
                _timedOut = timedOut;
                _originalCallback?.Invoke(this);
            }
        }
 public TResult End()
 {
     if (!_endExecutedGate.TryEnter())
     {
         throw new InvalidOperationException(); // Error.AsyncCommon_AsyncResultAlreadyConsumed();
     }
     if (_timedOut)
     {
         throw new TimeoutException();
     }
     WaitForBeginToCompleteAndDestroyTimer();
     return(_endDelegate(this._innerAsyncResult));
 }
Example #10
0
            private void ExecuteAsynchronousCallback(bool timedOut)
            {
                DestroyTimer();

                if (_handleCallbackGate.TryEnter())
                {
                    _timedOut = timedOut;

                    if (_originalCallback != null)
                    {
                        _originalCallback(this);
                    }
                }
            }
Example #11
0
        public TResult End()
        {
            if (!_endExecutedGate.TryEnter())
            {
                throw new InvalidOperationException("The provided IAsyncResult has already been consumed.");
            }

            if (_timedOut)
            {
                throw new TimeoutException();
            }
            WaitForBeginToCompleteAndDestroyTimer();

            return(_endDelegate(_innerAsyncResult));
        }
Example #12
0
            public TResult End()
            {
                if (!_endExecutedGate.TryEnter())
                {
                    throw Error.AsyncResultAlreadyConsumed();
                }

                if (_timedOut)
                {
                    throw Error.AsyncTimeout();
                }

                DestroyTimer();

                lock (_delegateLock)
                {
                }
                return(_endDelegate(_innerAsyncResult));
            }