public void RegisterTask_SynchronousCompletion()
        {
            // Arrange
            SimpleSynchronizationContext syncContext = new SimpleSynchronizationContext();
            AsyncManager asyncManager         = new AsyncManager(syncContext);
            bool         endDelegateWasCalled = false;

            Func <AsyncCallback, IAsyncResult> beginDelegate = callback =>
            {
                Assert.Equal(1, asyncManager.OutstandingOperations.Count);
                MockAsyncResult asyncResult = new MockAsyncResult(
                    true /* completedSynchronously */
                    );
                callback(asyncResult);
                return(asyncResult);
            };
            Action <IAsyncResult> endDelegate = delegate
            {
                endDelegateWasCalled = true;
            };

            // Act
            asyncManager.RegisterTask(beginDelegate, endDelegate);

            // Assert
            Assert.True(endDelegateWasCalled);
            Assert.False(syncContext.SendWasCalled);
            Assert.Equal(0, asyncManager.OutstandingOperations.Count);
        }
        public void RegisterTask_AsynchronousCompletion_SwallowsExceptionsThrownByEndDelegate()
        {
            // Arrange
            SimpleSynchronizationContext syncContext = new SimpleSynchronizationContext();
            AsyncManager asyncManager         = new AsyncManager(syncContext);
            bool         endDelegateWasCalled = false;

            using (ManualResetEvent waitHandle = new ManualResetEvent(false /* initialState */))
            {
                Func <AsyncCallback, IAsyncResult> beginDelegate = callback =>
                {
                    MockAsyncResult asyncResult = new MockAsyncResult(false /* completedSynchronously */);
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        callback(asyncResult);
                        waitHandle.Set();
                    });
                    return(asyncResult);
                };
                Action <IAsyncResult> endDelegate = delegate
                {
                    endDelegateWasCalled = true;
                    throw new Exception("This is a sample exception.");
                };

                // Act
                asyncManager.RegisterTask(beginDelegate, endDelegate);
                waitHandle.WaitOne();

                // Assert
                Assert.True(endDelegateWasCalled);
                Assert.Equal(0, asyncManager.OutstandingOperations.Count);
            }
        }
        public void RegisterTask_ResetsOutstandingOperationCountIfBeginMethodThrows()
        {
            // Arrange
            SimpleSynchronizationContext syncContext = new SimpleSynchronizationContext();
            AsyncManager asyncManager = new AsyncManager(syncContext);

            Func <AsyncCallback, IAsyncResult> beginDelegate = cb =>
            {
                throw new InvalidOperationException("BeginDelegate throws.");
            };
            Action <IAsyncResult> endDelegate = ar =>
            {
                Assert.True(false, "This should never be called.");
            };

            // Act & assert
            Assert.Throws <InvalidOperationException>(
                delegate
            {
                asyncManager.RegisterTask(beginDelegate, endDelegate);
            },
                "BeginDelegate throws."
                );

            Assert.Equal(0, asyncManager.OutstandingOperations.Count);
        }
예제 #4
0
        public void RegisterTaskWithAction()
        {
            // Arrange
            Func <int>    numCallsFunc;
            AsyncManager  helper         = GetAsyncManagerForRegisterTask(out numCallsFunc);
            AsyncCallback storedCallback = null;

            int opCountDuringBeginDelegate = 0;

            Action <AsyncCallback> beginDelegate = innerCb => {
                storedCallback             = innerCb;
                opCountDuringBeginDelegate = helper.OutstandingOperations.Count;
            };

            // Act
            int opCountBeforeBeginDelegate = helper.OutstandingOperations.Count;

            helper.RegisterTask(beginDelegate, null /* endDelegate */);
            storedCallback(null);
            int opCountAfterEndDelegate = helper.OutstandingOperations.Count;

            // Assert
            Assert.AreEqual(0, opCountBeforeBeginDelegate);
            Assert.AreEqual(1, opCountDuringBeginDelegate);
            Assert.AreEqual(0, opCountAfterEndDelegate);
            Assert.AreEqual(1, numCallsFunc(), "Send() was not called.");
        }
