Beispiel #1
0
        public void Actions_wait_for_earlier_actions_to_finish()
        {
            // setup
            var scheduler = new SingleActionScheduler();

            bool blocked     = true;
            var  blockedTask = scheduler.Schedule(() => { while (blocked)
                                                          {
                                                              Thread.Sleep(1);
                                                          }
                                                  });

            WaitForStart(blockedTask);

            // execute
            int count1 = 0;
            var task1  = scheduler.Schedule(() => count1++);

            // verify
            task1.Wait(50).Should().BeFalse();

            blocked = false;
            blockedTask.Wait();
            task1.Wait();
            count1.Should().Be(1);
        }
Beispiel #2
0
        public void When_multiple_actions_scheduled_old_ones_are_canceled()
        {
            // setup
            var scheduler = new SingleActionScheduler();

            bool blocked     = true;
            var  blockedTask = scheduler.Schedule(() => { while (blocked)
                                                          {
                                                              Thread.Sleep(1);
                                                          }
                                                  });

            WaitForStart(blockedTask);

            // execute
            int count1 = 0;
            var task1  = scheduler.Schedule(() => count1++);

            int count2 = 0;
            var task2  = scheduler.Schedule(() => count2++);

            // verify
            blocked = false;
            blockedTask.Wait();

            count1.Should().Be(0);
            task1.IsCanceled.Should().BeTrue();

            task2.Wait();
            count2.Should().Be(1);
        }
Beispiel #3
0
        public virtual Task AddNewJob(Job job)
        {
            Jobs.AddOrUpdate(job);

            EventAggregator.Publish(new NewJobAvailableEvent());

            return(StartJobScheduler.Schedule(TryStartNextJob));
        }
Beispiel #4
0
        public void After_exception_later_actions_get_run()
        {
            // setup
            var scheduler = new SingleActionScheduler();

            WaitForStart(scheduler.Schedule(() => { throw new TestException(); }));

            // execute
            int count2 = 0;
            var task2  = scheduler.Schedule(() => count2++);

            // verify
            task2.Wait();
            count2.Should().Be(1);
        }
Beispiel #5
0
        public void Simple_task_runs()
        {
            // setup
            var scheduler = new SingleActionScheduler();
            int count     = 0;

            // execute
            scheduler.Schedule(() => count++).Wait();

            // verify
            count.Should().Be(1);
        }
Beispiel #6
0
        public void Exception_propagates()
        {
            // setup
            var scheduler = new SingleActionScheduler();

            // execute
            var t = scheduler.Schedule(() => { throw new TestException(); });

            // verify
            Action wait = t.Wait;

            wait.ShouldThrow <TestException>();
        }
Beispiel #7
0
        public void After_cancel_later_actions_get_run()
        {
            // setup
            var scheduler = new SingleActionScheduler();
            var source    = new CancellationTokenSource();

            WaitForStart(scheduler.Schedule(() =>
            {
                while (true)
                {
                    source.Token.ThrowIfCancellationRequested();
                    Thread.Sleep(100);
                }
            }, source.Token));

            // execute
            var task1 = scheduler.Schedule(() => new object());

            source.Cancel();

            // verify
            task1.Wait();
        }
Beispiel #8
0
        public void Cancellation_works()
        {
            // setup
            var scheduler   = new SingleActionScheduler();
            var source      = new CancellationTokenSource();
            var foreverTask = scheduler.Schedule(() =>
            {
                while (true)
                {
                    source.Token.ThrowIfCancellationRequested();
                    Thread.Sleep(1);
                }
            }, source.Token);

            WaitForStart(foreverTask);

            // execute
            source.Cancel();

            // verify
            Action wait = foreverTask.Wait;

            wait.ShouldThrow <OperationCanceledException>();
        }