예제 #1
0
        public void Get_NonExistingJobName_ThrowsInvalidJobOperationException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            jobManager.Create("job1");
            jobManager.Create("job2");

            // Act
            var ex = Assert.Throws <InvalidJobOperationException>(() => jobManager.Get("non-existing"));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("Job not found: 'non-existing'."));
        }
예제 #2
0
        public void Create_NameAlreadyExists_ThrowsInvalidJobOperationException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var name = "job1";

            jobManager.Create(name);

            // Act
            var ex = Assert.Throws <InvalidJobOperationException>(() => jobManager.Create(name));

            // Assert
            Assert.That(ex.Message, Is.EqualTo($"Job '{name}' already exists."));
        }
예제 #3
0
        public void Get_Started_ReturnsJob()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(false);
            jobManager.Start();
            var job1 = jobManager.Create("job1");
            var job2 = jobManager.Create("job2");

            // Act
            var gotJob1 = jobManager.Get("job1");

            // Assert
            Assert.That(gotJob1, Is.SameAs(job1));
        }
예제 #4
0
        public void GetNames_Disposed_ThrowsJobObjectIsDisposedException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            jobManager.Create("job1");
            jobManager.Create("job2");
            jobManager.Dispose();

            // Act
            var ex = Assert.Throws <JobObjectDisposedException>(() => jobManager.GetNames());

            // Assert
            Assert.That(ex.Message, Is.EqualTo($"'{typeof(IJobManager).FullName}' is disposed."));
            Assert.That(ex.ObjectName, Is.EqualTo(typeof(IJobManager).FullName));
        }
예제 #5
0
        public void GetNames_Started_ReturnsJobNames()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(false);
            jobManager.Start();

            jobManager.Create("job1");
            jobManager.Create("job2");

            // Act
            var jobNames = jobManager.GetNames();

            // Assert
            CollectionAssert.AreEquivalent(new string[] { "job1", "job2" }, jobNames);
        }
예제 #6
0
        public void IsDisposed_JobIsDisposed_ReturnsTrue()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromHours(1), token);
            };

            job.IsEnabled = true;

            job.ForceStart();
            job.Dispose();

            // Act
            var isDisposed = job.IsDisposed;


            // Assert
            Assert.That(isDisposed, Is.True);
        }
예제 #7
0
        public void WaitInt_JobIsDisposed_ThrowsJobObjectDisposedException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromSeconds(0.5), token);
            };

            job.IsEnabled = true;

            job.Schedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);

            job.Dispose();

            // Act
            var ex = Assert.Throws <JobObjectDisposedException>(() => job.Wait(10));

            // Assert
            Assert.That(ex, Has.Message.EqualTo("'my-job' is disposed."));
        }
예제 #8
0
        public void Cancel_NotRunning_ReturnsFalse()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);
            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");
            job.IsEnabled = true;

            // Act
            var canceled = job.Cancel();

            // Assert
            Assert.That(canceled, Is.False);

            var info = job.GetInfo(null);

            Assert.That(info.CurrentRun, Is.Null);
            Assert.That(info.NextDueTime, Is.EqualTo(TestHelper.NeverCopy));
            Assert.That(info.NextDueTimeIsOverridden, Is.False);
            Assert.That(info.RunCount, Is.Zero);
            Assert.That(info.Runs, Is.Empty);
        }
예제 #9
0
        public void Output_ValueIsSet_EqualsToThatValue()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            // Act
            TextWriter writer1 = new StringWriterWithEncoding(Encoding.UTF8);

            job.Output = writer1;
            var readOutput1 = job.Output;

            job.IsEnabled = true;
            TextWriter writer2 = new StringWriterWithEncoding(Encoding.UTF8);

            job.Output = writer2;
            var readOutput2 = job.Output;

            job.IsEnabled = false;
            TextWriter writer3 = null;

            job.Output = writer3;
            var readOutput3 = job.Output;

            // Assert
            Assert.That(writer1, Is.EqualTo(readOutput1));
            Assert.That(writer2, Is.EqualTo(readOutput2));
            Assert.That(writer3, Is.EqualTo(readOutput3));
        }
예제 #10
0
        public void WaitInt_NegativeArgument_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromSeconds(0.5), token);
            };

            job.IsEnabled = true;

            job.Schedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);

            // Act
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => job.Wait(-1));

            // Assert
            Assert.That(ex.ParamName, Is.EqualTo("millisecondsTimeout"));
        }
