Exemplo n.º 1
0
        public void timer_cannot_inherit_mappedcontext()
        {
            object getObject = null;
            string getValue  = null;

            var   mre    = new ManualResetEvent(false);
            Timer thread = new Timer((s) =>
            {
                try
                {
                    getObject = MDC.GetObject("DoNotExist");
                    getValue  = MDC.Get("DoNotExistEither");
                }
                finally
                {
                    mre.Set();
                }
            });

            thread.Change(0, Timeout.Infinite);
            mre.WaitOne();
            Assert.Null(getObject);
            Assert.Empty(getValue);
        }
Exemplo n.º 2
0
        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());
        }