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 Task When_starting_correlationContext_with_legacy_ctor_when_another_context_is_active_should_not_throw()
            {
                const string parentContextId = nameof(parentContextId);

#pragma warning disable 618 // justification, covering legacy implementation (pre v3.0)
                var sut = new CorrelationManager(
                    new CorrelationContextFactory(_correlationContextAccessor),
                    _correlationIdFactoryMock.Object,
                    new NullLogger <CorrelationManager>()
                    );
#pragma warning restore 618

                return(sut.CorrelateAsync(parentContextId,
                                          async() =>
                {
                    CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
                    parentContext.Should().NotBeNull();
                    parentContext.CorrelationId.Should().Be(parentContextId);

                    await sut.CorrelateAsync(() =>
                    {
                        CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
                        innerContext.Should().NotBeNull().And.NotBe(parentContext);
                        innerContext.CorrelationId.Should().NotBe(parentContextId);

                        return Task.CompletedTask;
                    });
                }));
            }
        public async Task Given_a_task_should_run_task_inside_correlated_context()
        {
            // Pre-assert
            _correlationContextAccessor.CorrelationContext.Should().BeNull();

            // Act
            await _sut.CorrelateAsync(() =>
            {
                // Inline assert
                _correlationContextAccessor.CorrelationContext.Should().NotBeNull();
                return(Task.CompletedTask);
            });

            // Post-assert
            _correlationContextAccessor.CorrelationContext.Should().BeNull();
        }