Beispiel #1
0
        public void ShouldCreateTest()
        {
            ThreadLocalContextualStorageStrategy threadLocalContextualStorageStrategy;
            const string KEY = "somekey";
            bool         result;
            string       value;
            string       expected;

            threadLocalContextualStorageStrategy = new ThreadLocalContextualStorageStrategy();

            // has unset
            result = threadLocalContextualStorageStrategy.HasValue(KEY);
            Assert.IsFalse(result);

            // get unset
            value = threadLocalContextualStorageStrategy.GetValue <string>(KEY);
            Assert.IsNull(value);

            // remove unset
            threadLocalContextualStorageStrategy.RemoveValue(KEY);

            // set unset
            expected = Guid.NewGuid().ToString("N");
            threadLocalContextualStorageStrategy.SetValue(KEY, expected);

            // has isset
            result = threadLocalContextualStorageStrategy.HasValue(KEY);
            Assert.IsTrue(result);

            // get isset
            value = threadLocalContextualStorageStrategy.GetValue <string>(KEY);
            Assert.IsNotNull(value);
            Assert.AreEqual(expected, value);

            // set isset
            expected = Guid.NewGuid().ToString("N");
            threadLocalContextualStorageStrategy.SetValue(KEY, expected);

            result = threadLocalContextualStorageStrategy.HasValue(KEY);
            Assert.IsTrue(result);

            value = threadLocalContextualStorageStrategy.GetValue <string>(KEY);
            Assert.IsNotNull(value);
            Assert.AreEqual(expected, value);

            // remove isset
            threadLocalContextualStorageStrategy.RemoveValue(KEY);

            // verify remove
            result = threadLocalContextualStorageStrategy.HasValue(KEY);
            Assert.IsFalse(result);

            value = threadLocalContextualStorageStrategy.GetValue <string>(KEY);
            Assert.IsNull(value);
        }
		public void ShouldCreateTest()
		{
			ThreadLocalContextualStorageStrategy threadLocalContextualStorageStrategy;
			const string KEY = "somekey";
			bool result;
			string value;
			string expected;

			threadLocalContextualStorageStrategy = new ThreadLocalContextualStorageStrategy();

			// has unset
			result = threadLocalContextualStorageStrategy.HasValue(KEY);
			Assert.IsFalse(result);

			// get unset
			value = threadLocalContextualStorageStrategy.GetValue<string>(KEY);
			Assert.IsNull(value);

			// remove unset
			threadLocalContextualStorageStrategy.RemoveValue(KEY);

			// set unset
			expected = Guid.NewGuid().ToString("N");
			threadLocalContextualStorageStrategy.SetValue(KEY, expected);

			// has isset
			result = threadLocalContextualStorageStrategy.HasValue(KEY);
			Assert.IsTrue(result);

			// get isset
			value = threadLocalContextualStorageStrategy.GetValue<string>(KEY);
			Assert.IsNotNull(value);
			Assert.AreEqual(expected, value);

			// set isset
			expected = Guid.NewGuid().ToString("N");
			threadLocalContextualStorageStrategy.SetValue(KEY, expected);

			result = threadLocalContextualStorageStrategy.HasValue(KEY);
			Assert.IsTrue(result);

			value = threadLocalContextualStorageStrategy.GetValue<string>(KEY);
			Assert.IsNotNull(value);
			Assert.AreEqual(expected, value);

			// remove isset
			threadLocalContextualStorageStrategy.RemoveValue(KEY);

			// verify remove
			result = threadLocalContextualStorageStrategy.HasValue(KEY);
			Assert.IsFalse(result);

			value = threadLocalContextualStorageStrategy.GetValue<string>(KEY);
			Assert.IsNull(value);
		}
		public void ShouldCreateCrossThreadTest()
		{
			ThreadLocalContextualStorageStrategy threadLocalContextualStorageStrategy;
			const string KEY = "somekey";
			bool result;
			string value;
			string expected;

			Thread thread;
			int managedThreadId;

			managedThreadId = Thread.CurrentThread.ManagedThreadId;
			threadLocalContextualStorageStrategy = new ThreadLocalContextualStorageStrategy();

			// set unset
			expected = Guid.NewGuid().ToString("N");
			threadLocalContextualStorageStrategy.SetValue(KEY, expected);

			// has isset
			result = threadLocalContextualStorageStrategy.HasValue(KEY);
			Assert.IsTrue(result);

			// get isset
			value = threadLocalContextualStorageStrategy.GetValue<string>(KEY);
			Assert.IsNotNull(value);
			Assert.AreEqual(expected, value);

			thread = new Thread(() =>
								{
									int _managedThreadId = Thread.CurrentThread.ManagedThreadId;

									Assert.AreNotEqual(_managedThreadId, managedThreadId);

									// has isset(other thread)
									result = threadLocalContextualStorageStrategy.HasValue(KEY);
									Assert.IsFalse(result);
								});

			thread.Start();
			thread.Join();
		}
Beispiel #4
0
        public void ShouldCreateCrossThreadTest()
        {
            ThreadLocalContextualStorageStrategy threadLocalContextualStorageStrategy;
            const string KEY = "somekey";
            bool         result;
            string       value;
            string       expected;

            Thread thread;
            int    managedThreadId;

            managedThreadId = Thread.CurrentThread.ManagedThreadId;
            threadLocalContextualStorageStrategy = new ThreadLocalContextualStorageStrategy();

            // set unset
            expected = Guid.NewGuid().ToString("N");
            threadLocalContextualStorageStrategy.SetValue(KEY, expected);

            // has isset
            result = threadLocalContextualStorageStrategy.HasValue(KEY);
            Assert.IsTrue(result);

            // get isset
            value = threadLocalContextualStorageStrategy.GetValue <string>(KEY);
            Assert.IsNotNull(value);
            Assert.AreEqual(expected, value);

            thread = new Thread(() =>
            {
                int _managedThreadId = Thread.CurrentThread.ManagedThreadId;

                Assert.AreNotEqual(_managedThreadId, managedThreadId);

                // has isset(other thread)
                result = threadLocalContextualStorageStrategy.HasValue(KEY);
                Assert.IsFalse(result);
            });

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