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();
        }
        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();
        }