Skip to content

Commit

Permalink
Fixed unit tests (#63)
Browse files Browse the repository at this point in the history
* Fixed unit tests

* Removed IAdderJobs from ExecutorJobs and ExecutorJobsTests completely

---------

Co-authored-by: a.skotarev <[email protected]>
  • Loading branch information
sskalex and a.skotarev authored May 16, 2023
1 parent c20e2fc commit 3679e35
Show file tree
Hide file tree
Showing 16 changed files with 10 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/Horarium.AspNetCore/HorariumServerHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Horarium.Interfaces;
Expand Down
8 changes: 0 additions & 8 deletions src/Horarium.InMemory/InMemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ public Task AddRecurrentJobSettings(RecurrentJobSettings settings)
return Task.CompletedTask;
}

public Task<string> GetCronForRecurrentJob(string jobKey)
{
if (!_settingsStorage.TryGetValue(jobKey, out var settings))
throw new Exception($"Settings for recurrent job (jobKey = {jobKey}) aren't found");

return Task.FromResult(settings.Cron);
}

public Task<Dictionary<JobStatus, int>> GetJobStatistic()
{
return Task.FromResult(_storage.GetStatistics());
Expand Down
1 change: 0 additions & 1 deletion src/Horarium.IntegrationTest/FallbackJobTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Horarium.IntegrationTest.Jobs;
using Horarium.IntegrationTest.Jobs.Fallback;
using Xunit;

Expand Down
1 change: 0 additions & 1 deletion src/Horarium.IntegrationTest/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using Horarium.InMemory;
using Horarium.Mongo;
using Horarium.Repository;
Expand Down
3 changes: 1 addition & 2 deletions src/Horarium.IntegrationTest/Jobs/Fallback/FallbackJob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Horarium.Interfaces;

namespace Horarium.IntegrationTest.Jobs.Fallback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Horarium.Interfaces;

namespace Horarium.IntegrationTest.Jobs.Fallback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Horarium.Fallbacks;
using Horarium.Interfaces;

namespace Horarium.IntegrationTest.Jobs.Fallback
Expand Down
3 changes: 1 addition & 2 deletions src/Horarium.IntegrationTest/Jobs/SequenceJob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Horarium.Interfaces;

Expand Down
3 changes: 0 additions & 3 deletions src/Horarium.IntegrationTest/TestParallelsWorkTwoManagers.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Horarium.InMemory;
using Horarium.IntegrationTest.Jobs;
using Xunit;

Expand Down
14 changes: 0 additions & 14 deletions src/Horarium.Mongo/MongoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,6 @@ await collection.ReplaceOneAsync(
});
}

public async Task<string> GetCronForRecurrentJob(string jobKey)
{
var collection = _mongoClientProvider.GetCollection<RecurrentJobSettingsMongo>();

var recurrentJobSettingsCollection = await collection.FindAsync(x => x.JobKey == jobKey);

var recurrentJobSettings = recurrentJobSettingsCollection.FirstOrDefault();

if (recurrentJobSettings == null)
throw new Exception("Не найдены настройки для рекуррентного джоба");

return recurrentJobSettings.Cron;
}

