Esempio n. 1
0
        public void StressZeroTest()
        {
            ParallelTestHelper.Repeat(delegate {
                int times = 0;

                ParallelTestHelper.ParallelStressTest(snzi, (s) => {
                    int t = Interlocked.Increment(ref times);

                    for (int i = 0; i < 20; i++)
                    {
                        if (i % 2 == 0)
                        {
                            snzi.Increment();
                        }
                        else
                        {
                            snzi.Decrement();
                        }
                        if (i % (3 * t) == 0)
                        {
                            Thread.Sleep(0);
                        }
                    }
                });

                Assert.IsTrue(snzi.IsSet, "#1");
            });
        }
Esempio n. 2
0
        public void WaitChildTestCase()
        {
            ParallelTestHelper.Repeat(delegate {
                bool r1 = false, r2 = false, r3 = false;

                Task t = Task.Factory.StartNew(delegate {
                    Task.Factory.StartNew(delegate {
                        Thread.Sleep(50);
                        r1 = true;
                        //Console.WriteLine("finishing 1");
                    });
                    Task.Factory.StartNew(delegate {
                        Thread.Sleep(300);

                        r2 = true;
                        //Console.WriteLine("finishing 2");
                    });
                    Task.Factory.StartNew(delegate {
                        Thread.Sleep(150);

                        r3 = true;
                        //Console.WriteLine("finishing 3");
                    });
                });

                t.Wait();
                Assert.IsTrue(r2, "#1");
                Assert.IsTrue(r3, "#2");
                Assert.IsTrue(r1, "#3");
                Assert.AreEqual(TaskStatus.RanToCompletion, t.Status, "#4");
            }, 10);
        }
Esempio n. 3
0
        public void WaitAnyTest()
        {
            ParallelTestHelper.Repeat(delegate {
                int flag     = 0;
                int finished = 0;

                InitWithDelegate(delegate {
                    int times = Interlocked.Exchange(ref flag, 1);
                    if (times == 1)
                    {
                        SpinWait sw = new SpinWait();
                        while (finished == 0)
                        {
                            sw.SpinOnce();
                        }
                    }
                    else
                    {
                        Interlocked.Increment(ref finished);
                    }
                });

                int index = Task.WaitAny(tasks);

                Assert.IsTrue(flag == 1, "#1");
                Assert.AreEqual(1, finished, "#2");
                Assert.AreNotEqual(-1, index, "#3");

                Task.WaitAll(tasks);
            });
        }
        public static void AddStressTest(IProducerConsumerCollection <int> coll)
        {
            ParallelTestHelper.Repeat(delegate {
                int amount        = -1;
                const int count   = 10;
                const int threads = 5;

                ParallelTestHelper.ParallelStressTest(coll, (q) => {
                    int t = Interlocked.Increment(ref amount);
                    for (int i = 0; i < count; i++)
                    {
                        coll.TryAdd(t);
                    }
                }, threads);

                Assert.AreEqual(threads * count, coll.Count, "#-1");
                int[] values = new int[threads];
                int temp;
                while (coll.TryTake(out temp))
                {
                    values[temp]++;
                }

                for (int i = 0; i < threads; i++)
                {
                    Assert.AreEqual(count, values[i], "#" + i);
                }
            });
        }
Esempio n. 5
0
        public void ParallelForSmallRangeTest()
        {
            ParallelTestHelper.Repeat(() => {
                int test = -1;
                Parallel.For(0, 1, (i) => test = i);

                Assert.AreEqual(0, test, "#1");
            });
        }
Esempio n. 6
0
 public void WaitAllTest()
 {
     ParallelTestHelper.Repeat(delegate {
         int achieved = 0;
         InitWithDelegate(delegate { Interlocked.Increment(ref achieved); });
         Task.WaitAll(tasks);
         Assert.AreEqual(max, achieved, "#1");
     });
 }
Esempio n. 7
0
        public void ParallelForTestCase()
        {
            ParallelTestHelper.Repeat(() => {
                int[] expected = Enumerable.Range(1, 1000).Select((e) => e * 2).ToArray();
                int[] actual   = Enumerable.Range(1, 1000).ToArray();
                SpinWait sw    = new SpinWait();

                Parallel.For(0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce(); });

                CollectionAssert.AreEquivalent(expected, actual, "#1, same pixels");
                CollectionAssert.AreEqual(expected, actual, "#2, pixels in order");
            });
        }
