Exemple #1
0
        public void RemoveActionRemovesQueuedTaskAndFailesToRemoveActiveTask()
        {
            IBlockingQueue <IRunnable> q = new ArrayBlockingQueue <IRunnable>(10);
            var es = new ThreadPoolExecutor(1, 1, Delays.Long, q);

            ExecutorService = es;
            var tasks = new Action[5];

            for (int i = 0; i < 5; i++)
            {
                int timeout = i;
                tasks[i] = NewInterruptableAction(Delays.Medium + TimeSpan.FromMilliseconds(timeout));
                es.Execute(tasks[i]);
            }
            Thread.Sleep(Delays.Short);
            Assert.IsFalse(es.Remove(tasks[0]));
            Assert.That(q.Count, Is.EqualTo(4));
            Assert.IsTrue(es.Remove(tasks[4]));
            Assert.IsFalse(es.Remove(tasks[4]));
            Assert.That(q.Count, Is.EqualTo(3));
            Assert.IsTrue(es.Remove(tasks[3]));
            Assert.That(q.Count, Is.EqualTo(2));
            InterruptAndJoinPool(es);
        }
Exemple #2
0
        public void RemoveRunnableRemovesQueuedTaskAndFailesToRemoveActiveTask()
        {
            IBlockingQueue <IRunnable> q = new ArrayBlockingQueue <IRunnable>(10);
            var es = new ThreadPoolExecutor(1, 1, Delays.Long, q);

            ExecutorService = es;
            FutureTask <bool>[] tasks = new FutureTask <bool> [5];
            for (int i = 0; i < 5; i++)
            {
                tasks[i] = new FutureTask <bool>(_mediumInterruptableAction, true);
                es.Execute(tasks[i]);
            }
            Thread.Sleep(Delays.Short);
            Assert.IsFalse(es.Remove(tasks[0]));
            Assert.IsTrue(q.Contains(tasks[4]));
            Assert.IsTrue(q.Contains(tasks[3]));
            Assert.IsTrue(es.Remove(tasks[4]));
            Assert.IsFalse(es.Remove(tasks[4]));
            Assert.IsFalse(q.Contains(tasks[4]));
            Assert.IsTrue(q.Contains(tasks[3]));
            Assert.IsTrue(es.Remove(tasks[3]));
            Assert.IsFalse(q.Contains(tasks[3]));
            InterruptAndJoinPool(es);
        }