예제 #11
0
        public async Task Schedule_SetValidValue_SetsSchedule()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var name = "job1";
            var job  = jobManager.Create(name);

            job.IsEnabled = true;

            var writer = new StringWriterWithEncoding(Encoding.UTF8);

            job.Output = writer;

            // Act
            var newSchedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start.AddSeconds(2));

            job.Schedule = newSchedule;
            job.Routine  = (parameter, tracker, output, token) =>
            {
                output.Write("Hello!");
                return(Task.CompletedTask);
            };

            await timeMachine.WaitUntilSecondsElapse(start, 2.5);

            // Assert
            Assert.That(writer.ToString(), Is.EqualTo("Hello!"));
            Assert.That(job.Schedule, Is.SameAs(newSchedule));
        }
예제 #12
0
        public void WaitInt_NotRunning_ReturnsCompletedImmediately()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromSeconds(0.5), token);
            };

            // Act
            var waitResult = job.Wait(10);

            // Assert
            Assert.That(waitResult, Is.EqualTo(JobRunStatus.Completed));
        }
예제 #13
0
        public void Schedule_SetThenDisposed_EqualsToLastOne()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var schedule1 = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);
            var schedule2 = new SimpleSchedule(SimpleScheduleKind.Minute, 1, start);
            var schedule3 = new SimpleSchedule(SimpleScheduleKind.Hour, 1, start);

            job.Schedule = schedule1;
            var readSchedule1 = job.Schedule;

            job.Schedule = schedule2;
            var readSchedule2 = job.Schedule;

            // Act
            job.Schedule = schedule3;
            job.Dispose();
            var readSchedule3 = job.Schedule;

            // Assert
            Assert.That(readSchedule1, Is.SameAs(schedule1));
            Assert.That(readSchedule2, Is.SameAs(schedule2));
            Assert.That(readSchedule3, Is.SameAs(schedule3));
        }
예제 #14
0
        public void WaitTimeSpan_WasRunningThenEnds_WaitsAndReturnsCompleted()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.IsEnabled = true;

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await Task.Delay(500, token);
            };

            // Act
            job.ForceStart();

            var waitResult = job.Wait(TimeSpan.FromMilliseconds(1000));

            // Assert
            Assert.That(waitResult, Is.EqualTo(JobRunStatus.Completed));
        }
예제 #15
0
        public void Parameter_ValueIsSet_EqualsToThatValue()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            // Act
            object parameter1 = 1;

            job.Parameter = parameter1;
            var readParameter1 = job.Parameter;

            job.IsEnabled = true;
            object parameter2 = "hello";

            job.Parameter = parameter2;
            var readParameter2 = job.Parameter;

            job.IsEnabled = false;
            object parameter3 = null;

            job.Parameter = parameter3;
            var readParameter3 = job.Parameter;

            // Assert
            Assert.That(parameter1, Is.EqualTo(readParameter1));
            Assert.That(parameter2, Is.EqualTo(readParameter2));
            Assert.That(parameter3, Is.EqualTo(readParameter3));
        }
예제 #16
0
        public void WaitInt_WasRunningThenCanceled_WaitsAndReturnsCanceled()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.IsEnabled = true;

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await Task.Delay(500, token);
            };

            // Act
            job.ForceStart();

            ThreadPool.QueueUserWorkItem(state =>
            {
                Thread.Sleep(200);
                job.Cancel();
            });

            var waitResult = job.Wait(1000);

            // Assert
            Assert.That(waitResult, Is.EqualTo(JobRunStatus.Canceled));
        }
        public void ProgressTracker_ValueIsSet_EqualsToThatValue()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            // Act
            IProgressTracker tracker1 = new MockProgressTracker();

            job.ProgressTracker = tracker1;
            var readTracker1 = job.ProgressTracker;

            job.IsEnabled = true;
            IProgressTracker tracker2 = new MockProgressTracker();

            job.ProgressTracker = tracker2;
            var readTracker2 = job.ProgressTracker;

            job.IsEnabled = false;
            IProgressTracker tracker3 = null;

            job.ProgressTracker = tracker3;
            var readTracker3 = job.ProgressTracker;

            // Assert
            Assert.That(tracker1, Is.EqualTo(readTracker1));
            Assert.That(tracker2, Is.EqualTo(readTracker2));
            Assert.That(tracker3, Is.EqualTo(readTracker3));
        }
