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");
            }
        }
        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");
            }
        }
 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");
         }
     }
 }
Beispiel #4
0
        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();
        }
        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");
            }
        }
 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");
         }
     }
 }
        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");
            }
        }