Beispiel #1
0
 public void TaskUtilitiesTests_RunConcurrentlyTest()
 {
     using (var verify = new Verify())
     {
         Sleeper(verify).RunConcurrently();
     }
 }
Beispiel #2
0
 public async Task TaskUtilitiesTests_ForEachAsync_ListT_2()
 {
     using (var verify = new Verify())
     {
         var items = Enumerable.Range(0, 10).ToList();
         await items.ForEachAsync(i => NumericSleeperManualStart(verify, i));
     }
 }
Beispiel #3
0
        public async Task TaskUtilitiesTests_ForEachAsync_ListAction_2()
        {
            using (var verify = new Verify())
            {
                var actions = new List<Func<Task>>();
                actions.Add(() => SleeperManualStart(verify));
                actions.Add(() => SleeperManualStart(verify));
                actions.Add(() => SleeperManualStart(verify));
                actions.Add(() => SleeperManualStart(verify));
                actions.Add(() => SleeperManualStart(verify));

                await actions.ForEachAsync();
            }
        }
Beispiel #4
0
 public void RandomExtended_Choose()
 {
     using (var verify = new Verify())
     {
         var rand = new RandomExtended(0);
         var source = new List<int>();
         for (int i = 100; i < 200; i++)
             source.Add(i);
         for (int i = 0; i < 100; i++)
         {
             var item = rand.Choose(source);
             verify.IsBetween(100, item, 199, "Item couldn't be in the sourse list");
             verify.IsTrue(source.Contains(item), "Item should have been left in the list");
         }
     }
 }
Beispiel #5
0
 public void RandomExtended_Choose3()
 {
     using (var verify = new Verify())
     {
         var rand = new RandomExtended(0);
         var source = new List<int>();
         for (int i = 100; i < 200; i++)
             source.Add(i);
         for (int i = 0; i < 100; i += 10)
         {
             var items = rand.Choose(source, 10, false);
             foreach (var item in items)
             {
                 verify.IsBetween(100, item, 199, "Item couldn't be in the source list");
                 verify.IsTrue(source.Contains(item), "Item should have been left in the list");
             }
             var distinct = items.Distinct().Count();
             verify.AreEqual(items.Count, distinct, "There shouldn't have been any duplicates");
         }
     }
 }
Beispiel #6
0
 public void TaskUtilitiesTests_AutoCancelingTaskTest2()
 {
     using (var verify = new Verify())
     {
         var timer = Stopwatch.StartNew();
         var t = TaskUtilities.AutoCancelingTask(1000);
         Memory.CycleGC();
         var result = t.WaitForCompleteOrCancel();
         timer.Stop();
         verify.IsFalse(result, "WaitForCompleteOrCancel should have returned false");
         verify.IsTrue(t.IsCanceled, "Task should have been canceled.");
         verify.AreEqual(1.0, timer.Elapsed.TotalSeconds, 0.05, "Elapsed time was incorrect.");
     }
 }
Beispiel #7
0
        public async Task TaskUtilitiesTests_WhenAllCancelTest2()
        {

            using (var verify = new Verify())
            {
                try
                {
                    var tcs1 = new TaskCompletionSource<object>();
                    var tcs2 = new TaskCompletionSource<object>();
                    var task1 = tcs1.Task;
                    var task2 = tcs2.Task;
                    var list = new List<Task>() { task1, task2 };
                    using (var cs = new CancellationTokenSource(100))
                    {
                        var ct = cs.Token;

                        await list.WhenAll(ct);
                    }
                    verify.Fail("Error expected");
                }
                catch (OperationCanceledException)
                {
                    //Sucess
                }
            }
        }
Beispiel #8
0
        public async Task TaskUtilitiesTests_WhenAllCancelTest1()
        {
            using (var verify = new Verify())
            {
                var task1 = Task.Delay(TimeSpan.FromMilliseconds(100));
                var task2 = Task.Delay(TimeSpan.FromMilliseconds(125));
                var task3 = Task.Delay(TimeSpan.FromMilliseconds(150));
                var list = new List<Task>() { task1, task2, task3 };
                using (var cs = new CancellationTokenSource())
                {
                    var ct = cs.Token;

                    await list.WhenAll(ct);
                }
                verify.IsTrueForAll(list, t => t.IsCompleted, "All tasks should have been completed.");
            }
        }
Beispiel #9
0
 public void RandomExtended_Pick2()
 {
     using (var verify = new Verify())
     {
         var rand = new RandomExtended(0);
         var source = new List<int>();
         for (int i = 100; i < 200; i++)
             source.Add(i);
         for (int i = 0; i < 100; i += 10)
         {
             var items = rand.Pick(source, 10);
             foreach (var item in items)
             {
                 verify.IsBetween(100, item, 199, "Item couldn't be in the source list");
                 verify.IsFalse(source.Contains(item), "Item should have been removed from list");
             }
         }
         Assert.AreEqual(0, source.Count, "List should have been left empty");
     }
 }