예제 #5
0
        public void RegisterTask_AsynchronousCompletion()
        {
            // Arrange
            SimpleSynchronizationContext syncContext = new SimpleSynchronizationContext();
            AsyncManager asyncManager         = new AsyncManager(syncContext);
            bool         endDelegateWasCalled = false;

            ManualResetEvent waitHandle = new ManualResetEvent(false /* initialState */);

            Func <AsyncCallback, IAsyncResult> beginDelegate = callback => {
                Assert.AreEqual(1, asyncManager.OutstandingOperations.Count, "Counter was not incremented properly.");
                MockAsyncResult asyncResult = new MockAsyncResult(false /* completedSynchronously */);
                ThreadPool.QueueUserWorkItem(_ => {
                    Assert.AreEqual(1, asyncManager.OutstandingOperations.Count, "Counter shouldn't have been decremented yet.");
                    callback(asyncResult);
                    waitHandle.Set();
                });
                return(asyncResult);
            };
            Action <IAsyncResult> endDelegate = delegate { endDelegateWasCalled = true; };

            // Act
            asyncManager.RegisterTask(beginDelegate, endDelegate);
            waitHandle.WaitOne();

            // Assert
            Assert.IsTrue(endDelegateWasCalled);
            Assert.IsTrue(syncContext.SendWasCalled, "Asynchronous call to End() should have been routed through SynchronizationContext.Send()");
            Assert.AreEqual(0, asyncManager.OutstandingOperations.Count, "Counter was not decremented properly.");
        }
예제 #6
0
        public void RegisterTaskWithActionThrowsIfBeginDelegateIsNull()
        {
            // Arrange
            AsyncManager helper = GetAsyncManagerForRegisterTask();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                helper.RegisterTask((Action <AsyncCallback>)null, null);
            }, "beginDelegate");
        }
예제 #7
0
        public void RegisterTask_SynchronousCompletion()
        {
            // Arrange
            SimpleSynchronizationContext syncContext = new SimpleSynchronizationContext();
            AsyncManager asyncManager         = new AsyncManager(syncContext);
            bool         endDelegateWasCalled = false;

            Func <AsyncCallback, IAsyncResult> beginDelegate = callback => {
                Assert.AreEqual(1, asyncManager.OutstandingOperations.Count, "Counter was not incremented properly.");
                MockAsyncResult asyncResult = new MockAsyncResult(true /* completedSynchronously */);
                callback(asyncResult);
                return(asyncResult);
            };
            Action <IAsyncResult> endDelegate = delegate { endDelegateWasCalled = true; };

            // Act
            asyncManager.RegisterTask(beginDelegate, endDelegate);

            // Assert
            Assert.IsTrue(endDelegateWasCalled);
            Assert.IsFalse(syncContext.SendWasCalled, "Synchronous call to End() should not have been routed through SynchronizationContext.Send()");
            Assert.AreEqual(0, asyncManager.OutstandingOperations.Count, "Counter was not decremented properly.");
        }
예제 #8
0
        public void RegisterTaskWithFunc()
        {
            // Arrange
            Func <int>      numCallsFunc;
            AsyncManager    helper         = GetAsyncManagerForRegisterTask(out numCallsFunc);
            MockAsyncResult asyncResult    = new MockAsyncResult();
            AsyncCallback   storedCallback = null;

            int opCountDuringBeginDelegate = 0;
            int opCountDuringEndDelegate   = 0;

            Func <AsyncCallback, IAsyncResult> beginDelegate = innerCb => {
                storedCallback             = innerCb;
                opCountDuringBeginDelegate = helper.OutstandingOperations.Count;
                return(asyncResult);
            };
            AsyncCallback endDelegate = ar => {
                Assert.AreEqual(asyncResult, ar);
                opCountDuringEndDelegate = helper.OutstandingOperations.Count;
            };

            // Act
            int          opCountBeforeBeginDelegate = helper.OutstandingOperations.Count;
            IAsyncResult returnedAsyncResult        = helper.RegisterTask(beginDelegate, endDelegate);

            storedCallback(returnedAsyncResult);
            int opCountAfterEndDelegate = helper.OutstandingOperations.Count;

            // Assert
            Assert.AreEqual(asyncResult, returnedAsyncResult);
            Assert.AreEqual(0, opCountBeforeBeginDelegate);
            Assert.AreEqual(1, opCountDuringBeginDelegate);
            Assert.AreEqual(1, opCountDuringEndDelegate);
            Assert.AreEqual(0, opCountAfterEndDelegate);
            Assert.AreEqual(1, numCallsFunc(), "Send() was not called.");
        }