Example #1
0
        [Test] public void NewSingleThreadPoolChokesOnNullFactory()
        {
            var e = Assert.Throws <ArgumentNullException>(
                () => Executors.NewSingleThreadExecutor(null));

            Assert.That(e.ParamName, Is.EqualTo("threadFactory"));
        }
Example #2
0
        [Test] public void NewSingleThreadExecutorWithFactoryCanExecuteRunnables()
        {
            var e = Executors.NewSingleThreadExecutor(new SimpleThreadFactory());

            CanExecuteRunnable(e);
            JoinPool(e);
        }
Example #3
0
        [Test] public void NewSingleThreadExecutorCanExecuteRunnables()
        {
            var e = Executors.NewSingleThreadExecutor();

            CanExecuteRunnable(e);
            JoinPool(e);
        }
Example #4
0
        [Test] public void NewDefaultThreadFactoryHasSpecifiedPriorityBackgroundStatusAndName()
        {
            Action r = ThreadManager.GetManagedAction(
                delegate
            {
                Thread current = Thread.CurrentThread;
                Assert.IsTrue(!current.IsBackground);
                Assert.IsTrue(current.Priority <= ThreadPriority.Normal);
                String name = current.Name;
                Assert.IsTrue(name.EndsWith("thread-1"));
            });
            IExecutorService e = Executors.NewSingleThreadExecutor(Executors.NewDefaultThreadFactory());

            e.Execute(r);
            e.Shutdown();
            Thread.Sleep(Delays.Short);
            JoinPool(e);
            ThreadManager.JoinAndVerify();
        }
Example #5
0
        [Test] public void TimeoutsFromExecuteWillTimeOutIfTheyComputeTooLong()
        {
            const int               n        = 10000;
            IExecutorService        executor = Executors.NewSingleThreadExecutor();
            List <ICallable <int> > tasks    = new List <ICallable <int> >(n);

            try
            {
                DateTime startTime = DateTime.UtcNow;

                while (tasks.Count < n)
                {
                    tasks.Add(new TimedCallable <int>(executor,
                                                      delegate { Thread.SpinWait(300); return(1); }, TimeSpan.FromMilliseconds(1)));
                }

                int iters = 0;
                foreach (var task in tasks)
                {
                    try
                    {
                        ++iters;
                        task.Call();
                    }
                    catch (TimeoutException)
                    {
                        Assert.That(iters, Is.GreaterThan(0));
                        return;
                    }
                }
                // if by chance we didn't ever time out, total time must be small
                TimeSpan elapsed = DateTime.UtcNow - startTime;
                Assert.That(elapsed.TotalMilliseconds, Is.LessThan(n));
            }
            finally
            {
                JoinPool(executor);
            }
        }