Example #1
0
        public async Task LockTest()
        {
            var asyncLock = new AsyncLock();

            var       opActive = false;
            const int time     = 200;
            const int timeInc  = 10;
            const int count    = 10;

            async Task Op(int num)
            {
                using (await asyncLock.AcquireAsync())
                {
                    Assert.IsFalse(opActive);
                    opActive = true;
                    await TaskEx.Delay(200 + num *timeInc);

                    Assert.IsTrue(opActive);
                    opActive = false;
                }
            }

            var sw = Stopwatch.StartNew();
            await Enumerable
            .Range(0, 10)
            .Select(i => TaskEx.Run(() => Op(i)))
            .WhenAll();

            sw.Stop();
            Assert.IsFalse(opActive);
            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, time * count + timeInc * count / 2);
        }
 public void TwoAsserts_BothAssertsFail_Async()
 {
     Assert.Multiple(async() =>
     {
         await Task.Delay(100);
         Assert.That(complex.RealPart, Is.EqualTo(5.0), "RealPart");
         Assert.That(complex.ImaginaryPart, Is.EqualTo(4.2), "ImaginaryPart");
     });
 }
 public void ThreeAssertsSucceed_Async()
 {
     Assert.Multiple(async() =>
     {
         await Task.Delay(100);
         Assert.That(2 + 2, Is.EqualTo(4));
         Assert.That(complex.RealPart, Is.EqualTo(5.2));
         Assert.That(complex.ImaginaryPart, Is.EqualTo(3.9));
     });
 }
Example #4
0
        /// <summary>
        /// Executes a provided <see cref="Task"/>, cancelling the task and executing a fallback action if the Task doesn't complete before the provided timeout.
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="taskSelector"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static async Task Timeout(CancellationToken ct, Func <CancellationToken, Task> taskSelector, TimeSpan timeout, Action onTimedOut)
        {
            // We're not using CancellationTokenSource's timeout support because we want to be able to trace code and know when it's
            // about to be cancelled because of a timeout.
            using (var tokenSource = new CancellationTokenSource())
            {
                var task = taskSelector(tokenSource.Token);

                if ((await StaticTask.WhenAny(task, StaticTask.Delay(timeout, ct))) != task)
                {
                    // We timed out, cancel the task and throw a relevant exception.
                    tokenSource.Cancel();
                    onTimedOut();
                }
            }
        }
        public void TwoNestedBlocks_TwoAssertsFail_Async()
        {
            Assert.Multiple(() =>
            {
                Assert.Multiple(async() =>
                {
                    await Task.Delay(100);
                    Assert.That(2 + 2, Is.EqualTo(5));
                });

                Assert.Multiple(async() =>
                {
                    await Task.Delay(100);
                    Assert.That(complex.RealPart, Is.EqualTo(5.2), "RealPart");
                    Assert.That(complex.ImaginaryPart, Is.EqualTo(4.2), "ImaginaryPart");
                });
            });
        }
Example #6
0
 private static async Task <bool> TryTakeAndHold(
     [NotNull] AsyncLock asyncLock, TimeSpan holdTime, CancellationToken cancellation = default(CancellationToken), Action callback = null)
 {
     try
     {
         using (await asyncLock.AcquireAsync(holdTime, cancellation))
         {
             callback?.Invoke();
             await TaskEx.Delay(holdTime);
         }
         return(true);
     }
     catch (OperationCanceledException)
     {
         return(false);
     }
     catch (TimeoutException)
     {
         return(false);
     }
 }
Example #7
0
        public async Task LockCancellationTest()
        {
            var asyncLock = new AsyncLock();
            var holdTime  = TimeSpan.FromSeconds(2);
            var delayTime = TimeSpan.FromMilliseconds(200);

            var lock1Started = new ManualResetEventSlim(false);

            var lock1 = TryTakeAndHold(asyncLock, holdTime, callback: () => lock1Started.Set());

            lock1Started.Wait();

            var cts2  = new CancellationTokenSource();
            var sw2   = Stopwatch.StartNew();
            var lock2 = TryTakeAndHold(asyncLock, holdTime, cts2.Token);
            await TaskEx.Delay(delayTime);

            cts2.Cancel();
            var lock2Taken = await lock2;

            sw2.Stop();

            var sw3   = Stopwatch.StartNew();
            var lock3 = TryTakeAndHold(asyncLock, delayTime);
            await TaskEx.Delay(delayTime);

            var lock3Taken = await lock3;

            sw3.Stop();

            var lock1Taken = await lock1;

            Assert.IsTrue(lock1Taken);
            Assert.IsFalse(lock2Taken);
            Assert.Less(sw2.Elapsed, holdTime - delayTime);
            Assert.IsFalse(lock3Taken);
            Assert.Less(sw3.Elapsed, holdTime - delayTime);
        }
Example #8
0
        public static async System.Threading.Tasks.Task WarningAfterAwaitTaskDelay()
        {
            await Task.Delay(1);

            Assert.Warn("(Warning message)");
        }
Example #9
0
 public async System.Threading.Tasks.Task AsyncTaskTestCase(int x)
 {
     await Task.Delay(0);
 }
Example #10
0
 public async void AsyncVoidTestCase(int x)
 {
     await Task.Delay(0);
 }
Example #11
0
 public System.Threading.Tasks.Task NonAsyncTask()
 {
     return(Task.Delay(0));
 }
Example #12
0
 public async System.Threading.Tasks.Task AsyncTask()
 {
     await Task.Delay(1);
 }
Example #13
0
 public async void AsyncVoid()
 {
     await Task.Delay(1); // To avoid warning message
 }
Example #14
0
 public System.Threading.Tasks.Task TaskTestCase(int x)
 {
     return(Task.Delay(0));
 }