Merge pull request #1074 from esdrubal/bug18421
authorRodrigo Kumpera <kumpera@gmail.com>
Thu, 19 Jun 2014 15:45:17 +0000 (11:45 -0400)
committerRodrigo Kumpera <kumpera@gmail.com>
Thu, 19 Jun 2014 15:45:17 +0000 (11:45 -0400)
Changed TryPeek to handle race condition with Dequeue.

1  2 
mcs/class/corlib/Test/System.Collections.Concurrent/ConcurrentQueueTests.cs

index 51aeeeb8e89b522ea4e2d263b27d3a91fcc72b97,df35b501787f59df719c89079da690cb93506097..91436a05b2127ccce3817228f27378facd793db7
@@@ -30,6 -30,7 +30,7 @@@ using System.Collections.Generic
  using System.Collections.Concurrent;
  
  using NUnit.Framework;
+ using MonoTests.System.Threading.Tasks;
  
  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()
                {
                {
                        queue.CopyTo (new int[3], 0);
                }
 -              
 +
                static WeakReference CreateWeakReference (object obj)
                {
                        return new WeakReference (obj);
                }
 -              
 +
                [Test]
 +              // This depends on precise stack scanning
 +              [Category ("NotWorking")]
                public void TryDequeueReferenceTest ()
                {
                        var obj = new Object ();