public void Wait_ThrowsOperationCanceledException_OnStop()
        {
            var fail = true;

            using (var controller = new ThreadController())
            {
                controller.Add(c =>
                {
                    try
                    {
                        Thread.Sleep(100);
                        c.Wait();
                    }
                    catch (OperationCanceledException)
                    {
                        fail = false;
                    }
                });

                controller.Start();
                controller.Stop();

                Assert.That(fail, Is.False, "Expected OperationCanceledException");
            }
        }
        public void Stop_DoesNotPropagate_OperationCanceledExceptions()
        {
            using (var controller = new ThreadController())
            {
                controller.Add(c => { Thread.Sleep(50); c.Wait(); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); });
                controller.Add(c => { Thread.Sleep(50); throw new OperationCanceledException(); });

                controller.Start();
                controller.Stop();
            }
        }
        public void Add_DoesNotStartTasks()
        {
            var token = "Token";
            using(var controller = new ThreadController())
            {
                controller.Add(c => { token = "Changed"; });

                Thread.Sleep(50);
                Assert.That(token, Is.EqualTo("Token"));

                controller.Stop();
            }
        }
        public void StartAction_StartsTaskImmediately()
        {
            var token = "Token";
            using(var controller = new ThreadController())
            {
                controller.Start(c => { token = "Changed"; });

                Thread.Sleep(50);
                Assert.That(token, Is.EqualTo("Changed"));

                controller.Stop();
            }
        }
        public void Add_DoesNotStartTasks()
        {
            var token = "Token";

            using (var controller = new ThreadController())
            {
                controller.Add(c => { token = "Changed"; });

                Thread.Sleep(50);
                Assert.That(token, Is.EqualTo("Token"));

                controller.Stop();
            }
        }
        public void StartAction_StartsTaskImmediately()
        {
            var token = "Token";

            using (var controller = new ThreadController())
            {
                controller.Start(c => { token = "Changed"; });

                Thread.Sleep(50);
                Assert.That(token, Is.EqualTo("Changed"));

                controller.Stop();
            }
        }
        public void Pause_PausesWaitingTasks()
        {
            long[] token = { 0 };
            using (var controller = new ThreadController())
            {
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });

                controller.Start();
                controller.Pause();

                Thread.Sleep(100);
                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(0));

                controller.Stop();
            }
        }
        public void Pause_PausesWaitingTasks()
        {
            long[] token = { 0 };
            using(var controller = new ThreadController())
            {
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); Interlocked.Increment(ref token[0]); });

                controller.Start();
                controller.Pause();

                Thread.Sleep(100);
                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(0));

                controller.Stop();
            }
        }
        public void Start_StartsAllPendingTasks()
        {
            long[] token = { 0 };
            using (var controller = new ThreadController())
            {
                controller.Add(c => Interlocked.Increment(ref token[0]));
                controller.Add(c => Interlocked.Increment(ref token[0]));
                controller.Add(c => Interlocked.Increment(ref token[0]));

                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(0));

                controller.Start();
                Thread.Sleep(50);

                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(3));

                controller.Stop();
            }
        }
        public void Stop_PropagatesExceptions()
        {
            using (var controller = new ThreadController())
            {
                controller.Add(c => { throw new InvalidOperationException(); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); throw new ArgumentOutOfRangeException(); });
                controller.Add(c => { throw new ArgumentNullException(); });

                controller.Start();

                try
                {
                    controller.Stop();
                    Assert.Fail("Expected AggregateException.");
                }

                catch (AggregateException)
                {
                }
            }
        }
        public void Wait_WithTimeout_BlocksExecution()
        {
            var fail = false;

            using (var controller = new ThreadController())
            {
                controller.Add(c =>
                {
                    c.Wait(TimeSpan.FromMilliseconds(100));
                    fail = true;
                });

                controller.Add(c => c.Wait());

                controller.Start();
                Thread.Sleep(50);
                controller.Stop();

                Assert.That(fail, Is.False);
            }
        }
Exemplo n.º 12
0
        public void Execute(ScheduledTask task, ThreadController controller)
        {
            controller.Wait();

            task.Status = TaskStatus.Running;
            _repository.Save(task);

            var client = CreateClient(task);

            controller.Wait();

            try
            {
                client.Open();
                var result = client.Execute();
                client.Close();

                RecordResult(task, result.Result, result.Message);
            }

            catch (TimeoutException te)
            {
                RecordResult(task, TaskResult.RecoverableFault, te.Message, te);
                client.Abort();
            }

            catch (CommunicationException ce)
            {
                RecordResult(task, TaskResult.RecoverableFault, ce.Message, ce);
                client.Abort();
            }

            catch (Exception ex)
            {
                RecordResult(task, TaskResult.FatalError, ex.Message, ex);
                client.Abort();
                throw;
            }
        }
