Merge pull request #1074 from esdrubal/bug18421
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentQueueTests.cs
index 51aeeeb8e89b522ea4e2d263b27d3a91fcc72b97..91436a05b2127ccce3817228f27378facd793db7 100644 (file)
@@ -30,6 +30,7 @@ using System.Collections.Generic;
 using System.Collections.Concurrent;
 
 using NUnit.Framework;
+using MonoTests.System.Threading.Tasks;
 
 namespace MonoTests.System.Collections.Concurrent
 {
@@ -115,6 +116,34 @@ namespace MonoTests.System.Collections.Concurrent
                        CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
                }
                
+               [Test]
+               public void StressTryPeekTestCase ()
+               {
+                       ParallelTestHelper.Repeat (delegate {
+                               var queue = new ConcurrentQueue<object> ();
+                               queue.Enqueue (new object());
+                               
+                               const int threads = 10;
+                               int threadCounter = 0;
+                               bool success = true;
+                               
+                               ParallelTestHelper.ParallelStressTest (queue, (q) => {
+                                       int threadId = Interlocked.Increment (ref threadCounter);
+                                       object temp;
+                                       if (threadId < threads)
+                                       {
+                                               while (queue.TryPeek (out temp))
+                                                       if (temp == null)
+                                                               success = false;
+                                       } else {
+                                               queue.TryDequeue (out temp);
+                                       }
+                               }, threads);
+                               
+                               Assert.IsTrue (success, "TryPeek returned unexpected null value.");
+                       }, 10);
+               }
+               
                [Test]
                public void CountTestCase()
                {