Example #1
0
        public void SyncExceptionHandlingWithoutAwait()
        {
            Exception exceptionThrown = null;

            try
            {
                SyncMethods.ThrowAnExceptionAfterOneSecond(1);
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown);
        }
Example #2
0
        public void AwaitedAsyncWrappedInNonAwaitedAction()
        {
            Exception exceptionThrown = null;
            Action    willThrow       = async() => await SyncMethods.ThrowAnExceptionAfterOneSecond(1);

            try
            {
                willThrow();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown);
        }
Example #3
0
        public async Task SyncExceptionHandlingWithAwait()
        {
            Exception exceptionThrown = null;

            try
            {
                await SyncMethods.ThrowAnExceptionAfterOneSecond(1);
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf <AggregateException>(exceptionThrown);
        }
Example #4
0
        public async Task AwaitedAsyncWrappedInNonAwaitedFuncOfTask()
        {
            Exception   exceptionThrown = null;
            Func <Task> willThrow       = async() => await SyncMethods.ThrowAnExceptionAfterOneSecond(1);

            try
            {
                //awaitable because it's a func
                await willThrow();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown);
        }
Example #5
0
        public async Task FindingMyException()
        {
            Exception exceptionThrown = null;
            var       delay           = Task.Delay(TimeSpan.FromSeconds(0.5));
            var       errorProneTask  = SyncMethods
                                        .ThrowAnExceptionAfterOneSecond(1)
                                        .ContinueWith(task => exceptionThrown = task.Exception);

            var firstTaskToComplete = await Task.WhenAny(delay, errorProneTask);

            Assert.AreEqual(delay, firstTaskToComplete);

            //give the error prone task a chance to fail.
            await Task.Delay(1);

            Assert.IsNotNull(exceptionThrown);
        }
Example #6
0
        public async Task LosingMyException()
        {
            Exception exceptionThrown = null;
            var       delay           = Task.Delay(TimeSpan.FromSeconds(0.5));
            var       errorProneTask  = SyncMethods.ThrowAnExceptionAfterOneSecond(1);

            try
            {
                var firstTaskToComplete = await Task.WhenAny(delay, errorProneTask);

                Assert.AreEqual(delay, firstTaskToComplete);

                //give the error prone task a chance to fail.
                await Task.Delay(1);
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown, "Exception wont be set because my code "
                             + "on the calling thread left the try catch block");
        }