public void NDCTest2() { 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 { NDC.Clear(); Assert.AreEqual(string.Empty, NDC.TopMessage); Assert.AreEqual(string.Empty, NDC.Pop()); AssertContents(NDC.GetAllMessages()); using (NDC.Push("foo")) { Assert.AreEqual("foo", NDC.TopMessage); AssertContents(NDC.GetAllMessages(), "foo"); using (NDC.Push("bar")) { AssertContents(NDC.GetAllMessages(), "bar", "foo"); Assert.AreEqual("bar", NDC.TopMessage); NDC.Push("baz"); AssertContents(NDC.GetAllMessages(), "baz", "bar", "foo"); Assert.AreEqual("baz", NDC.TopMessage); Assert.AreEqual("baz", NDC.Pop()); AssertContents(NDC.GetAllMessages(), "bar", "foo"); Assert.AreEqual("bar", NDC.TopMessage); } AssertContents(NDC.GetAllMessages(), "foo"); Assert.AreEqual("foo", NDC.TopMessage); } AssertContents(NDC.GetAllMessages()); Assert.AreEqual(string.Empty, NDC.Pop()); } catch (Exception ex) { lock (exceptions) { exceptions.Add(ex); } } finally { if (Interlocked.Decrement(ref remaining) == 0) { mre.Set(); } } }); } mre.WaitOne(); Assert.AreEqual(0, exceptions.Count); }
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()); }