コード例 #1
0
        public void MethodReturnsATaskOfTAndMutatesGlobalState()
        {
            const int expected = 5;

            SyncMethods.MutateGlobalState();

            Assert.AreEqual(expected, MutableGlobalState.State);
        }
コード例 #2
0
        public void UsingAResultLooksFineWithTaskCompletionSource()
        {
            // what is the problem with this (only relevant with certain sync contexts)
            const string expected = "foo";

            var actual = SyncMethods
                         .WaitASecondAndReturnAValueUsingTaskCompletionSource(expected)
                         .Result;

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void UsingResultLooksFineSoWhatsTheProblem()
        {
            // what is the problem with this (only relevant with certain sync contexts)
            const string expected = "foo";

            var actual = SyncMethods
                         .WaitASecondAndReturnAValue(expected)
                         .Result;

            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
        public void SyncExceptionHandlingWithoutAwait()
        {
            Exception exceptionThrown = null;

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

            try
            {
                willThrow();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            Assert.IsNotNull(exceptionThrown);
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
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");
        }