public async Task GivenTwoJobs_WhenPublishing_ThenShouldPublishBoth() { // Given var firstJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var secondJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var firstJobExecution = JobExecution.New(firstJob, firstJob.RunAt); var secondJobExecution = JobExecution.New(secondJob, secondJob.RunAt); _options.JobsExchange = "jobs"; Mock.Get(_model) .Setup(x => x.WaitForConfirms()) .Returns(true); var batchPublish = Mock.Of <IBasicPublishBatch>(); Mock.Get(_model) .Setup(x => x.CreateBasicPublishBatch()) .Returns(batchPublish); Mock.Get(_model) .Setup(x => x.BasicPublish(_options.JobsExchange, firstJob.Subject, true, null, It.IsAny <ReadOnlyMemory <byte> >())); Mock.Get(_model) .Setup(x => x.BasicPublish(_options.JobsExchange, secondJob.Subject, true, null, It.IsAny <ReadOnlyMemory <byte> >())); // When var result = await _publisher.PublishManyAsync(new [] { firstJobExecution, secondJobExecution }, CancellationToken.None); // Then result.ShouldBeTrue(); Mock.Get(batchPublish) .Verify(x => x.Publish(), Times.Once); }
public async Task GivenValidRequest_WhenCreating_ShouldReturnCreated() { // Given var request = new CreateJobRequest { Subject = "test", Payload = "do things", RunAt = DateTime.UtcNow.AddDays(1), }; var job = Job.New(request.Subject, request.Payload, true, request.RunAt, request.StopAfter, null); var jobExecution = JobExecution.New(job, request.RunAt); Mock.Get(_mediator) .Setup(x => x.Send(It.IsAny <Core.Handlers.CreateJobRequest>(), CancellationToken.None)) .ReturnsAsync(new CreateJobResponse(job, jobExecution)); // When var result = await _controller.CreateAsync(request, CancellationToken.None); var actionResult = result.Result as ObjectResult; // Then actionResult.ShouldBeAssignableTo <ObjectResult>(); actionResult.StatusCode.ShouldBe(StatusCodes.Status201Created); }
public async Task GivenTwoPendingExecutions_WhenFailingToPublish_ShouldReScheduled() { // Given var request = new SchedulePendingRequest(); var executions = new[] { JobExecution.New(Job.New("test", "test payload", true, DateTime.UtcNow, DateTime.UtcNow, "* * * * *"), DateTime.UtcNow), JobExecution.New(Job.New("test", "test payload", true, DateTime.UtcNow, DateTime.UtcNow, "* * * * *"), DateTime.UtcNow), }; Mock.Get(_jobExecutionRepository) .SetupSequence(x => x.GetAndMarkPending(It.IsAny <int>(), It.IsAny <DateTime>(), CancellationToken.None)) .ReturnsAsync(executions) .ReturnsAsync(Enumerable.Empty <JobExecution>()); Mock.Get(_jobPublisher) .Setup(x => x.PublishManyAsync(It.IsAny <IEnumerable <JobExecution> >(), CancellationToken.None)) .ReturnsAsync(false); // When await _handler.Handle(request, CancellationToken.None); // Then Mock.Get(_jobExecutionRepo) .Verify(x => x.UpdateManyAsync(It.IsAny <IEnumerable <JobExecution> >(), CancellationToken.None), Times.Once); }
public async Task GivenSingleJob_WhenPublishing_ThenShouldPublish() { // Given var job = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var jobExecution = JobExecution.New(job, job.RunAt); // When var result = await _publisher.PublishAsync(jobExecution, CancellationToken.None); // Then result.ShouldBeTrue(); Mock.Get(_serviceBusSender) .Verify(x => x.SendMessageAsync(It.IsAny <ServiceBusMessage>(), CancellationToken.None) , Times.Once); }
public async Task <CreateJobResponse> Handle(CreateJobRequest request, CancellationToken cancellationToken) { await _jobRepository.AddAsync(request.Job, cancellationToken); JobExecution?jobExecution = null; if (request.Job.Enabled && request.Job.TryGetNextRun(DateTime.UtcNow, out var runAt) && runAt.HasValue) { jobExecution = JobExecution.New(request.Job, runAt.Value); await _jobExecutionRepository.AddAsync(jobExecution, cancellationToken); } return(new CreateJobResponse(request.Job, jobExecution)); }
private async Task ScheduleNextExecutionsAsync(IEnumerable <JobExecution> pendingJobs, CancellationToken ctx) { var nextJobExecutions = new List <JobExecution>(); foreach (var pending in pendingJobs) { // If there is a next schedule available for the job queue it if (pending.Job.TryGetNextRun(DateTime.UtcNow, out var nextRun) && nextRun.HasValue) { var nextJobExecution = JobExecution.New(pending.Job, nextRun.Value); nextJobExecutions.Add(nextJobExecution); } } await _jobExecutionRepo.AddManyAsync(nextJobExecutions, ctx); }
public async Task GivenTwoJobs_WhenPublishing_ThenShouldPublishBoth() { // Given var firstJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var secondJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var firstJobExecution = JobExecution.New(firstJob, firstJob.RunAt); var secondJobExecution = JobExecution.New(secondJob, secondJob.RunAt); // When var result = await _publisher.PublishManyAsync(new [] { firstJobExecution, secondJobExecution }, CancellationToken.None); // Then result.ShouldBeTrue(); Mock.Get(_serviceBusSender) .Verify(x => x.SendMessagesAsync(It.IsAny <IEnumerable <ServiceBusMessage> >(), CancellationToken.None) , Times.Once); }
public async Task GivenRabbitIsUnavailable_WhenPublishingMany_ThenShouldReturnFalse() { // Given var job = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var jobExecution = JobExecution.New(job, job.RunAt); _options.JobsExchange = "jobs"; Mock.Get(_connectionFactory) .Setup(x => x.CreateConnection()) .Throws(new Exception()); // When var result = await _publisher.PublishManyAsync(new [] { jobExecution }, CancellationToken.None); // Then result.ShouldBeFalse(); }
public async Task GivenOneExecution_WhenHandling_ShouldReturn() { // Given var request = new GetExecutionsRequest(Enumerable.Empty <ExecutionStatus>(), "subject", 10, 0); Mock.Get(_jobExecutionRepository) .Setup(x => x.GetManyAsync(It.IsAny <Func <IQueryable <JobExecution>, IQueryable <JobExecution> > >(), CancellationToken.None)) .ReturnsAsync(new List <JobExecution> { JobExecution.New(Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow, null), DateTime.UtcNow) }.AsReadOnly()); // When var result = await _handler.Handle(request, CancellationToken.None); // Then result.Executions.ShouldHaveSingleItem(); }
public async Task GivenExistingId_WhenGettingById_ShouldReturn() { // Given var jobExecution = JobExecution.New(Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow, null), DateTime.UtcNow); Mock.Get(_mediator) .Setup(x => x.Send(It.IsAny <GetJobExecutionsRequest>(), CancellationToken.None)) .ReturnsAsync(new GetJobExecutionsResponse(new [] { jobExecution })); // When var result = await _controller.GetByJobIdAsync(Guid.NewGuid(), CancellationToken.None); var actionResult = result.Result as OkObjectResult; // Then actionResult.ShouldBeAssignableTo <OkObjectResult>(); actionResult.StatusCode.ShouldBe(StatusCodes.Status200OK); }
public async Task GivenTwoExecution_WhenHandling_ShouldReturn() { // Given var request = new GetJobExecutionsRequest(Guid.NewGuid()); Mock.Get(_repository) .Setup(x => x.GetManyAsync(It.IsAny <Func <IQueryable <JobExecution>, IQueryable <JobExecution> > >(), CancellationToken.None)) .ReturnsAsync(new List <JobExecution> { JobExecution.New(Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow, null), DateTime.UtcNow), JobExecution.New(Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow, null), DateTime.UtcNow) }.AsReadOnly()); // When var result = await _handler.Handle(request, CancellationToken.None); // Then result.Executions.Count().ShouldBe(2); }
public async Task GivenRabbitIsUnavailable_WhenPublishing_ThenShouldReturnFalse() { // Given var job = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var jobExecution = JobExecution.New(job, job.RunAt); _options.JobsExchange = "jobs"; Mock.Get(_connectionFactory) .Setup(x => x.CreateConnection()) .Throws(new Exception()); // When var result = await _publisher.PublishAsync(jobExecution, CancellationToken.None); // Then result.ShouldBeFalse(); Mock.Get(_model) .Verify(x => x.BasicPublish(_options.JobsExchange, job.Subject, true, null, It.IsAny <ReadOnlyMemory <byte> >()), Times.Never); }
public async Task GivenSingleJob_WhenUnsuccessfulPublishing_ThenShouldReturnFalse() { // Given var job = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var jobExecution = JobExecution.New(job, job.RunAt); _options.JobsExchange = "jobs"; Mock.Get(_model) .Setup(x => x.WaitForConfirms()) .Returns(true); Mock.Get(_model) .Setup(x => x.BasicPublish(_options.JobsExchange, job.Subject, true, null, It.IsAny <ReadOnlyMemory <byte> >())) .Throws <Exception>(); // When var result = await _publisher.PublishAsync(jobExecution, CancellationToken.None); // Then result.ShouldBeFalse(); }
public async Task GivenTwoPendingExecutions_WhenHandling_ShouldReturnTwoScheduled() { // Given var request = new SchedulePendingRequest(); var executions = new[] { JobExecution.New(Job.New("test", "test payload", true, DateTime.UtcNow, DateTime.UtcNow, "* * * * *"), DateTime.UtcNow), JobExecution.New(Job.New("test", "test payload", true, DateTime.UtcNow, DateTime.UtcNow, "* * * * *"), DateTime.UtcNow), }; Mock.Get(_jobExecutionRepository) .SetupSequence(x => x.GetAndMarkPending(It.IsAny <int>(), It.IsAny <DateTime>(), CancellationToken.None)) .ReturnsAsync(executions) .ReturnsAsync(Enumerable.Empty <JobExecution>()); // When var result = await _handler.Handle(request, CancellationToken.None); // Then result.NumScheduled.ShouldBe(2); }
public async Task GivenTwoJobs_WhenUnsuccessfulPublishing_ThenShouldReturnFalse() { // Given var firstJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var secondJob = Job.New("subject", "payload", true, DateTime.UtcNow, DateTime.UtcNow.AddHours(1), null); var firstJobExecution = JobExecution.New(firstJob, firstJob.RunAt); var secondJobExecution = JobExecution.New(secondJob, secondJob.RunAt); _options.JobsExchange = "jobs"; Mock.Get(_model) .Setup(x => x.WaitForConfirms()) .Returns(true); Mock.Get(_model) .Setup(x => x.CreateBasicPublishBatch()) .Throws <Exception>(); // When var result = await _publisher.PublishManyAsync(new [] { firstJobExecution, secondJobExecution }, CancellationToken.None); // Then result.ShouldBeFalse(); }