public async Task WaitForJobsToCompleteWithJobsFailed_ReturnsFalse(bool useServiceBus)
        {
            IJobsApiClient jobsApiClient             = Substitute.For <IJobsApiClient>();
            JobManagementResiliencePolicies policies = new JobManagementResiliencePolicies
            {
                JobsApiClient = Policy.NoOpAsync()
            };

            IMessengerService messengerService = null;

            if (useServiceBus)
            {
                messengerService = Substitute.For <IMessengerService, IServiceBusService>();
            }
            else
            {
                messengerService = Substitute.For <IMessengerService, IQueueService>();
            }

            ILogger logger = Substitute.For <ILogger>();

            JobManagement jobManagement = new JobManagement(jobsApiClient, logger, policies, messengerService);

            string jobId = "3456";

            jobsApiClient
            .GetLatestJobsForSpecification("specificationId", Arg.Is <string[]>(_ => _.Single() == "PopulateScopedProviders"))
            .Returns(new ApiResponse <IDictionary <string, JobSummary> >(HttpStatusCode.OK, new Dictionary <string, JobSummary> {
                { string.Empty, new JobSummary {
                      RunningStatus = RunningStatus.Completed, CompletionStatus = CompletionStatus.Failed, JobId = jobId
                  } }
            }));

            messengerService
            .ReceiveMessage("topic/Subscriptions/correlationId", Arg.Any <Predicate <JobSummary> >(), TimeSpan.FromMilliseconds(600000))
            .Returns(new JobSummary
            {
                CompletionStatus = CompletionStatus.Failed
            });

            //Act
            bool jobsComplete = await jobManagement.QueueJobAndWait(async() => await Task.Run(() => { return(true); }), "PopulateScopedProviders", "specificationId", "correlationId", "topic");

            //Assert
            if (useServiceBus)
            {
                await((IServiceBusService)messengerService)
                .Received(1)
                .CreateSubscription("topic", "correlationId", Arg.Is <TimeSpan>(_ => _.Days == 1));

                await messengerService
                .Received(1)
                .ReceiveMessage("topic/Subscriptions/correlationId", Arg.Any <Predicate <JobSummary> >(), TimeSpan.FromMilliseconds(600000));
            }

            jobsComplete
            .Should()
            .BeFalse();
        }
