Example #1
0
        public void NDCTest2_object()
        {
            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
                    {
                        NDC.Clear();
                        Assert.Null(NDC.TopObject);
                        Assert.Null(NDC.PopObject());
                        AssertContents(NDC.GetAllMessages());
                        using (NDC.Push("foo"))
                        {
                            Assert.Equal("foo", NDC.TopObject);
                            AssertContents(NDC.GetAllObjects(), "foo");
                            using (NDC.Push("bar"))
                            {
                                AssertContents(NDC.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NDC.TopObject);
                                NDC.Push("baz");
                                AssertContents(NDC.GetAllObjects(), "baz", "bar", "foo");
                                Assert.Equal("baz", NDC.TopObject);
                                Assert.Equal("baz", NDC.PopObject());

                                AssertContents(NDC.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NDC.TopObject);
                            }

                            AssertContents(NDC.GetAllObjects(), "foo");
                            Assert.Equal("foo", NDC.TopObject);
                        }

                        AssertContents(NDC.GetAllMessages());
                        Assert.Null(NDC.PopObject());
                    }
                    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());
        }