예제 #18
0
        public void ForceStart_AlreadyStartedByForce_ThrowsJobException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.IsEnabled = true;

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(800, token);
            };

            job.ForceStart();

            // Act
            var ex = Assert.Throws <JobException>(() => job.ForceStart());

            // Assert
            Assert.That(ex, Has.Message.EqualTo("Job 'my-job' is already running."));
        }
예제 #19
0
        public async Task ForceStart_AlreadyStartedBySchedule_ThrowsJobException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromSeconds(0.7), token);
            };

            job.IsEnabled = true;

            job.Schedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);

            // Act
            await timeMachine.WaitUntilSecondsElapse(start, 1.1);

            var ex = Assert.Throws <JobException>(() => job.ForceStart());

            // Assert
            Assert.That(ex, Has.Message.EqualTo("Job 'my-job' is already running."));
        }
예제 #20
0
        public async Task Schedule_DueTimeWasOverriddenThenScheduleIsSet_OverriddenDueTimeIsDiscardedAndScheduleIsSet()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);


            job.OverrideDueTime(start.AddSeconds(2.5));

            ISchedule schedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);

            // Act
            await timeMachine.WaitUntilSecondsElapse(start, 1.8);

            job.Schedule = schedule;

            // Assert
            var info = job.GetInfo(null);

            Assert.That(info.NextDueTime, Is.EqualTo(start.AddSeconds(2)));
            Assert.That(info.NextDueTimeIsOverridden, Is.False);
        }
예제 #21
0
        public async Task WaitInt_WasRunningTooLong_WaitsAndReturnsNull()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.IsEnabled = true;

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await output.WriteAsync("Hello!");

                await Task.Delay(TimeSpan.FromHours(1), token);
            };

            job.ForceStart();

            // Act

            await timeMachine.WaitUntilSecondsElapse(start, 1.0);

            var gotSignal = job.Wait(1000);

            // Assert
            Assert.That(gotSignal, Is.Null);
        }
예제 #22
0
        public void Schedule_SetValidValueForEnabledOrDisabledJob_SetsValue()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            ISchedule schedule1 = new SimpleSchedule(SimpleScheduleKind.Minute, 1, start);
            ISchedule schedule2 = new SimpleSchedule(SimpleScheduleKind.Day, 1, start);

            // Act
            job.Schedule = schedule1;
            var updatedSchedule1 = job.Schedule;
            var dueTime1         = job.GetInfo(null).NextDueTime;

            job.IsEnabled = true;

            job.Schedule = schedule2;
            var updatedSchedule2 = job.Schedule;
            var dueTime2         = job.GetInfo(null).NextDueTime;

            // Assert
            Assert.That(updatedSchedule1, Is.SameAs(schedule1));
            Assert.That(dueTime1, Is.EqualTo(start.AddMinutes(1)));

            Assert.That(updatedSchedule2, Is.SameAs(schedule2));
            Assert.That(dueTime2, Is.EqualTo(start.AddDays(1)));
        }
예제 #23
0
        public async Task WaitInt_WasRunningThenFaulted_WaitsAndReturnsFaulted()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.IsEnabled = true;

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await Task.Delay(500, token);

                throw new AbandonedMutexException("Hello there!");
            };

            // Act
            job.ForceStart();

            var waitResult = job.Wait(1000);

            await Task.Delay(50); // let job run get written.

            // Assert
            Assert.That(waitResult, Is.EqualTo(JobRunStatus.Faulted));
            Assert.That(job.GetInfo(null).Runs.Single().Exception, Is.TypeOf <AbandonedMutexException>());
        }
예제 #24
0
        public async Task Schedule_SetAndStarted_ReflectedInCurrentRunAndUpdatesToNextDueTime()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            job.Routine = async(parameter, tracker, output, token) =>
            {
                await Task.Delay(10000, token); // long run
            };
            ISchedule schedule = new SimpleSchedule(SimpleScheduleKind.Second, 1, start);

            job.IsEnabled = true;

            // Act
            job.Schedule = schedule; // will fire at 00:01

            await timeMachine.WaitUntilSecondsElapse(start, 1.03);

            //await Task.Delay(1030); // let job start

            // Assert
            try
            {
                var info = job.GetInfo(null);
                var currentRunNullable = info.CurrentRun;

                Assert.That(currentRunNullable, Is.Not.Null); // todo: was null once :(
                var currentRun = currentRunNullable.Value;

                Assert.That(currentRun.StartReason, Is.EqualTo(JobStartReason.ScheduleDueTime));
                Assert.That(currentRun.DueTime, Is.EqualTo(start.AddSeconds(1)));
                Assert.That(currentRun.StartTime,
                            Is.EqualTo(start.AddSeconds(1)).Within(TimeSpan.FromMilliseconds(20)));

                // due time is 00:02 after start
                Assert.That(info.NextDueTime, Is.EqualTo(start.AddSeconds(2)));
            }
            catch (Exception ex)
            {
                var sb = new StringBuilder();
                sb.AppendLine("*** Test Failed ***");
                sb.AppendLine(ex.ToString());
                sb.AppendLine("*** Log: ***");

                var log = _logWriter.ToString();

                sb.AppendLine(log);

                Assert.Fail(sb.ToString());
            }
        }
