2011-05-22 16:53:21 +00:00
|
|
|
|
// ReSharper disable RedundantUsingDirective
|
2011-10-20 23:42:17 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
using System.Linq;
|
2011-05-22 16:53:21 +00:00
|
|
|
|
using System;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using AutoMoq;
|
2011-06-02 21:06:46 +00:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NUnit.Framework;
|
2011-08-22 00:48:37 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Jobs;
|
2011-05-19 03:55:35 +00:00
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
2011-10-24 05:54:09 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
namespace NzbDrone.Core.Test.ProviderTests.JobProviderTests
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
// ReSharper disable InconsistentNaming
|
2011-05-19 03:55:35 +00:00
|
|
|
|
public class JobProviderTest : TestBase
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
|
|
|
|
[Test]
|
2011-04-22 05:46:47 +00:00
|
|
|
|
public void Run_Jobs_Updates_Last_Execution()
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new FakeJob() };
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
//Act
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
//Assert
|
|
|
|
|
var settings = timerProvider.All();
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
Assert.AreNotEqual(DateTime.MinValue, settings[0].LastExecution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Run_Jobs_Updates_Last_Execution_Mark_as_unsuccesful()
|
|
|
|
|
{
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new BrokenJob() };
|
2011-04-22 05:46:47 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-22 05:46:47 +00:00
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-04-22 05:46:47 +00:00
|
|
|
|
|
2011-07-11 04:53:34 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
//Assert
|
|
|
|
|
var settings = timerProvider.All();
|
|
|
|
|
Assert.AreNotEqual(DateTime.MinValue, settings[0].LastExecution);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
settings[0].Success.Should().BeFalse();
|
2011-05-19 03:55:35 +00:00
|
|
|
|
ExceptionVerification.ExcpectedErrors(1);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2011-07-11 05:15:37 +00:00
|
|
|
|
public void scheduler_skips_jobs_that_arent_mature_yet()
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var BaseFakeJob = new FakeJob();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { BaseFakeJob };
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-07-11 04:53:34 +00:00
|
|
|
|
Thread.Sleep(500);
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-07-11 04:53:34 +00:00
|
|
|
|
Thread.Sleep(500);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
BaseFakeJob.ExecutionCount.Should().Be(1);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
//This test will confirm that the concurrency checks are rest
|
|
|
|
|
//after execution so the job can successfully run.
|
|
|
|
|
public void can_run_async_job_again()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var BaseFakeJob = new FakeJob();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
2011-07-11 05:15:37 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { BaseFakeJob };
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(FakeJob));
|
2011-07-11 04:53:34 +00:00
|
|
|
|
Thread.Sleep(1000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.QueueJob(typeof(FakeJob));
|
2011-10-23 02:31:28 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
BaseFakeJob.ExecutionCount.Should().Be(2);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-20 06:25:39 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void no_concurent_jobs()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new SlowJob() };
|
2011-05-20 06:25:39 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-20 06:25:39 +00:00
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(SlowJob), 1);
|
|
|
|
|
jobProvider.QueueJob(typeof(SlowJob), 2);
|
|
|
|
|
jobProvider.QueueJob(typeof(SlowJob), 3);
|
2011-05-20 06:25:39 +00:00
|
|
|
|
|
|
|
|
|
|
2011-10-23 02:31:28 +00:00
|
|
|
|
Thread.Sleep(5000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-05-27 02:12:28 +00:00
|
|
|
|
//Asserts are done in ExceptionVerification
|
2011-05-20 06:25:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
|
[Test]
|
|
|
|
|
//This test will confirm that the concurrency checks are rest
|
|
|
|
|
//after execution so the job can successfully run.
|
|
|
|
|
public void can_run_broken_async_job_again()
|
|
|
|
|
{
|
2011-07-11 04:53:34 +00:00
|
|
|
|
var brokenJob = new BrokenJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { brokenJob };
|
2011-04-21 01:26:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-21 01:26:13 +00:00
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(BrokenJob));
|
2011-04-21 01:26:13 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.QueueJob(typeof(BrokenJob));
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
|
|
2011-05-19 03:55:35 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-08-22 00:48:37 +00:00
|
|
|
|
brokenJob.ExecutionCount.Should().Be(2);
|
2011-05-19 03:55:35 +00:00
|
|
|
|
ExceptionVerification.ExcpectedErrors(2);
|
2011-04-21 01:26:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void can_run_two_jobs_at_the_same_time()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
WithRealDb();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var fakeJob = new FakeJob();
|
|
|
|
|
IList<IJob> fakeJobs = new List<IJob> { fakeJob };
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
Mocker.SetConstant(fakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var jobProvider = Mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
jobProvider.QueueScheduled();
|
|
|
|
|
jobProvider.QueueScheduled();
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
Thread.Sleep(2000);
|
|
|
|
|
|
|
|
|
|
fakeJob.ExecutionCount.Should().Be(1);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void can_queue_jobs_at_the_same_time()
|
|
|
|
|
{
|
|
|
|
|
var slowJob = new SlowJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var BaseFakeJob = new FakeJob();
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { slowJob, BaseFakeJob };
|
2011-05-17 07:04:49 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
|
|
|
|
|
jobProvider.QueueJob(typeof(SlowJob));
|
|
|
|
|
var thread1 = new Thread(() => jobProvider.QueueJob(typeof(FakeJob)));
|
|
|
|
|
var thread2 = new Thread(() => jobProvider.QueueJob(typeof(FakeJob)));
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
|
|
|
|
thread1.Start();
|
|
|
|
|
thread2.Start();
|
|
|
|
|
|
|
|
|
|
thread1.Join();
|
|
|
|
|
thread2.Join();
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
BaseFakeJob.ExecutionCount.Should().Be(1);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-05-17 07:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void Init_Jobs()
|
|
|
|
|
{
|
|
|
|
|
var fakeTimer = new FakeJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { fakeTimer };
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
|
|
|
|
|
|
|
|
|
var timers = timerProvider.All();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-06-02 21:06:46 +00:00
|
|
|
|
|
|
|
|
|
timers.Should().HaveCount(1);
|
2011-06-23 06:56:17 +00:00
|
|
|
|
timers[0].Interval.Should().Be(fakeTimer.DefaultInterval);
|
|
|
|
|
timers[0].Name.Should().Be(fakeTimer.Name);
|
|
|
|
|
timers[0].TypeName.Should().Be(fakeTimer.GetType().ToString());
|
|
|
|
|
timers[0].LastExecution.Should().HaveYear(2000);
|
|
|
|
|
timers[0].Enable.Should().BeTrue();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Init_Timers_only_registers_once()
|
|
|
|
|
{
|
2011-06-17 19:50:49 +00:00
|
|
|
|
var repo = MockLib.GetEmptyDatabase();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
var fakeTimer = new FakeJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { fakeTimer };
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
mocker.SetConstant(repo);
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mocker2 = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
mocker2.SetConstant(repo);
|
|
|
|
|
var assertTimerProvider = mocker2.Resolve<JobProvider>();
|
|
|
|
|
|
|
|
|
|
var timers = assertTimerProvider.All();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-06-02 21:06:46 +00:00
|
|
|
|
timers.Should().HaveCount(1);
|
2011-06-23 06:56:17 +00:00
|
|
|
|
timers[0].Enable.Should().BeTrue();
|
2011-05-11 18:25:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Init_Timers_sets_interval_0_to_disabled()
|
|
|
|
|
{
|
2011-06-17 19:50:49 +00:00
|
|
|
|
var repo = MockLib.GetEmptyDatabase();
|
2011-05-11 18:25:32 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
var disabledJob = new DisabledJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { disabledJob };
|
2011-05-11 18:25:32 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
mocker.SetConstant(repo);
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-11 18:25:32 +00:00
|
|
|
|
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mocker2 = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
mocker2.SetConstant(repo);
|
|
|
|
|
var assertTimerProvider = mocker2.Resolve<JobProvider>();
|
|
|
|
|
|
|
|
|
|
var timers = assertTimerProvider.All();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-06-02 21:06:46 +00:00
|
|
|
|
timers.Should().HaveCount(1);
|
2011-05-11 18:25:32 +00:00
|
|
|
|
Assert.IsFalse(timers[0].Enable);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 04:01:01 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void Get_Next_Execution_Time()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new FakeJob() };
|
2011-05-17 04:01:01 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-17 04:01:01 +00:00
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-05-17 04:01:01 +00:00
|
|
|
|
var next = timerProvider.NextScheduledRun(typeof(FakeJob));
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-05-17 04:01:01 +00:00
|
|
|
|
//Assert
|
|
|
|
|
var settings = timerProvider.All();
|
|
|
|
|
Assert.IsNotEmpty(settings);
|
|
|
|
|
Assert.AreEqual(next, settings[0].LastExecution.AddMinutes(15));
|
|
|
|
|
}
|
2011-05-17 07:16:45 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Disabled_isnt_run_by_scheduler()
|
|
|
|
|
{
|
2011-06-17 19:50:49 +00:00
|
|
|
|
var repo = MockLib.GetEmptyDatabase();
|
2011-05-17 07:16:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var disabledJob = new DisabledJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { disabledJob };
|
2011-05-17 07:16:45 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
mocker.SetConstant(repo);
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-17 07:16:45 +00:00
|
|
|
|
|
|
|
|
|
var timerProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
timerProvider.Initialize();
|
|
|
|
|
|
2011-08-03 16:29:03 +00:00
|
|
|
|
timerProvider.QueueScheduled();
|
2011-05-17 07:16:45 +00:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-08-22 00:48:37 +00:00
|
|
|
|
Assert.AreEqual(0, disabledJob.ExecutionCount);
|
2011-05-17 07:16:45 +00:00
|
|
|
|
}
|
2011-05-18 05:29:23 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SingleId_do_not_update_last_execution()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new FakeJob() };
|
2011-05-18 05:29:23 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-18 05:29:23 +00:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(FakeJob), 10);
|
2011-05-18 05:29:23 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var settings = jobProvider.All();
|
2011-06-23 06:56:17 +00:00
|
|
|
|
settings.Should().NotBeEmpty();
|
|
|
|
|
settings[0].LastExecution.Should().HaveYear(2000);
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-05-18 05:29:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SingleId_do_not_set_success()
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new FakeJob() };
|
2011-05-18 05:29:23 +00:00
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-06-17 19:50:49 +00:00
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-05-18 05:29:23 +00:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(FakeJob), 10);
|
2011-05-18 05:29:23 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var settings = jobProvider.All();
|
2011-05-18 05:29:23 +00:00
|
|
|
|
Assert.IsNotEmpty(settings);
|
|
|
|
|
Assert.IsFalse(settings[0].Success);
|
|
|
|
|
}
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void existing_queue_should_start_queue_if_not_running()
|
|
|
|
|
{
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var BaseFakeJob = new FakeJob();
|
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { BaseFakeJob };
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-08-22 00:48:37 +00:00
|
|
|
|
var fakeQueueItem = new JobQueueItem
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
JobType = BaseFakeJob.GetType(),
|
2011-08-22 00:48:37 +00:00
|
|
|
|
TargetId = 12,
|
|
|
|
|
SecondaryTargetId = 0
|
|
|
|
|
};
|
2011-10-23 05:39:14 +00:00
|
|
|
|
|
2011-07-11 04:53:34 +00:00
|
|
|
|
//Act
|
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
jobProvider.Initialize();
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Add(fakeQueueItem);
|
2011-11-07 06:26:21 +00:00
|
|
|
|
jobProvider.QueueJob(BaseFakeJob.GetType(), 12);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-11-07 06:26:21 +00:00
|
|
|
|
BaseFakeJob.ExecutionCount.Should().Be(1);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Item_added_to_queue_while_scheduler_runs_is_executed()
|
|
|
|
|
{
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
|
|
|
|
var slowJob = new SlowJob();
|
|
|
|
|
var disabledJob = new DisabledJob();
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { slowJob, disabledJob };
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
|
|
|
|
mocker.SetConstant(MockLib.GetEmptyDatabase());
|
2011-11-07 06:26:21 +00:00
|
|
|
|
mocker.SetConstant(BaseFakeJobs);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
var jobProvider = mocker.Resolve<JobProvider>();
|
|
|
|
|
|
|
|
|
|
jobProvider.Initialize();
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var _jobThread = new Thread(jobProvider.QueueScheduled);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
_jobThread.Start();
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.QueueJob(typeof(DisabledJob), 12);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
|
|
|
|
|
//Assert
|
2011-10-23 21:53:24 +00:00
|
|
|
|
jobProvider.Queue.Should().BeEmpty();
|
2011-08-22 00:48:37 +00:00
|
|
|
|
slowJob.ExecutionCount.Should().Be(1);
|
|
|
|
|
disabledJob.ExecutionCount.Should().Be(1);
|
2011-07-11 04:53:34 +00:00
|
|
|
|
}
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void trygin_to_queue_unregistered_job_should_fail()
|
2011-05-11 18:25:32 +00:00
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
WithRealDb();
|
2011-05-11 18:25:32 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
IList<IJob> BaseFakeJobs = new List<IJob> { new SlowJob(), new DisabledJob() };
|
2011-05-17 07:16:45 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
Mocker.SetConstant(BaseFakeJobs);
|
2011-05-11 18:25:32 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var jobProvider = Mocker.Resolve<JobProvider>();
|
2011-04-21 01:26:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
jobProvider.Initialize();
|
|
|
|
|
jobProvider.QueueJob(typeof(string));
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
ExceptionVerification.ExcpectedErrors(1);
|
2011-04-21 01:26:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|