Exemplo n.º 1
0
        public void TestThreadContext()
        {
            ThreadContext.Set("1", 1);

            var set = ThreadContext.PropertySet;

            set["2"] = 2;

            object value = ThreadContext.Get("1");

            Assert.AreEqual(value, 1, "value should be 1");

            value = set["2"];
            Assert.AreEqual(value, 2, "value should be 2");

            bool result = ThreadContext.Contains("1");

            Assert.IsTrue(result, "resutl should be true");

            result = ThreadContext.Contains("2");
            Assert.IsTrue(result, "resutl should be true");

            result = ThreadContext.Contains("3");
            Assert.IsFalse(result, "resutl should be false");

            value = ThreadContext.Get("3");
            Assert.IsNull(value, "value should be null");

            ThreadContext.Remove("1");

            result = ThreadContext.Contains("1");
            Assert.IsFalse(result, "resutl should be false");

            ThreadContext.Clear();

            result = ThreadContext.Contains("2");
            Assert.IsFalse(result, "resutl should be false");

            ThreadContext.Set("1", 1);
            ThreadContext.Set("2", 2);
            List <System.Threading.Thread> threadList = new List <System.Threading.Thread>();

            for (int i = 0; i < ThreadCount; i++)
            {
                var thread = new System.Threading.Thread(ThreadFunc);
                Assert.IsNotNull(thread, "value should not be null");
                threadList.Add(thread);
                thread.Start(i);
            }

            for (int i = 0; i < threadList.Count; i++)
            {
                threadList[i].Join();
            }

            for (int i = 0; i < ThreadCount; i++)
            {
                value = ThreadContext.Get((i + ThreadCount).ToString());
                Assert.IsNull(value, "value should be null");;
            }

            Assert.AreEqual(checkCount_, ThreadCount * ThreadCount, "value should be ThreadCount * ThreadCount");
        }