예제 #25
0
        public async Task Schedule_ScheduleThrows_DueTimeSetToNever()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);


            var n = 0;

            job.IsEnabled = true;
            job.Routine   = (parameter, tracker, output, token) =>
            {
                n = 100;
                return(Task.CompletedTask);
            };

            // set some normal schedule first
            job.Schedule = new SimpleSchedule(SimpleScheduleKind.Minute, 1, start);
            var normalDueTime = job.GetInfo(null).NextDueTime;

            var exception = new NotSupportedException("I do not support this!");

            // Act
            var scheduleMock = new Mock <ISchedule>();

            scheduleMock
            .Setup(x => x.GetDueTimeAfter(It.IsAny <DateTimeOffset>()))
            .Throws(exception);

            job.Schedule = scheduleMock.Object;

            await Task.Delay(3000); // let job try to start (it should not start though)

            var info           = job.GetInfo(null);
            var faultedDueTime = info.NextDueTime;

            // Assert
            Assert.That(normalDueTime, Is.EqualTo(start.AddMinutes(1)));
            Assert.That(faultedDueTime.Year, Is.EqualTo(9000));

            // job never ran
            Assert.That(info.CurrentRun, Is.Null);
            Assert.That(info.Runs, Is.Empty);
            Assert.That(info.RunCount, Is.Zero);
            Assert.That(n, Is.EqualTo(0));

            var log = _logWriter.ToString();

            Assert.That(log, Does.Contain($"{exception.GetType().FullName}: {exception.Message}"));

            Assert.Pass(_logWriter.ToString());
        }
예제 #26
0
        public async Task Output_SetAfterFirstRun_NextTimeRunsWithNewOutput()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            ISchedule schedule = new ConcreteSchedule(
                start.AddSeconds(1),
                start.AddSeconds(3));

            job.Schedule = schedule;

            var writer1 = new StringWriterWithEncoding(Encoding.UTF8);
            var writer2 = new StringWriterWithEncoding(Encoding.UTF8);

            job.Routine = async(parameter, tracker, writer, token) =>
            {
                for (var i = 0; i < 5; i++)
                {
                    await writer.WriteAsync(i.ToString());
                }

                await Task.Delay(200, token);
            };

            job.IsEnabled = true;

            // Act
            await timeMachine.WaitUntilSecondsElapse(start, 0.8);

            job.Output = writer1;

            await timeMachine.WaitUntilSecondsElapse(start, 2.7);

            job.Output = writer2;

            await timeMachine.WaitUntilSecondsElapse(start, 4.8);

            var info = job.GetInfo(null);

            // Assert
            Assert.That(info.CurrentRun, Is.Null);

            Assert.That(info.RunCount, Is.EqualTo(2));
            Assert.That(info.Runs, Has.Count.EqualTo(2));

            Assert.That(writer1.ToString(), Is.EqualTo("01234"));
            Assert.That(writer2.ToString(), Is.EqualTo("01234"));
        }
예제 #27
0
        public async Task Routine_ReturnsCompletedTask_LogsCompletedTask()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);


            var output = new StringWriterWithEncoding(Encoding.UTF8);

            job.Output = output;


            JobDelegate routine = (parameter, tracker, writer, token) =>
            {
                writer.WriteLine("Hi there!");
                return(Task.CompletedTask);
            };

            job.Schedule = new ConcreteSchedule(
                start.AddSeconds(1));

            job.IsEnabled = true;

            // Act
            job.Routine = routine;
            var updatedRoutine = job.Routine;

            await timeMachine.WaitUntilSecondsElapse(start, 1.5); // will fail by this time

            var outputResult = output.ToString();

            var info = job.GetInfo(null);

            // Assert
            Assert.That(updatedRoutine, Is.SameAs(routine));
            Assert.That(outputResult, Does.Contain("Hi there!"));

            Assert.That(info.CurrentRun, Is.Null);

            Assert.That(info.RunCount, Is.EqualTo(1));
            var run = info.Runs.Single();

            Assert.That(run.Status, Is.EqualTo(JobRunStatus.Completed));

            var log = _logWriter.ToString();

            Assert.That(log,
                        Does.Contain($"Job 'my-job' completed synchronously. Reason of start was 'ScheduleDueTime'."));
        }
