public async Task Given_job_is_queued_inside_correlationContext_should_use_correlationId_of_correlation_context()
        {
            const string correlationId = "my-id";
            var          expectedJob   = new BackgroundTestExecutor
            {
                CorrelationId   = correlationId,
                JobHasCompleted = true
            };

            // Act
            await _correlationManager.CorrelateAsync(correlationId,
                                                     async() =>
            {
                await Task.Yield();
                expectedJob.JobId = _client.Enqueue <BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null));
            });

            await WaitUntilJobCompletedAsync(expectedJob.JobId);

            // Assert
            ExecutedJobs.Should().BeEquivalentTo(
                new List <object> {
                expectedJob
            },
                "a correlation context exists, so the correlation id should be used when performing the job"
                );
        }
        public async Task Given_parent_job_is_queued_outside_correlation_context_when_queueing_continuation_also_outside_context_should_use_job_id_from_parent()
        {
            var expectedParentJob = new BackgroundTestExecutor
            {
                JobHasCompleted = true
            };
            var expectedContinuationJob = new BackgroundTestExecutor
            {
                JobHasCompleted = true
            };

            // Queue parent
            expectedParentJob.JobId = expectedParentJob.CorrelationId = expectedContinuationJob.CorrelationId = _client.Enqueue <BackgroundTestExecutor>(
                job => job.RunAsync(TimeoutInMillis, null)
                );

            // Act
            expectedContinuationJob.JobId = _client.ContinueJobWith <BackgroundTestExecutor>(
                expectedParentJob.JobId,
                job => job.RunAsync(TimeoutInMillis, null)
                );

            await WaitUntilJobCompletedAsync(expectedContinuationJob.JobId);

            // Assert
            ExecutedJobs.Should().BeEquivalentTo(
                new List <object>
            {
                expectedParentJob,
                expectedContinuationJob
            },
                "the parent id has no correlation id but the continuation job should have the parent job id for correlation id"
                );
        }
        public async Task Given_parent_job_is_queued_when_queueing_continuation_inside_of_correlation_context_should_use_correlation_id_from_context()
        {
            var expectedParentJob = new BackgroundTestExecutor
            {
                CorrelationId   = "parent-id",
                JobHasCompleted = true
            };
            var expectedContinuationJob = new BackgroundTestExecutor
            {
                CorrelationId   = "continuation-id",
                JobHasCompleted = true
            };

            // Queue parent
            _correlationManager.Correlate(expectedParentJob.CorrelationId, () =>
            {
                expectedParentJob.JobId = _client.Enqueue <BackgroundTestExecutor>(
                    job => job.RunAsync(TimeoutInMillis, null)
                    );
            });

            // Act
            _correlationManager.Correlate(expectedContinuationJob.CorrelationId, () =>
            {
                expectedContinuationJob.JobId = _client.ContinueJobWith <BackgroundTestExecutor>(
                    expectedParentJob.JobId,
                    job => job.RunAsync(TimeoutInMillis, null)
                    );
            });

            await WaitUntilJobCompletedAsync(expectedContinuationJob.JobId);

            // Assert
            ExecutedJobs.Should().BeEquivalentTo(
                new List <object>
            {
                expectedParentJob,
                expectedContinuationJob
            },
                "the continuation job should have the same correlation id"
                );
        }
        public async Task Given_parent_job_is_queued_when_queueing_continuation_outside_of_correlation_context_should_use_same_correlation_id_as_parent()
        {
            const string correlationId     = "my-id";
            var          expectedParentJob = new BackgroundTestExecutor
            {
                CorrelationId   = correlationId,
                JobHasCompleted = true
            };
            var expectedContinuationJob = new BackgroundTestExecutor
            {
                CorrelationId   = correlationId,
                JobHasCompleted = true
            };

            // Queue parent
            _correlationManager.Correlate(correlationId, () =>
            {
                expectedParentJob.JobId = _client.Enqueue <BackgroundTestExecutor>(
                    job => job.RunAsync(TimeoutInMillis, null)
                    );
            });

            // Act
            // We queue on purpose outside of context, so we are able to test if the job inherits the correlation id from parent job.
            expectedContinuationJob.JobId = _client.ContinueJobWith <BackgroundTestExecutor>(
                expectedParentJob.JobId,
                job => job.RunAsync(TimeoutInMillis, null)
                );

            await WaitUntilJobCompletedAsync(expectedContinuationJob.JobId);

            // Assert
            ExecutedJobs.Should().BeEquivalentTo(
                new List <object>
            {
                expectedParentJob,
                expectedContinuationJob
            },
                "the continuation job should have the same correlation id"
                );
        }
        public async Task Given_job_is_queued_outside_correlationContext_should_use_jobId_as_correlationId()
        {
            string jobId       = _client.Enqueue <BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null));
            var    expectedJob = new BackgroundTestExecutor
            {
                JobId           = jobId,
                CorrelationId   = jobId,
                JobHasCompleted = true
            };

            // Act
            await WaitUntilJobCompletedAsync(jobId);

            // Assert
            ExecutedJobs.Should().BeEquivalentTo(
                new List <object> {
                expectedJob
            },
                "no correlation context exists, so the job id should be used when performing the job"
                );
        }