Exemplo n.º 13
0
        public void Execute(ScheduledTask task, ThreadController controller)
        {
            controller.Wait();

            task.Status = TaskStatus.Running;
            _repository.Save(task);

            var client = CreateClient(task);

            controller.Wait();

            try
            {
                client.Open();
                var result = client.Execute();
                client.Close();

                RecordResult(task, result.Result, result.Message);
            }

            catch(TimeoutException te)
            {
                RecordResult(task, TaskResult.RecoverableFault, te.Message, te);
                client.Abort();
            }

            catch(CommunicationException ce)
            {
                RecordResult(task, TaskResult.RecoverableFault, ce.Message, ce);
                client.Abort();
            }

            catch(Exception ex)
            {
                RecordResult(task, TaskResult.FatalError, ex.Message, ex);
                client.Abort();
                throw;
            }
        }
Exemplo n.º 14
0
        public void Poll(ThreadController controller)
        {
            controller.Wait(); // check for pause/abort

            while (true)
            {
                try
                {
                    var tasks = _repository.GetPendingTasks();
                    foreach (var task in tasks)
                    {
                        var taskToRun = task;
                        controller.Start(c => _executor.Execute(taskToRun, controller));
                    }
                }

                finally
                {
                    // TODO: configurable polling interval
                    controller.Wait(TimeSpan.FromMinutes(1));
                }
            }
        }
Exemplo n.º 15
0
        public void Poll(ThreadController controller)
        {
            controller.Wait(); // check for pause/abort

            while(true)
            {
                try
                {
                    var tasks = _repository.GetPendingTasks();
                    foreach(var task in tasks)
                    {
                        var taskToRun = task;
                        controller.Start(c => _executor.Execute(taskToRun, controller));
                    }
                }

                finally
                {
                    // TODO: configurable polling interval
                    controller.Wait(TimeSpan.FromMinutes(1));
                }
            }
        }
        public void Wait_OnNonTaskThread_BlocksForActivity()
        {
            var fail = true;

            using (var controller = new ThreadController())
            {
                controller.Start();
                controller.Pause();

                Action func = () =>
                {
                    controller.Wait(TimeSpan.FromMilliseconds(50));
                    fail = true;
                };

                fail = false;
                var result = func.BeginInvoke(null, null);
                controller.Stop();
                result.AsyncWaitHandle.WaitOne();

                Assert.That(fail, Is.False, "Task did not wait properly.");
            }
        }
        public void Stop_PropagatesExceptions()
        {
            using(var controller = new ThreadController())
            {
                controller.Add(c => { throw new InvalidOperationException(); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); throw new ArgumentOutOfRangeException(); });
                controller.Add(c => { throw new ArgumentNullException(); });

                controller.Start();

                try
                {
                    controller.Stop();
                    Assert.Fail("Expected AggregateException.");
                }

                catch(AggregateException)
                {
                }
            }
        }
        public void Wait_ThrowsOperationCanceledException_OnStop()
        {
            var fail = true;
            using(var controller = new ThreadController())
            {
                controller.Add(c =>
                    {
                        try
                        {
                            Thread.Sleep(100);
                            c.Wait();
                        }
                        catch(OperationCanceledException)
                        {
                            fail = false;
                        }
                    });

                controller.Start();
                controller.Stop();

                Assert.That(fail, Is.False, "Expected OperationCanceledException");
            }
        }
        public void Wait_WithTimeout_BlocksExecution()
        {
            var fail = false;
            using(var controller = new ThreadController())
            {
                controller.Add(c =>
                {
                    c.Wait(TimeSpan.FromMilliseconds(100));
                    fail = true;
                });

                controller.Add(c => c.Wait());

                controller.Start();
                Thread.Sleep(50);
                controller.Stop();

                Assert.That(fail, Is.False);
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Scheduler"/> class.
 /// </summary>
 public Scheduler()
 {
     _controller = new ThreadController();
     _poller     = new SchedulePoller();
 }
        public void Stop_DoesNotPropagate_OperationCanceledExceptions()
        {
            using(var controller = new ThreadController())
            {
                controller.Add(c => { Thread.Sleep(50); c.Wait(); });
                controller.Add(c => { Thread.Sleep(50); c.Wait(); });
                controller.Add(c => { Thread.Sleep(50); throw new OperationCanceledException(); });

                controller.Start();
                controller.Stop();
            }
        }
        public void Wait_OnNonTaskThread_BlocksForActivity()
        {
            var fail = true;
            using(var controller = new ThreadController())
            {
                controller.Start();
                controller.Pause();

                Action func = () =>
                {
                    controller.Wait(TimeSpan.FromMilliseconds(50));
                    fail = true;
                };

                fail = false;
                var result = func.BeginInvoke(null, null);
                controller.Stop();
                result.AsyncWaitHandle.WaitOne();

                Assert.That(fail, Is.False, "Task did not wait properly.");
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Scheduler"/> class.
 /// </summary>
 public Scheduler()
 {
     _controller = new ThreadController();
     _poller = new SchedulePoller();
 }
        public void Start_StartsAllPendingTasks()
        {
            long[] token = { 0 };
            using(var controller = new ThreadController())
            {
                controller.Add(c => Interlocked.Increment(ref token[0]));
                controller.Add(c => Interlocked.Increment(ref token[0]));
                controller.Add(c => Interlocked.Increment(ref token[0]));

                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(0));

                controller.Start();
                Thread.Sleep(50);

                Assert.That(Interlocked.Read(ref token[0]), Is.EqualTo(3));

                controller.Stop();
            }
        }