public void When_correlation_is_set_with_a_predefined_correlation_id_Then_it_should_always_return_it()
        {
            var correlationId1 = Guid.NewGuid();
            var correlationId2 = Guid.NewGuid();

            var thread1 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation(correlationId1);

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            var thread2 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation(correlationId2);

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            Task.WaitAll(thread1, thread2);

            Assert.Equal(correlationId1, thread1.Result);
            Assert.Equal(correlationId2, thread2.Result);
        }
        public void When_correlation_is_access_different_threads_It_returns_different_ids()
        {
            var thread1 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            var thread2 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            Task.WaitAll(thread1, thread2);

            Assert.NotEqual(thread1.Result, thread2.Result);
        }
        public void When_correlation_is_uninitialized_Then_it_returns_null_when_asked_for_current_correlation_id()
        {
            var thread = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.ClearCorrelation();
                return(CorrelationState.GetCurrentCorrelationId());
            });

            Task.WaitAll(thread);

            Assert.Null(thread.Result);
        }
        public void When_correlation_is_initialized_and_then_cleared_then_it_forgets_the_correlation_keyvalues()
        {
            var thread = Task <IEnumerable <KeyValuePair <string, object> > > .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                CorrelationState.TryAddOrUpdateCorrelationValue(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

                CorrelationState.ClearCorrelation();

                return(CorrelationState.GetCorrelationValues());
            });

            Task.WaitAll(thread);

            Assert.False(thread.Result.Any());
        }
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     CorrelationState.ClearCorrelation();
     base.OnActionExecuted(context);
 }
 public void Dispose()
 {
     CorrelationState.ClearCorrelation();
 }
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            CorrelationState.ClearCorrelation();

            return(base.OnActionExecutedAsync(actionExecutedContext, cancellationToken));
        }