Esempio n. 8
0
        public void ParallelForEachTestCase()
        {
            ParallelTestHelper.Repeat(() => {
                IEnumerable <int> e         = Enumerable.Repeat(1, 500);
                ConcurrentQueue <int> queue = new ConcurrentQueue <int> ();
                SpinWait sw = new SpinWait();
                int count   = 0;

                Parallel.ForEach(e, (element) => { Interlocked.Increment(ref count); queue.Enqueue(element); sw.SpinOnce(); });

                Assert.AreEqual(500, count, "#1");
                CollectionAssert.AreEquivalent(e, queue, "#2");
            });
        }
        public static void RemoveStressTest(IProducerConsumerCollection <int> coll, CheckOrderingType order)
        {
            ParallelTestHelper.Repeat(delegate {
                const int count   = 10;
                const int threads = 5;
                const int delta   = 5;

                for (int i = 0; i < (count + delta) * threads; i++)
                {
                    coll.TryAdd(i);
                }

                bool state = true;

                ParallelTestHelper.ParallelStressTest(coll, (q) => {
                    int t;
                    for (int i = 0; i < count; i++)
                    {
                        state &= coll.TryTake(out t);
                    }
                }, threads);

                Assert.IsTrue(state, "#1");
                Assert.AreEqual(delta * threads, coll.Count, "#2");

                string actual = string.Empty;
                int temp;
                while (coll.TryTake(out temp))
                {
                    actual += temp.ToString();;
                }

                IEnumerable <int> range = Enumerable.Range(order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
                if (order == CheckOrderingType.Reversed)
                {
                    range = range.Reverse();
                }

                string expected = range.Aggregate(string.Empty, (acc, v) => acc + v);

                if (order == CheckOrderingType.DontCare)
                {
                    CollectionAssert.AreEquivalent(expected, actual, "#3");
                }
                else
                {
                    Assert.AreEqual(expected, actual, "#3");
                }
            });
        }
Esempio n. 10
0
        public void ContinueWithOnCompletedSuccessfullyTestCase()
        {
            ParallelTestHelper.Repeat(delegate {
                bool result = false;

                Task t    = Task.Factory.StartNew(delegate { });
                Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnRanToCompletion);
                t.Wait();
                cont.Wait();

                Assert.IsNull(cont.Exception, "#1");
                Assert.IsNotNull(cont, "#2");
                Assert.IsTrue(result, "#3");
            });
        }
Esempio n. 11
0
        public void StressNonZeroTest()
        {
            ParallelTestHelper.Repeat(delegate {
                ParallelTestHelper.ParallelStressTest(snzi, (s) => {
                    snzi.Increment();
                    for (int i = 0; i < 1; i++)
                    {
                        if (i % 2 == 0)
                        {
                            snzi.Increment();
                        }
                        else
                        {
                            snzi.Decrement();
                        }
                    }
                });

                Assert.IsFalse(snzi.IsSet, "#1");
            });
        }
Esempio n. 12
0
        public void CancelTestCase()
        {
            int count = 1;

            ParallelTestHelper.Repeat(delegate {
                bool result = false;

                Task t = new Task(delegate {
                    result = true;
                });
                t.Cancel();
                Assert.IsTrue(t.IsCancellationRequested, "#-1");
                t.Start();
                t.Wait();

                Assert.IsInstanceOfType(typeof(TaskCanceledException), t.Exception, "#1 : " + count++);
                TaskCanceledException ex = (TaskCanceledException)t.Exception;
                Assert.AreEqual(t, ex.Task, "#2");
                Assert.IsFalse(result, "#3");
            });
        }
Esempio n. 13
0
        public void RemoveParallelTest()
        {
            ParallelTestHelper.Repeat(delegate {
                Setup();
                int index = 0;
                bool r1   = false, r2 = false, r3 = false;
                int val;

                ParallelTestHelper.ParallelStressTest(map, delegate {
                    int own = Interlocked.Increment(ref index);
                    switch (own)
                    {
                    case 1:
                        r1 = map.TryRemove("foo", out val);
                        break;

                    case 2:
                        r2 = map.TryRemove("bar", out val);
                        break;

                    case 3:
                        r3 = map.TryRemove("foobar", out val);
                        break;
                    }
                }, 3);

                Assert.AreEqual(0, map.Count);
                int value;

                Assert.IsTrue(r1, "1");
                Assert.IsTrue(r2, "2");
                Assert.IsTrue(r3, "3");

                Assert.IsFalse(map.TryGetValue("foo", out value), "#1");
                Assert.IsFalse(map.TryGetValue("bar", out value), "#2");
                Assert.IsFalse(map.TryGetValue("foobar", out value), "#3");
            });
        }
Esempio n. 14
0
        public void MultipleTaskTestCase()
        {
            ParallelTestHelper.Repeat(delegate {
                bool r1 = false, r2 = false, r3 = false;

                Task t1 = Task.Factory.StartNew(delegate {
                    r1 = true;
                });
                Task t2 = Task.Factory.StartNew(delegate {
                    r2 = true;
                });
                Task t3 = Task.Factory.StartNew(delegate {
                    r3 = true;
                });

                t1.Wait();
                t2.Wait();
                t3.Wait();

                Assert.IsTrue(r1, "#1");
                Assert.IsTrue(r2, "#2");
                Assert.IsTrue(r3, "#3");
            });
        }
Esempio n. 15
0
        public void ContinueWithOnAbortedTestCase()
        {
            ParallelTestHelper.Repeat(delegate {
                bool result     = false;
                bool taskResult = false;

                Task t = new Task(delegate { taskResult = true; });
                t.Cancel();

                Task cont = t.ContinueWith(delegate { result = true; }, TaskContinuationOptions.OnlyOnCanceled);

                t.Start();
                t.Wait();
                cont.Wait();

                Assert.IsFalse(taskResult, "#-1");
                Assert.AreEqual(TaskStatus.Canceled, t.Status, "#0");
                Assert.IsTrue(t.IsCanceled, "#0bis");

                Assert.IsNull(cont.Exception, "#1");
                Assert.IsNotNull(cont, "#2");
                Assert.IsTrue(result, "#3");
            });
        }