예제 #1
0
        public void data_are_correctly_retrieved_from_http_context_from_multiple_storage_instances()
        {
            var fakeHttpContext = FakeHttpContextHelper.GetFakeHttpContext();

            HttpContext.Current = fakeHttpContext;

            var ambientStorageOne = new AmbientStorage <int> {
                Value = 23
            };
            var ambientStorageTwo = new AmbientStorage <int> {
                Value = 24
            };

            var threadAmbientStorageOneValue = 0;
            var threadAmbientStorageTwoValue = 0;
            var thread = new Thread(() =>
            {
                HttpContext.Current = fakeHttpContext;

                threadAmbientStorageOneValue = ambientStorageOne.Value;
                threadAmbientStorageTwoValue = ambientStorageTwo.Value;
            });

            thread.Start();
            thread.Join();

            threadAmbientStorageOneValue.ShouldBe(23);
            threadAmbientStorageTwoValue.ShouldBe(24);
        }
        public void data_are_correctly_retrieved()
        {
            var ambientStorage = new AmbientStorage <int> {
                Value = 23
            };

            ambientStorage.Value.ShouldBe(23);
        }
        public async Task data_flows_with_the_execution_context_into_async_task()
        {
            var ambientStorage = new AmbientStorage <int> {
                Value = 23
            };

            await Task.Run(() =>
            {
                ambientStorage.Value.ShouldBe(23);
            });
        }
        public void data_are_correctly_set_and_retrieved_by_multiple_threads()
        {
            var ambientStorage    = new AmbientStorage <int>();
            var threadOneValueSet = false;
            var threadTwoValueSet = false;
            var threadOneValue    = 0;
            var threadTwoValue    = 0;

            var threadOne = new Thread(() =>
            {
                ambientStorage.Value = 23;
                threadOneValueSet    = true;
                while (!threadTwoValueSet)
                {
                    Thread.Sleep(0);
                }

                threadOneValue = ambientStorage.Value;
            });
            var threadTwo = new Thread(() =>
            {
                ambientStorage.Value = 24;
                threadTwoValueSet    = true;
                while (!threadOneValueSet)
                {
                    Thread.Sleep(0);
                }

                threadTwoValue = ambientStorage.Value;
            });

            threadOne.Start();
            threadTwo.Start();

            threadOne.Join();
            threadTwo.Join();

            threadOneValue.ShouldBe(23);
            threadTwoValue.ShouldBe(24);
        }
        public void data_are_correctly_retrieved_from_http_context()
        {
            var fakeHttpContext = FakeHttpContextHelper.GetFakeHttpContext();

            HttpContext.Current = fakeHttpContext;

            var ambientStorage = new AmbientStorage <int> {
                Value = 23
            };

            var threadValue = 0;
            var thread      = new Thread(() =>
            {
                HttpContext.Current = fakeHttpContext;

                threadValue = ambientStorage.Value;
            });

            thread.Start();
            thread.Join();

            threadValue.ShouldBe(23);
        }
        public void data_are_correctly_set_and_retrieved_by_multiple_threads()
        {
            var ambientStorage = new AmbientStorage <int>();

            var threadOneValueSet = false;
            var threadTwoValueSet = false;

            Exception threadOneException = null;
            Exception threadTwoException = null;

            var threadOneValue = 0;
            var threadTwoValue = 0;

            var threadOne = new Thread(() =>
            {
                try
                {
                    ambientStorage.Value = 23;
                    threadOneValueSet    = true;
                    while (!threadTwoValueSet)
                    {
                        Thread.Sleep(0);
                    }

                    threadOneValue = ambientStorage.Value;
                }
                catch (Exception e)
                {
                    threadOneException = e;
                }
            });
            var threadTwo = new Thread(() =>
            {
                try
                {
                    ambientStorage.Value = 24;
                    threadTwoValueSet    = true;
                    while (!threadOneValueSet)
                    {
                        Thread.Sleep(0);
                    }

                    threadTwoValue = ambientStorage.Value;
                }
                catch (Exception e)
                {
                    threadTwoException = e;
                }
            });

            threadOne.Start();
            threadTwo.Start();

            threadOne.Join();
            threadTwo.Join();

            threadOneException.ShouldBeNull();
            threadTwoException.ShouldBeNull();

            threadOneValueSet.ShouldBeTrue();
            threadTwoValueSet.ShouldBeTrue();

            threadOneValue.ShouldBe(23);
            threadTwoValue.ShouldBe(24);
        }