Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebakov Dmitriy Sergeevich committed Nov 27, 2021
1 parent 31f5d89 commit f47d280
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Horarium.Test/RunnerJobTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,87 @@ public async Task Start_ExecutionWithDelay_WithThrottle()
// Assert
jobRepositoryMock.Verify(r => r.GetReadyJob(It.IsAny<string>(), It.IsAny<TimeSpan>()), Times.Once);
}

[Fact]
public async Task Start_ExecutionWithDelay_IncreaseInterval()
{
// Arrange
var jobRepositoryMock = new Mock<IJobRepository>();

jobRepositoryMock.Setup(x => x.GetReadyJob(It.IsAny<string>(), It.IsAny<TimeSpan>()))
.ReturnsAsync(() => null);

var settings = new HorariumSettings
{
IntervalStartJob = TimeSpan.FromSeconds(1),
JobThrottleSettings = new JobThrottleSettings
{
UseJobThrottle = true,
IntervalMultiplier = 1,
JobRetrievalAttempts = 1,
}
};

var runnerJobs = new RunnerJobs(jobRepositoryMock.Object,
settings,
new JsonSerializerSettings(),
Mock.Of<IHorariumLogger>(),
Mock.Of<IExecutorJob>());

// Act
runnerJobs.Start();
await Task.Delay(settings.IntervalStartJob - TimeSpan.FromMilliseconds(500));
jobRepositoryMock.Invocations.Clear();

var interval = settings.IntervalStartJob +
settings.IntervalStartJob.Multiply(settings.JobThrottleSettings.IntervalMultiplier);
await Task.Delay(interval);
interval += settings.IntervalStartJob.Multiply(settings.JobThrottleSettings.IntervalMultiplier);
await Task.Delay(interval);
interval += settings.IntervalStartJob.Multiply(settings.JobThrottleSettings.IntervalMultiplier);
await Task.Delay(interval);

// Assert
jobRepositoryMock.Verify(r => r.GetReadyJob(It.IsAny<string>(), It.IsAny<TimeSpan>()), Times.Exactly(3));
}

[Fact]
public async Task Start_ExecutionWithDelay_MaxInterval()
{
// Arrange
var jobRepositoryMock = new Mock<IJobRepository>();

jobRepositoryMock.Setup(x => x.GetReadyJob(It.IsAny<string>(), It.IsAny<TimeSpan>()))
.ReturnsAsync(() => null);

var settings = new HorariumSettings
{
IntervalStartJob = TimeSpan.FromSeconds(1),
JobThrottleSettings = new JobThrottleSettings
{
UseJobThrottle = true,
IntervalMultiplier = 1,
JobRetrievalAttempts = 1,
MaxJobThrottleInterval = TimeSpan.FromSeconds(1)
}
};

var runnerJobs = new RunnerJobs(jobRepositoryMock.Object,
settings,
new JsonSerializerSettings(),
Mock.Of<IHorariumLogger>(),
Mock.Of<IExecutorJob>());

// Act
runnerJobs.Start();
await Task.Delay(settings.IntervalStartJob - TimeSpan.FromMilliseconds(500));
jobRepositoryMock.Invocations.Clear();

await Task.Delay(TimeSpan.FromSeconds(5));

// Assert
jobRepositoryMock.Verify(r => r.GetReadyJob(It.IsAny<string>(), It.IsAny<TimeSpan>()), Times.Exactly(5));
}

}
}

0 comments on commit f47d280

Please sign in to comment.