コード例 #1
0
 public void TestQueueWorker()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         using (var finished = new ManualResetEvent(false))
         {
             int countdown = 42;
             for (int i = 0; i < 42; ++i)
             {
                 ftp.QueueWorker(
                     rng.Next(12),
                     () =>
                 {
                     Assert.Greater(countdown, 0, "Incorrect countdown value, some jobs may have run more than once");
                     if (Interlocked.Decrement(ref countdown) == 0)
                     {
                         finished.Set();
                     }
                 });
             }
             finished.WaitOne();
             Assert.AreEqual(0, countdown, "Incorrect countdown value, some jobs may not have run or run more than once");
             Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
         }
     }
 }
コード例 #2
0
        public void TestQueueWorkerFuture()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                var now = DateTime.UtcNow;
                int intToCheck = rng.Next();

                var futureInt = ftp.QueueWorker(() => intToCheck);
                var futureStr = ftp.QueueWorker(rng.Next(12), () => "Forty two");
                var futureDateTime = ftp.QueueWorker(rng.Next(), () => DateTime.UtcNow);
                var futureExc = ftp.QueueWorker<int>(() => { throw new InvalidOperationException(); });

                Assert.AreEqual(intToCheck, futureInt.Value, "The Future returned a bad value.");
                Assert.AreEqual("Forty two", futureStr.Value, "The Future returned a bad value.");
                Assert.GreaterOrEqual(futureDateTime.Value, now, "The Future should return a date in the future!");

                int r_e = rng.Next ();
                int r_f = r_e;
                var ex = Assert.Throws(typeof(FutureValueException), () => { r_f = futureExc.Value; }, "Exception type is incorrect.");
                Assert.IsTrue(ex.InnerException is InvalidOperationException);
                Assert.AreEqual(r_e, r_f, "The Future should not transmit a value.");

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #3
0
        public void TestQueueWorkerFuture()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                var now        = DateTime.UtcNow;
                int intToCheck = rng.Next();

                var futureInt      = ftp.QueueWorker(() => intToCheck);
                var futureStr      = ftp.QueueWorker(rng.Next(12), () => "Forty two");
                var futureDateTime = ftp.QueueWorker(rng.Next(), () => DateTime.UtcNow);
                var futureExc      = ftp.QueueWorker <int>(() => { throw new InvalidOperationException(); });

                Assert.AreEqual(intToCheck, futureInt.Value, "The Future returned a bad value.");
                Assert.AreEqual("Forty two", futureStr.Value, "The Future returned a bad value.");
                Assert.GreaterOrEqual(futureDateTime.Value, now, "The Future should return a date in the future!");

                int r_e = rng.Next();
                int r_f = r_e;
                var ex  = Assert.Throws(typeof(FutureValueException), () => { r_f = futureExc.Value; }, "Exception type is incorrect.");
                Assert.IsTrue(ex.InnerException is InvalidOperationException);
                Assert.AreEqual(r_e, r_f, "The Future should not transmit a value.");

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #4
0
 public void TestLifeCycle()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         Assert.AreEqual(8, ftp.NThreads, "Bad number of started threads");
         Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
         Assert.AreEqual(0, ftp.Running, "There should not be any running job");
     }
 }
コード例 #5
0
 public void TestLifeCycle()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         Assert.AreEqual(8, ftp.NThreads, "Bad number of started threads");
         Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
         Assert.AreEqual(0, ftp.Running, "There should not be any running job");
     }
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: sdanzan/SimpleConcurrency
        static void Main(string[] args)
        {
            var pool = new FairThreadPool("Example", Environment.ProcessorCount);

            Console.WriteLine(pool.Name);

            var future = pool.QueueWorker(() => 40);
            int r = 2 + future.Value;
            Console.WriteLine(r);
            Console.WriteLine("Press [ENTER] to exit.");
            Console.ReadLine();

            pool.Dispose();
        }