예제 #28
0
        public async Task Schedule_ScheduleReturnsDueTimeBeforeNow_DueTimeSetToNever()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);
            var job = jobManager.Create("my-job");

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var n = 0;

            job.IsEnabled = true;
            job.Routine   = (parameter, tracker, output, token) =>
            {
                n = 100;
                return(Task.CompletedTask);
            };

            // set some normal schedule first
            job.Schedule = new SimpleSchedule(SimpleScheduleKind.Minute, 1, start);
            var normalDueTime = job.GetInfo(null).NextDueTime;

            // Act
            var scheduleMock = new Mock <ISchedule>();

            scheduleMock
            .Setup(x => x.GetDueTimeAfter(It.IsAny <DateTimeOffset>()))
            .Returns(start.AddSeconds(-1));     // due time in the past

            job.Schedule = scheduleMock.Object;

            await Task.Delay(3000); // let job try to start (it should not start though)

            var info           = job.GetInfo(null);
            var faultedDueTime = info.NextDueTime;

            // Assert
            Assert.That(normalDueTime, Is.EqualTo(start.AddMinutes(1)));
            Assert.That(faultedDueTime.Year, Is.EqualTo(9000));

            // job never ran
            Assert.That(info.CurrentRun, Is.Null);
            Assert.That(info.Runs, Is.Empty);
            Assert.That(info.RunCount, Is.Zero);
            Assert.That(n, Is.EqualTo(0));

            var log = _logWriter.ToString();

            Assert.That(log, Does.Contain($"Due time is earlier than current time. Due time is changed to 'never'."));

            Assert.Pass(_logWriter.ToString());
        }
        public async Task ProgressTracker_SetOnTheFly_RunsWithOldParameterAndNextTimeRunsWithNewProgressTracker()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(true);

            var start       = "2000-01-01Z".ToUtcDateOffset();
            var timeMachine = ShiftedTimeProvider.CreateTimeMachine(start);

            TimeProvider.Override(timeMachine);

            var job = jobManager.Create("my-job");

            ISchedule schedule = new ConcreteSchedule(
                start.AddSeconds(1),
                start.AddSeconds(3));

            job.Schedule = schedule;

            var tracker1 = new MockProgressTracker();
            var tracker2 = new MockProgressTracker();

            job.Routine = async(parameter, tracker, writer, token) =>
            {
                for (int i = 0; i < 5; i++)
                {
                    tracker.UpdateProgress((decimal)i * 20, null);
                }

                await Task.Delay(200, token);
            };

            job.IsEnabled = true;

            // Act
            await timeMachine.WaitUntilSecondsElapse(start, 0.8);

            job.ProgressTracker = tracker1;

            await timeMachine.WaitUntilSecondsElapse(start, 1.3);

            job.ProgressTracker = tracker2;

            await timeMachine.WaitUntilSecondsElapse(start, 4.8);

            var info = job.GetInfo(null);

            // Assert
            Assert.That(info.CurrentRun, Is.Null);
            Assert.That(info.RunCount, Is.EqualTo(2));
            Assert.That(info.Runs, Has.Count.EqualTo(2));

            CollectionAssert.AreEquivalent(new decimal[] { 0m, 20m, 40m, 60m, 80m }, tracker1.GetList());
            CollectionAssert.AreEquivalent(new decimal[] { 0m, 20m, 40m, 60m, 80m }, tracker2.GetList());
        }
예제 #30
0
        public void Create_NotStarted_ThrowsInvalidJobOperationException()
        {
            // Arrange
            using IJobManager jobManager = TestHelper.CreateJobManager(false);

            // Act
            var ex = Assert.Throws <InvalidJobOperationException>(() => jobManager.Create("job1"));

            // Assert
            Assert.That(ex.Message, Is.EqualTo($"'{typeof(IJobManager).FullName}' not started."));
        }