Esempio n. 2
0
        private async Task <bool> CheckAllJobs(string jobType, string specificationId, Predicate <JobSummary> predicate)
        {
            ApiResponse <IDictionary <string, JobSummary> > jobResponse = await _jobsApiClientPolicy.ExecuteAsync(() =>
            {
                return(_jobsApiClient.GetLatestJobsForSpecification(specificationId, new string[] { jobType }));
            });

            if ((int?)jobResponse?.StatusCode >= 200 && (int?)jobResponse?.StatusCode <= 299)
            {
                JobSummary summary = jobResponse.Content?.Values.FirstOrDefault();

                return(predicate(summary));
            }
            else
            {
                // any failures retrieving jobsummaries we ignore and keep polling as we don't know what state the jobs are in
                return(true);
            }
        }
        public async Task GetLatestJobForSpecification_Called_ReturnsJobSummary()
        {
            string specificationId = "1234";
            string jobType         = "3456";
            string jobId           = "5678";

            IJobsApiClient jobsApiClient             = Substitute.For <IJobsApiClient>();
            JobManagementResiliencePolicies policies = new JobManagementResiliencePolicies
            {
                JobsApiClient = Policy.NoOpAsync()
            };
            IMessengerService messengerService = Substitute.For <IMessengerService>();
            ILogger           logger           = Substitute.For <ILogger>();

            string[] jobTypes = new string[]
            {
                jobType
            };

            IDictionary <string, JobSummary> jobSummary = new Dictionary <string, JobSummary> {
                { string.Empty, new JobSummary {
                      JobId = jobId
                  } }
            };
            ApiResponse <IDictionary <string, JobSummary> > jobSummaryApiResponse = new ApiResponse <IDictionary <string, JobSummary> >(HttpStatusCode.OK, jobSummary);

            jobsApiClient
            .GetLatestJobsForSpecification(specificationId, jobTypes)
            .Returns(jobSummaryApiResponse);

            JobManagement jobManagement = new JobManagement(jobsApiClient, logger, policies, messengerService);

            //Act
            IDictionary <string, JobSummary> result = await jobManagement.GetLatestJobsForSpecification(specificationId, jobTypes);

            Assert.AreEqual(result, jobSummary);

            await jobsApiClient
            .Received(1)
            .GetLatestJobsForSpecification(specificationId, jobTypes);
        }
        public async Task <IActionResult> GetSpecificationJobs([FromRoute] string specificationId, [FromRoute] string jobTypes)
        {
            string[] jobTypesArray = jobTypes.Split(',', StringSplitOptions.RemoveEmptyEntries);

            ApiResponse <IEnumerable <JobSummary> > response = await _jobsApiClient.GetLatestJobsForSpecification(specificationId, jobTypesArray);

            IActionResult errorResult = response.IsSuccessOrReturnFailureResult("JobSummary", treatNoContentAsSuccess: true);

            if (errorResult != null)
            {
                return(errorResult);
            }

            if (response.StatusCode == HttpStatusCode.NoContent)
            {
                return(Ok(jobTypesArray.Select(x => (string)null)));
            }

            IEnumerable <JobSummaryViewModel> jobs = _mapper.Map <IEnumerable <JobSummaryViewModel> >(response.Content);

            return(Ok(jobs));
        }
        public void GetLatestJobForSpecification_UnsuccessfulApiResponse_ThrowsJobsNotRetrievedException()
        {
            string specificationId = "1234";
            string jobType         = "3456";

            IJobsApiClient jobsApiClient             = Substitute.For <IJobsApiClient>();
            JobManagementResiliencePolicies policies = new JobManagementResiliencePolicies
            {
                JobsApiClient = Policy.NoOpAsync()
            };
            IMessengerService messengerService = Substitute.For <IMessengerService>();
            ILogger           logger           = Substitute.For <ILogger>();

            string[] jobTypes = new string[]
            {
                jobType
            };

            string message = $"Error while retrieving latest jobs for Specifiation: {specificationId} and JobTypes: {string.Join(',', jobTypes)}";

            ApiResponse <IDictionary <string, JobSummary> > jobSummaryApiResponse = new ApiResponse <IDictionary <string, JobSummary> >(HttpStatusCode.BadRequest);

            jobsApiClient
            .GetLatestJobsForSpecification(specificationId, jobTypes)
            .Returns(jobSummaryApiResponse);

            JobManagement jobManagement = new JobManagement(jobsApiClient, logger, policies, messengerService);

            //Act
            Func <Task> invocation = async() => await jobManagement.GetLatestJobsForSpecification(specificationId, jobTypes);

            //Arrange
            invocation.Should()
            .Throw <JobsNotRetrievedException>()
            .WithMessage(message);
        }
        public async Task WaitForJobsToCompleteWithNoJobsRunning_ReturnsTrue()
        {
            IJobsApiClient jobsApiClient             = Substitute.For <IJobsApiClient>();
            JobManagementResiliencePolicies policies = new JobManagementResiliencePolicies
            {
                JobsApiClient = Policy.NoOpAsync()
            };
            IMessengerService messengerService = Substitute.For <IMessengerService>();
            ILogger           logger           = Substitute.For <ILogger>();

            JobManagement jobManagement = new JobManagement(jobsApiClient, logger, policies, messengerService);

            jobsApiClient
            .GetLatestJobsForSpecification("specificationId", Arg.Is <string[]>(_ => _.Single() == "PopulateScopedProviders"))
            .Returns(new ApiResponse <IDictionary <string, JobSummary> >(HttpStatusCode.NoContent));

            //Act
            bool jobsComplete = await jobManagement.QueueJobAndWait(async() => await Task.Run(() => { return(true); }), "PopulateScopedProviders", "specificationId", "correlationId", "topic");

            //Assert
            jobsComplete
            .Should()
            .BeTrue();
        }