public async Task RemoveJob(string jobId)
{
var collection = _mongoClientProvider.GetCollection<JobMongoModel>();
Expand Down
33 changes: 5 additions & 28 deletions src/Horarium.Test/ExecutorJobTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json;
using Horarium.Handlers;
using Horarium.Interfaces;
using Horarium.Repository;
Expand All @@ -22,7 +21,6 @@ public async Task GetJobFromScope_ThenDisposeScope()

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand Down Expand Up @@ -50,7 +48,6 @@ public async Task ExceptionInJob_SaveError()

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand All @@ -77,7 +74,6 @@ public async Task ExceptionCreateJob_LoggingError()

var executorJob = new ExecutorJob(
Mock.Of<IJobRepository>(),
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down Expand Up @@ -106,7 +102,6 @@ public async Task ExceptionInJob_SaveError_WhenAllRepeatesIsFailed()

var executorJob = new ExecutorJob(
Mock.Of<IJobRepository>(),
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand Down Expand Up @@ -134,7 +129,6 @@ public async Task ExceptionInJob_SaveError_WhenNotAllRepeatesIsFailed()

var executorJob = new ExecutorJob(
Mock.Of<IJobRepository>(),
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand All @@ -160,12 +154,9 @@ public async Task RecurrentJob_RescheduleAfterRun()

jobScopeMock.Setup(x => x.CreateJob(It.IsAny<Type>()))
.Returns(() => new TestReccurrentJob());
jobRepositoryMock.Setup(x => x.GetCronForRecurrentJob(It.IsAny<string>()))
.ReturnsAsync(cron);

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand All @@ -183,23 +174,19 @@ await executorJob.Execute(new JobMetadata()
}

[Fact]
public async Task RecurrentJob_ThrowException_AddRecurrentJobNextStart()
public async Task RecurrentJob_ThrowException_RescheduleJob()
{
// Arrange
var jobRepositoryMock = new Mock<IJobRepository>();
var (jobScopeFactoryMock, jobScopeMock) = CreateScopeMock();
var jobAdderJob = new Mock<IAdderJobs>();

const string cron = "*/15 * * * * *";

jobScopeMock.Setup(x => x.CreateJob(It.IsAny<Type>()))
.Returns(() => throw new Exception());
jobRepositoryMock.Setup(x => x.GetCronForRecurrentJob(It.IsAny<string>()))
.ReturnsAsync(cron);

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
jobAdderJob.Object,
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand All @@ -218,10 +205,10 @@ public async Task RecurrentJob_ThrowException_AddRecurrentJobNextStart()
await executorJob.Execute(job);

// Assert
jobRepositoryMock.Verify(x => x.GetCronForRecurrentJob(It.IsAny<string>()), Times.Once);
jobAdderJob.Verify(x => x.AddRecurrentJob(It.Is<JobMetadata>(j => j.JobType == job.JobType &&
j.JobKey == job.JobKey &&
j.Cron == job.Cron)));
jobRepositoryMock.Verify(x => x.RescheduleRecurrentJob(
job.JobId,
Utils.ParseAndGetNextOccurrence(cron).Value,
It.IsAny<Exception>()));
}

[Fact]
Expand All @@ -234,14 +221,11 @@ public async Task RecurrentJob_ExceptionCreateJob_LoggingError()

const string cron = "*/15 * * * * *";

jobRepositoryMock.Setup(x => x.GetCronForRecurrentJob(It.IsAny<string>()))
.ReturnsAsync(cron);
jobScopeMock.Setup(x => x.CreateJob(It.IsAny<Type>()))
.Throws<Exception>();

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down Expand Up @@ -276,7 +260,6 @@ public async Task JobWithNextJob_DelayIsNull_AddedJobStartAtEqualNow()

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down Expand Up @@ -316,7 +299,6 @@ public async Task JobWithNextJob_DelayIsZero_AddedJobStartAtEqualNow()

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand Down Expand Up @@ -357,7 +339,6 @@ public async Task JobWithNextJob_DelayContains_AddedJobStartAtEqualNowPlusDelay(

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object
Expand Down Expand Up @@ -396,7 +377,6 @@ public async Task ThrowExceptionJobWithoutFailedStrategy_UseDefaultFailedStrateg

var executorJob = new ExecutorJob(
Mock.Of<IJobRepository>(),
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand All @@ -423,7 +403,6 @@ public async Task ThrowExceptionJobWithFailedStrategy_UseFailedStrategyFromJob()

var executorJob = new ExecutorJob(
Mock.Of<IJobRepository>(),
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down Expand Up @@ -453,7 +432,6 @@ public async Task ThrowExceptionJobCountStartedEqMax_DontCallFailedStrategyAndFa

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down Expand Up @@ -482,7 +460,6 @@ public async Task FailedEventOfAJobThrows_FailedJobCalledNevertheless()

var executorJob = new ExecutorJob(
jobRepositoryMock.Object,
Mock.Of<IAdderJobs>(),
new HorariumSettings
{
JobScopeFactory = jobScopeFactoryMock.Object,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Horarium.Fallbacks;
using Horarium.Interfaces;

namespace Horarium.Builders.Parameterized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Horarium.Fallbacks;
using Horarium.Interfaces;

namespace Horarium.Builders.Parameterized
Expand Down
1 change: 0 additions & 1 deletion src/Horarium/Handlers/ExecutorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ExecutorJob : IExecutorJob

public ExecutorJob(
IJobRepository jobRepository,
IAdderJobs adderJobs,
HorariumSettings settings)
{
_jobRepository = jobRepository;
Expand Down
7 changes: 2 additions & 5 deletions src/Horarium/HorariumServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Horarium.Handlers;
Expand All @@ -14,7 +13,6 @@ public class HorariumServer : HorariumClient, IHorarium
{
private readonly HorariumSettings _settings;
private IRunnerJobs _runnerJobs;
private readonly IAdderJobs _adderJobs;

private readonly IJobRepository _jobRepository;

Expand All @@ -27,13 +25,12 @@ public HorariumServer(IJobRepository jobRepository, HorariumSettings settings)
: base(jobRepository, settings)
{
_settings = settings;
_adderJobs = new AdderJobs(jobRepository, _settings.JsonSerializerSettings);
_jobRepository = jobRepository;
}

public void Start()
{
var executorJob = new ExecutorJob(_jobRepository, _adderJobs, _settings);
var executorJob = new ExecutorJob(_jobRepository, _settings);

_runnerJobs = new RunnerJobs(_jobRepository, _settings, _settings.JsonSerializerSettings, _settings.Logger,
executorJob, new UncompletedTaskList());
Expand Down
2 changes: 0 additions & 2 deletions src/Horarium/Repository/IJobRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface IJobRepository

Task AddRecurrentJobSettings(RecurrentJobSettings settings);

Task<string> GetCronForRecurrentJob(string jobKey);

Task<Dictionary<JobStatus, int>> GetJobStatistic();

Task RescheduleRecurrentJob(string jobId, DateTime startAt, Exception error);
Expand Down

0 comments on commit 3679e35

Please sign in to comment.