Beispiel #10
0
 static async Task Sleeper(Verify verify)
 {
     verify.WriteLine("Before");
     await Task.Delay(1000);
     verify.WriteLine("After");
 }
Beispiel #11
0
 public void TaskUtilitiesTests_RunConcurrently()
 {
     using (var verify = new Verify())
     {
         var task = new Task(() => Thread.Sleep(100));
         task.RunConcurrently();
     }
 }
Beispiel #12
0
 static Task NumericSleeperManualStart(Verify verify, int i)
 {
     return new Task(() =>
     {
         verify.WriteLine("Before " + i);
         Thread.Sleep(1000);
         verify.WriteLine("After " + i);
     });
 }
Beispiel #13
0
 static Task SleeperManualStart(Verify verify)
 {
     return new Task(() =>
     {
         verify.WriteLine("Before");
         Thread.Sleep(1000);
         verify.WriteLine("After");
     });
 }
Beispiel #14
0
 static async Task NumericSleeper(Verify verify, int i)
 {
     verify.WriteLine("Before " + i);
     await Task.Delay(1000);
     verify.WriteLine("After " + i);
 }
Beispiel #15
0
        public void RandomExtended_DateTime()
        {
            using (var verify = new Verify())
            {
                var rand = new RandomExtended(0);
                var minValue = new DateTime(2001, 1, 1);
                var maxValue = new DateTime(2010, 12, 31);

                for (int i = 0; i < LoopLimit; i++)
                {
                    var result = rand.NextDateTime(minValue, maxValue);
                    verify.IsBetween(minValue, result, maxValue, "random result not within expected range");
                }
            }
        }
Beispiel #16
0
 public async Task TaskUtilitiesTests_WhenAnyTest1()
 {
     using (var verify = new Verify())
     {
         var list = new List<Task> { Sleeper(verify), Sleeper(verify), Sleeper(verify) };
         await list.WhenAny();
     }
 }
Beispiel #17
0
 public async Task TaskUtilitiesTests_AutoCompletingTaskTest2()
 {
     using (var verify = new Verify())
     {
         var timer = Stopwatch.StartNew();
         var task = TaskUtilities.AutoCompletingTask(123, 1000);
         Memory.CycleGC();
         var result = await task;
         timer.Stop();
         verify.AreEqual(123, result, "Task result was wrong");
         verify.AreEqual(1.0, timer.Elapsed.TotalSeconds, 0.05, "Delay was incorrect");
     }
 }
Beispiel #18
0
        public void TaskUtilitiesTests_WaitForCancelTest1()
        {
            using (var verify = new Verify())
            {
                var task = Sleeper(verify);
                task.RunConcurrently();

                var result = task.WaitForCompleteOrCancel();

                verify.AreEqual(TaskStatus.RanToCompletion, task.Status, "The task should have been completed");
                verify.IsTrue(result, "Assertion should be true because the task was completed");
            }
        }
Beispiel #19
0
        public void TaskUtilitiesTests_WaitForCancelTest2()
        {
            using (var verify = new Verify())
            {
                var cts = new CancellationTokenSource(500);
                var task = LongSleeper(verify, cts.Token);
                task.RunConcurrently();

                var result = task.WaitForCompleteOrCancel();

                verify.AreEqual(TaskStatus.Canceled, task.Status, "The task should have been canceled");
                verify.IsFalse(result, "Assertion should be false because the task was canceled");
            }
        }
Beispiel #20
0
        public async Task TaskUtilitiesTests_WhenAnyCancelTest1()
        {
            using (var verify = new Verify())
            {
                var tcs1 = new TaskCompletionSource<object>();
                var tcs2 = new TaskCompletionSource<object>();
                var task1 = tcs1.Task;
                var task2 = tcs2.Task;
                var task3 = Task.Delay(TimeSpan.FromMilliseconds(100));
                var list = new List<Task>() { task1, task2, task3 };
                using (var cs = new CancellationTokenSource())
                {
                    var ct = cs.Token;

                    await list.WhenAny(ct);
                }
            }
        }
Beispiel #21
0
 static async Task LongSleeper(Verify verify, CancellationToken token)
 {
     verify.WriteLine("Before");
     for (var i = 0; i < 10; i++)
     {
         await Task.Delay(1000);
         token.ThrowIfCancellationRequested();
     }
     verify.WriteLine("After");
 }
Beispiel #22
0
        public void RandomExtended_Long()
        {
            using (var verify = new Verify())
            {
                var rand = new RandomExtended(0);
                var minValue = 1L;
                var maxValue = 100L;

                for (int i = 0; i < LoopLimit; i++)
                {
                    var result = rand.NextInt64(minValue, maxValue);
                    verify.IsBetween(minValue, result, maxValue, "random result not within expected range");
                }
            }
        }