コード例 #1
0
        public async Task Start_WontRecoverBeforeIntervalTimeout_AfterFailedDB()
        {
            // Arrange
            var jobRepositoryMock = new Mock <IJobRepository>();

            var settings = new HorariumSettings
            {
                IntervalStartJob = TimeSpan.FromSeconds(2),
            };

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

            jobRepositoryMock.SetupSequence(x => x.GetReadyJob(It.IsAny <string>(), It.IsAny <TimeSpan>()))
            .ThrowsAsync(new Exception())
            .ReturnsAsync(new JobDb());

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

            // Assert
            jobRepositoryMock.Verify(r => r.GetReadyJob(It.IsAny <string>(), It.IsAny <TimeSpan>()), Times.Once);
        }
コード例 #2
0
        public async Task StopAsync_AwaitsWhenAllCompleted()
        {
            // Arrange
            var jobRepositoryMock   = new Mock <IJobRepository>();
            var uncompletedTaskList = new Mock <IUncompletedTaskList>();
            var cancellationToken   = new CancellationTokenSource().Token;

            var settings = new HorariumSettings
            {
                IntervalStartJob = TimeSpan.FromSeconds(2),
            };

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

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

            // Act
            runnerJobs.Start();
            await Task.Delay(TimeSpan.FromSeconds(1));

            await runnerJobs.Stop(cancellationToken);

            // Assert
            uncompletedTaskList.Verify(x => x.WhenAllCompleted(cancellationToken), Times.Once);
        }
コード例 #3
0
        public async Task Start_Stop()
        {
            // Arrange
            var jobRepositoryMock = new Mock <IJobRepository>();

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

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

            // Act
            runnerJobs.Start();

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

            await runnerJobs.Stop(CancellationToken.None);

            jobRepositoryMock.Invocations.Clear();

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

            // Assert
            jobRepositoryMock.Verify(x => x.GetReadyJob(It.IsAny <string>(), It.IsAny <TimeSpan>()), Times.Never);
        }
コード例 #4
0
        public async Task Start_NextJobStarted_AddsJobTaskToUncompletedTasks()
        {
            // Arrange
            var jobRepositoryMock   = new Mock <IJobRepository>();
            var uncompletedTaskList = new Mock <IUncompletedTaskList>();

            uncompletedTaskList.Setup(x => x.Add(It.IsAny <Task>()));

            jobRepositoryMock.Setup(x => x.GetReadyJob(It.IsAny <string>(), It.IsAny <TimeSpan>()))
            .ReturnsAsync(new JobDb
            {
                JobType = typeof(object).ToString(),
            });

            var runnerJobs = new RunnerJobs(jobRepositoryMock.Object,
                                            new HorariumSettings
            {
                IntervalStartJob = TimeSpan.FromHours(1),     // prevent second job from starting
            },
                                            new JsonSerializerSettings(),
                                            Mock.Of <IHorariumLogger>(),
                                            Mock.Of <IExecutorJob>(),
                                            uncompletedTaskList.Object);

            // Act
            runnerJobs.Start();
            await Task.Delay(TimeSpan.FromSeconds(5));

            await runnerJobs.Stop(CancellationToken.None);

            // Assert
            uncompletedTaskList.Verify(x => x.Add(It.IsAny <Task>()), Times.Once);
        }
コード例 #5
0
        public async Task Start_ExecutionWithDelay_WithThrottle()
        {
            // Arrange
            var jobRepositoryMock = new Mock <IJobRepository>();

            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>(),
                                            Mock.Of <IUncompletedTaskList>());

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

            jobRepositoryMock.Invocations.Clear();

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

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