コード例 #7
0
        public void TestQueueWorkerWaitableTimeout()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                using (var toggle = new ManualResetEvent(false))
                {
                    var ww = ftp.QueueWaitableWorker(() => toggle.WaitOne());
                    Assert.IsFalse(ww.Wait(10), "The timeout should have expired");
                    Assert.IsFalse(ww.Wait(new TimeSpan(0, 0, 0, 0, 10)), "The timeout should have expired");
                    toggle.Set();
                    ww.Wait();
                }

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #8
0
        public void TestQueueWorkerFutureTimeout()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                using (var toggle = new ManualResetEvent(false))
                {
                    var futureInt = ftp.QueueWorker(() => { toggle.WaitOne(); return(42); });
                    Assert.IsFalse(futureInt.Wait(new TimeSpan(0, 0, 0, 0, 10)), "The Future should time out.");
                    Assert.IsFalse(futureInt.Wait(10), "The Future should time out.");
                    toggle.Set();
                    Assert.AreEqual(42, futureInt.Value, "The Future returned a bad value.");
                }

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #9
0
 public void TestQueueWorker()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         using (var finished = new ManualResetEvent(false))
         {
             int countdown = 42;
             for (int i = 0; i < 42; ++i)
             {
                 ftp.QueueWorker(
                     rng.Next(12),
                     () =>
                     {
                         Assert.Greater(countdown, 0, "Incorrect countdown value, some jobs may have run more than once");
                         if (Interlocked.Decrement(ref countdown) == 0) finished.Set();
                     });
             }
             finished.WaitOne();
             Assert.AreEqual(0, countdown, "Incorrect countdown value, some jobs may not have run or run more than once");
             Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
         }
     }
 }
コード例 #10
0
 public void TestQueueWorkerWaitable()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         int countdown = 42;
         var toWait    = new List <IWaitable>();
         for (int i = 0; i < 42; ++i)
         {
             toWait.Add(ftp.QueueWaitableWorker(
                            rng.Next(7),
                            () =>
             {
                 Assert.Greater(countdown, 0, "Incorrect countdown value, some jobs may have run more than once");
                 Interlocked.Decrement(ref countdown);
             }));
         }
         foreach (var w in toWait)
         {
             w.Wait();
         }
         Assert.AreEqual(0, countdown, "Incorrect countdown value, some jobs may not have run or run more than once");
         Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
     }
 }
コード例 #11
0
        public void TestQueueWorkerFutureTimeout()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                using (var toggle = new ManualResetEvent(false))
                {
                    var futureInt = ftp.QueueWorker(() => { toggle.WaitOne(); return 42; });
                    Assert.IsFalse(futureInt.Wait(new TimeSpan(0, 0, 0, 0, 10)), "The Future should time out.");
                    Assert.IsFalse(futureInt.Wait(10), "The Future should time out.");
                    toggle.Set();
                    Assert.AreEqual(42, futureInt.Value, "The Future returned a bad value.");
                }

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #12
0
        public void TestQueueWorkerWaitableTimeout()
        {
            using (var ftp = new FairThreadPool("Glube", 8))
            {
                using (var toggle = new ManualResetEvent(false))
                {
                    var ww = ftp.QueueWaitableWorker(() => toggle.WaitOne());
                    Assert.IsFalse(ww.Wait(10), "The timeout should have expired");
                    Assert.IsFalse(ww.Wait(new TimeSpan(0, 0, 0, 0, 10)), "The timeout should have expired");
                    toggle.Set();
                    ww.Wait();
                }

                Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
            }
        }
コード例 #13
0
 public void TestQueueWorkerWaitable()
 {
     using (var ftp = new FairThreadPool("Glube", 8))
     {
         int countdown = 42;
         var toWait = new List<IWaitable>();
         for (int i = 0; i < 42; ++i)
         {
             toWait.Add(ftp.QueueWaitableWorker(
                 rng.Next(7),
                 () =>
                 {
                     Assert.Greater(countdown, 0, "Incorrect countdown value, some jobs may have run more than once");
                     Interlocked.Decrement(ref countdown);
                 }));
         }
         foreach (var w in toWait)
         {
             w.Wait();
         }
         Assert.AreEqual(0, countdown, "Incorrect countdown value, some jobs may not have run or run more than once");
         Assert.AreEqual(0, ftp.Pending, "There sould not be any pending job");
     }
 }
コード例 #14
0
 /// <summary>
 /// Standard constructor.
 /// </summary>
 /// <param name="pool">The FairThreadPool to use.</param>
 public FairThreadPoolScheduler(FairThreadPool pool)
 {
     _pool = pool;
 }