Exemple #1
0
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 500;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));
                        Assert.IsFalse(MDC.Contains("foo2"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.IsTrue(MDC.Contains("foo"));
                        Assert.AreEqual("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.IsFalse(MDC.Contains("foo"));
                        Assert.AreEqual(string.Empty, MDC.Get("foo"));

                        Assert.IsTrue(MDC.Contains("foo2"));
                        Assert.AreEqual("bar2", MDC.Get("foo2"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            if (exceptions.Count != 0)
            {
                Assert.Fail(exceptions[0].ToString());
            }
        }
        public void MDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 100;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        MDC.Clear();
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));
                        Assert.False(MDC.Contains("foo2"));
                        Assert.Equal(string.Empty, MDC.Get("foo2"));

                        MDC.Set("foo", "bar");
                        MDC.Set("foo2", "bar2");

                        Assert.True(MDC.Contains("foo"));
                        Assert.Equal("bar", MDC.Get("foo"));

                        MDC.Remove("foo");
                        Assert.False(MDC.Contains("foo"));
                        Assert.Equal(string.Empty, MDC.Get("foo"));

                        Assert.True(MDC.Contains("foo2"));
                        Assert.Equal("bar2", MDC.Get("foo2"));

                        Assert.Null(MDC.GetObject("foo3"));
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            StringBuilder exceptionsMessage = new StringBuilder();

            foreach (var ex in exceptions)
            {
                if (exceptionsMessage.Length > 0)
                {
                    exceptionsMessage.Append("\r\n");
                }

                exceptionsMessage.Append(ex.ToString());
            }

            Assert.True(exceptions.Count == 0, exceptionsMessage.ToString());
        }