コード例 #1
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);
            }
        }
コード例 #2
0
 /// <summary>
 /// Executes the given task at some time in the future.
 /// </summary>
 /// <remarks>
 /// The task may execute in a new thread, in a pooled thread, or in the calling
 /// thread, at the discretion of the <see cref="IExecutor"/> implementation.
 /// </remarks>
 /// <param name="task">The task to be executed.</param>
 /// <exception cref="Spring.Threading.Execution.RejectedExecutionException">
 /// If the task cannot be accepted for execution.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 /// If the <paramref name="task"/> is <c>null</c>
 /// </exception>
 public virtual void Execute(Task task)
 {
     Execute(Executors.CreateRunnable(task));
 }
コード例 #3
0
        [Test] public void CreateRunnableChokesOnNullAction()
        {
            var e = Assert.Throws <ArgumentNullException>(() => Executors.CreateRunnable(null));

            Assert.That(e.ParamName, Is.EqualTo("action"));
        }