2013-04-01 06:49:46 +00:00
|
|
|
|
using System.Linq;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Core.Jobs;
|
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.JobTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public class JobRepositoryFixture : DbTest<JobRepository, JobDefinition>
|
2013-02-18 07:59:43 +00:00
|
|
|
|
{
|
|
|
|
|
FakeJob _fakeJob;
|
|
|
|
|
DisabledJob _disabledJob;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
_fakeJob = new FakeJob();
|
|
|
|
|
_disabledJob = new DisabledJob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Init_should_add_defintaions()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IJob> baseFakeJobs = new List<IJob> { _fakeJob };
|
|
|
|
|
Mocker.SetConstant(baseFakeJobs);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subject.Init();
|
|
|
|
|
|
|
|
|
|
Storage.All().Should().HaveCount(1);
|
2013-03-02 20:14:18 +00:00
|
|
|
|
StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes);
|
|
|
|
|
StoredModel.Name.Should().Be(_fakeJob.Name);
|
2013-03-25 04:36:24 +00:00
|
|
|
|
StoredModel.Type.Should().Be(_fakeJob.GetType().ToString());
|
2013-03-02 20:14:18 +00:00
|
|
|
|
StoredModel.LastExecution.Should().HaveYear(DateTime.Now.Year);
|
|
|
|
|
StoredModel.LastExecution.Should().HaveMonth(DateTime.Now.Month);
|
|
|
|
|
StoredModel.LastExecution.Should().HaveDay(DateTime.Today.Day);
|
|
|
|
|
StoredModel.Enable.Should().BeTrue();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void inti_should_removed_jobs_that_no_longer_exist()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IJob> fakeJobs = new List<IJob> { _fakeJob };
|
|
|
|
|
Mocker.SetConstant(fakeJobs);
|
|
|
|
|
|
|
|
|
|
var deletedJob = Builder<JobDefinition>.CreateNew()
|
2013-04-01 06:49:46 +00:00
|
|
|
|
.With(c => c.Id = 0)
|
|
|
|
|
.Build();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
Db.Insert(deletedJob);
|
2013-04-01 06:49:46 +00:00
|
|
|
|
|
|
|
|
|
//Make sure deleted job is stored
|
|
|
|
|
AllStoredModels.Should().HaveCount(1);
|
|
|
|
|
AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type);
|
|
|
|
|
|
2013-02-18 07:59:43 +00:00
|
|
|
|
Subject.Init();
|
|
|
|
|
|
2013-04-01 06:49:46 +00:00
|
|
|
|
//Make sure init has cleaned up the deleted job
|
2013-03-02 20:14:18 +00:00
|
|
|
|
AllStoredModels.Should().HaveCount(1);
|
2013-03-25 04:36:24 +00:00
|
|
|
|
AllStoredModels.Should().NotContain(c => c.Type == deletedJob.Type);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2013-04-01 06:49:46 +00:00
|
|
|
|
public void init_should_removed_jobs_that_no_longer_exist_even_with_same_name()
|
2013-02-18 07:59:43 +00:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<IJob> fakeJobs = new List<IJob> { _fakeJob };
|
|
|
|
|
Mocker.SetConstant(fakeJobs);
|
|
|
|
|
|
|
|
|
|
var deletedJob = Builder<JobDefinition>.CreateNew()
|
|
|
|
|
.With(c => c.Name = _fakeJob.Name)
|
2013-02-26 03:58:57 +00:00
|
|
|
|
.With(c => c.Id = 0)
|
2013-02-18 07:59:43 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
Db.Insert(deletedJob);
|
|
|
|
|
|
2013-04-01 06:49:46 +00:00
|
|
|
|
//Make sure deleted job is stored
|
|
|
|
|
AllStoredModels.Should().HaveCount(1);
|
|
|
|
|
AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type);
|
|
|
|
|
|
|
|
|
|
Subject.Init();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
2013-04-01 06:49:46 +00:00
|
|
|
|
//Make sure init has cleaned up the deleted job
|
2013-03-02 20:14:18 +00:00
|
|
|
|
AllStoredModels.Should().HaveCount(1);
|
2013-03-25 04:36:24 +00:00
|
|
|
|
AllStoredModels.Should().NotContain(c => c.Type == deletedJob.Type);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void init_should_update_existing_job()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var oldJob = Builder<JobDefinition>.CreateNew()
|
2013-02-26 03:58:57 +00:00
|
|
|
|
.With(c => c.Id = 0)
|
2013-02-18 07:59:43 +00:00
|
|
|
|
.With(c => c.Name = "OldName")
|
2013-03-25 04:36:24 +00:00
|
|
|
|
.With(c => c.Type = typeof(FakeJob).ToString())
|
2013-02-18 07:59:43 +00:00
|
|
|
|
.With(c => c.Interval = 0)
|
|
|
|
|
.With(c => c.Enable = true)
|
|
|
|
|
.With(c => c.Success = true)
|
|
|
|
|
.With(c => c.LastExecution = DateTime.Now.AddDays(-7).Date)
|
|
|
|
|
.Build();
|
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
Storage.Insert(oldJob);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
var newJob = new FakeJob();
|
|
|
|
|
|
|
|
|
|
IEnumerable<IJob> fakeJobs = new List<IJob> { newJob };
|
|
|
|
|
Mocker.SetConstant(fakeJobs);
|
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
Subject.Init();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
|
2013-03-02 20:14:18 +00:00
|
|
|
|
AllStoredModels.Should().HaveCount(1);
|
2013-03-25 04:36:24 +00:00
|
|
|
|
StoredModel.Type.Should().Be(newJob.GetType().FullName);
|
2013-03-02 20:14:18 +00:00
|
|
|
|
StoredModel.Name.Should().Be(newJob.Name);
|
|
|
|
|
StoredModel.Interval.Should().Be((int)newJob.DefaultInterval.TotalMinutes);
|
|
|
|
|
StoredModel.Enable.Should().Be(true);
|
|
|
|
|
StoredModel.Success.Should().Be(oldJob.Success);
|
|
|
|
|
StoredModel.LastExecution.Should().Be(oldJob.LastExecution);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void jobs_with_zero_interval_are_registered_as_disabled()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IJob> fakeJobs = new List<IJob> { _disabledJob };
|
|
|
|
|
Mocker.SetConstant(fakeJobs);
|
|
|
|
|
|
|
|
|
|
Subject.Init();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.All().Should().HaveCount(1);
|
|
|
|
|
Storage.All().First().Enable.Should().BeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 20:14:18 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void pending_job_should_get_jobs_that_have_matured()
|
|
|
|
|
{
|
|
|
|
|
var oldJob = Builder<JobDefinition>.CreateNew()
|
|
|
|
|
.With(c => c.Id = 0)
|
|
|
|
|
.With(c => c.Interval = 1)
|
|
|
|
|
.With(c => c.Enable = true)
|
|
|
|
|
.With(c => c.Success = true)
|
|
|
|
|
.With(c => c.LastExecution = DateTime.Now.AddMinutes(-5))
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.Insert(oldJob);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subject.GetPendingJobs().Should().HaveCount(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void pending_job_should_not_get_jobs_that_havent_matured()
|
|
|
|
|
{
|
|
|
|
|
var recent = Builder<JobDefinition>.CreateNew()
|
|
|
|
|
.With(c => c.Id = 0)
|
|
|
|
|
.With(c => c.Interval = 60)
|
|
|
|
|
.With(c => c.Enable = true)
|
|
|
|
|
.With(c => c.Success = true)
|
|
|
|
|
.With(c => c.LastExecution = DateTime.Now.AddMinutes(-5))
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.Insert(recent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subject.GetPendingJobs().Should().BeEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
/* [Test]
|
|
|
|
|
public void disabled_jobs_arent_run_by_scheduler()
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IJob> BaseFakeJobs = new List<IJob> { disabledJob };
|
|
|
|
|
Mocker.SetConstant(BaseFakeJobs);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
var jobProvider = Mocker.Resolve<JobController>();
|
|
|
|
|
jobProvider.QueueScheduled();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
WaitForQueue();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
disabledJob.ExecutionCount.Should().Be(0);
|
|
|
|
|
}*/
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|