public void SyncWithActionCapturesException()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException("Some exception text.");

            Mock <SynchronizationContext> mockSyncContext = new Mock <SynchronizationContext>();

            mockSyncContext
            .Setup(sc => sc.Send(It.IsAny <SendOrPostCallback>(), null))
            .Callback(
                delegate(SendOrPostCallback d, object state)
            {
                try
                {
                    d(state);
                }
                catch
                {
                    // swallow exceptions, just like AspNetSynchronizationContext
                }
            });

            // Act & assert
            SynchronousOperationException thrownException = Assert.Throws <SynchronousOperationException>(
                delegate { SynchronizationContextUtil.Sync(mockSyncContext.Object, () => { throw exception; }); },
                @"An operation that crossed a synchronization context failed. See the inner exception for more information.");

            Assert.Equal(exception, thrownException.InnerException);
        }
        public void ConstructorWithMessageParameter() {
            // Act
            SynchronousOperationException ex = new SynchronousOperationException("the message");

            // Assert
            Assert.AreEqual("the message", ex.Message);
        }
示例#3
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);
            }
        }
示例#4
0
        public void ConstructorWithMessageParameter()
        {
            // Act
            SynchronousOperationException ex = new SynchronousOperationException("the message");

            // Assert
            Assert.Equal("the message", ex.Message);
        }
        public void ConstructorWithMessageAndInnerExceptionParameter() {
            // Arrange
            Exception innerException = new Exception();

            // Act
            SynchronousOperationException ex = new SynchronousOperationException("the message", innerException);

            // Assert
            Assert.AreEqual("the message", ex.Message);
            Assert.AreEqual(innerException, ex.InnerException);
        }
示例#6
0
        public void ConstructorWithMessageAndInnerExceptionParameter()
        {
            // Arrange
            Exception innerException = new Exception();

            // Act
            SynchronousOperationException ex = new SynchronousOperationException("the message", innerException);

            // Assert
            Assert.Equal("the message", ex.Message);
            Assert.Equal(innerException, ex.InnerException);
        }
        public void TypeIsSerializable() {
            // Arrange
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            SynchronousOperationException ex = new SynchronousOperationException("the message", new Exception("inner exception"));

            // Act
            formatter.Serialize(ms, ex);
            ms.Position = 0;
            SynchronousOperationException deserialized = formatter.Deserialize(ms) as SynchronousOperationException;

            // Assert
            Assert.IsNotNull(deserialized, "Deserialization process did not return the exception.");
            Assert.AreEqual("the message", deserialized.Message);
            Assert.IsNotNull(deserialized.InnerException);
            Assert.AreEqual("inner exception", deserialized.InnerException.Message);
        }
示例#8
0
        public void TypeIsSerializable()
        {
            // Arrange
            MemoryStream    ms               = new MemoryStream();
            BinaryFormatter formatter        = new BinaryFormatter();
            SynchronousOperationException ex = new SynchronousOperationException("the message", new Exception("inner exception"));

            // Act
            formatter.Serialize(ms, ex);
            ms.Position = 0;
            SynchronousOperationException deserialized = formatter.Deserialize(ms) as SynchronousOperationException;

            // Assert
            Assert.NotNull(deserialized);
            Assert.Equal("the message", deserialized.Message);
            Assert.NotNull(deserialized.InnerException);
            Assert.Equal("inner exception", deserialized.InnerException.Message);
        }