From 112c2ecc3e792db3e213fe3f3196d5b4c080f48f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 29 Jan 2015 21:50:34 -0500 Subject: [PATCH] Update usage of StartNew/ContinueWith in tests Searched through tests for usage of StartNew and ContinueWith. - Replaced StartNew calls with Task.Run when the parameters to StartNew were all the defaults used by Task.Run or where no scheduler was specified but should have been. (I also changed one or two places to use Task.Run where LongRunning had been specified, but where using LongRunning with the default scheduler would have resulted in thousands of threads getting created). - Ensured remaining StartNew and ContinueWith calls were all targeting TaskScheduler.Default explicitly. --- .../tests/BlockingCollectionTests.cs | 12 ++++---- .../tests/ConcurrentBagTests.cs | 12 ++++---- .../tests/ConcurrentDictionaryTests.cs | 8 +++--- .../tests/ConcurrentQueueTests.cs | 28 ++++++++----------- .../tests/ConcurrentStackTests.cs | 8 +++--- .../tests/Queue/Queue_Synchronized.cs | 8 +++--- .../tests/Queue/Queue_get_SyncRoot.cs | 6 ++-- .../tests/Stack/Stack_get_SyncRoot.cs | 5 ++-- 8 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/System.Collections.Concurrent/tests/BlockingCollectionTests.cs b/src/System.Collections.Concurrent/tests/BlockingCollectionTests.cs index 7ae4f452b673..dcf301e06bc5 100644 --- a/src/System.Collections.Concurrent/tests/BlockingCollectionTests.cs +++ b/src/System.Collections.Concurrent/tests/BlockingCollectionTests.cs @@ -22,7 +22,7 @@ public static void TestBasicScenarios() Task[] tks = new Task[2]; // A simple blocking consumer with no cancellation. int expect = 1; - tks[0] = Task.Factory.StartNew(() => + tks[0] = Task.Run(() => { while (!bc.IsCompleted) { @@ -34,17 +34,17 @@ public static void TestBasicScenarios() } catch (InvalidOperationException) { } // throw when CompleteAdding called } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); // A simple blocking producer with no cancellation. - tks[1] = Task.Factory.StartNew(() => + tks[1] = Task.Run(() => { bc.Add(1); bc.Add(2); bc.Add(3); // Let consumer know we are done. bc.CompleteAdding(); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); Task.WaitAll(tks); } @@ -161,10 +161,10 @@ public static void TestBugFix914998() producer2.CompleteAdding(); - Task.Factory.StartNew(() => + Task.Run(() => { producer1.Add(100); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); int ignored, index, timeout = 5000; index = BlockingCollection.TryTakeFromAny(producerArray, out ignored, timeout); diff --git a/src/System.Collections.Concurrent/tests/ConcurrentBagTests.cs b/src/System.Collections.Concurrent/tests/ConcurrentBagTests.cs index 1012fa8216ce..a9401c686e10 100644 --- a/src/System.Collections.Concurrent/tests/ConcurrentBagTests.cs +++ b/src/System.Collections.Concurrent/tests/ConcurrentBagTests.cs @@ -19,15 +19,15 @@ public static void TestBasicScenarios() { ConcurrentBag cb = new ConcurrentBag(); Task[] tks = new Task[2]; - tks[0] = Task.Factory.StartNew(() => + tks[0] = Task.Run(() => { cb.Add(4); cb.Add(5); cb.Add(6); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); // Consume the items in the bag - tks[1] = Task.Factory.StartNew(() => + tks[1] = Task.Run(() => { int item; while (!cb.IsEmpty) @@ -40,7 +40,7 @@ public static void TestBasicScenarios() Assert.False(true, "Expected: 4|5|6; actual: " + item.ToString()); } } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); Task.WaitAll(tks); } @@ -398,12 +398,12 @@ public static void RTest9_ToArray() Task[] tasks = new Task[10]; for (int i = 0; i < tasks.Length; i++) { - tasks[i] = Task.Factory.StartNew(() => + tasks[i] = Task.Run(() => { int[] array = bag.ToArray(); if (array == null || array.Length != 10000) Interlocked.Increment(ref failCount); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); } Task.WaitAll(tasks); diff --git a/src/System.Collections.Concurrent/tests/ConcurrentDictionaryTests.cs b/src/System.Collections.Concurrent/tests/ConcurrentDictionaryTests.cs index 391291fa322d..0781a91c4b3c 100644 --- a/src/System.Collections.Concurrent/tests/ConcurrentDictionaryTests.cs +++ b/src/System.Collections.Concurrent/tests/ConcurrentDictionaryTests.cs @@ -19,7 +19,7 @@ public static void TestBasicScenarios() ConcurrentDictionary cd = new ConcurrentDictionary(); Task[] tks = new Task[2]; - tks[0] = Task.Factory.StartNew(() => + tks[0] = Task.Run(() => { var ret = cd.TryAdd(1, 11); if (!ret) @@ -34,9 +34,9 @@ public static void TestBasicScenarios() ret = cd.TryUpdate(2, 22, 222); Assert.True(ret); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); - tks[1] = Task.Factory.StartNew(() => + tks[1] = Task.Run(() => { var ret = cd.TryAdd(2, 222); if (!ret) @@ -51,7 +51,7 @@ public static void TestBasicScenarios() ret = cd.TryUpdate(1, 111, 11); Assert.True(ret); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); Task.WaitAll(tks); } diff --git a/src/System.Collections.Concurrent/tests/ConcurrentQueueTests.cs b/src/System.Collections.Concurrent/tests/ConcurrentQueueTests.cs index afb9814e57a9..9a9b83795219 100644 --- a/src/System.Collections.Concurrent/tests/ConcurrentQueueTests.cs +++ b/src/System.Collections.Concurrent/tests/ConcurrentQueueTests.cs @@ -20,14 +20,14 @@ public static void TestBasicScenarios() cq.Enqueue(1); Task[] tks = new Task[2]; - tks[0] = Task.Factory.StartNew(() => + tks[0] = Task.Run(() => { cq.Enqueue(2); cq.Enqueue(3); cq.Enqueue(4); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); - tks[1] = Task.Factory.StartNew(() => + tks[1] = Task.Run(() => { int item1, item2; var ret1 = cq.TryDequeue(out item1); @@ -44,7 +44,7 @@ public static void TestBasicScenarios() { Assert.Equal(1, item1); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); Task.WaitAll(tks); } @@ -138,8 +138,7 @@ public static void TestBugFix570046() { var q = new ConcurrentQueue(); - var t1 = Task.Factory.StartNew( - () => + var t1 = Task.Run(() => { for (int i = 0; i < 1000000; i++) { @@ -148,16 +147,15 @@ public static void TestBugFix570046() Assert.True(q.TryDequeue(out o), "TryDequeue should never return false in this test"); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); - var t2 = Task.Factory.StartNew( - () => + var t2 = Task.Run(() => { foreach (var item in q) { Assert.NotNull(item); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); t2.Wait(); } @@ -171,13 +169,11 @@ public static void TestBugFix570046() public static void TestBugFix484295() { CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(15)); - Task peepTask = Task.Factory - .StartNew(() => TryPeek(cts.Token), cts.Token) - .ContinueWith(task => HandleExceptions(task)); + Task peepTask = Task + .Run(() => TryPeek(cts.Token), cts.Token) + .ContinueWith(task => HandleExceptions(task), TaskScheduler.Default); - Task queueDequeueTask = Task.Factory.StartNew( - () => QueueDequeue(cts.Token), - cts.Token, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + Task queueDequeueTask = Task.Run(() => QueueDequeue(cts.Token)); Console.WriteLine("Waiting 15 seconds for both Queue/Dequeue and TryPeek tasks.."); Task.WaitAll(peepTask, queueDequeueTask); diff --git a/src/System.Collections.Concurrent/tests/ConcurrentStackTests.cs b/src/System.Collections.Concurrent/tests/ConcurrentStackTests.cs index 2b6f3d485776..3b669854538d 100644 --- a/src/System.Collections.Concurrent/tests/ConcurrentStackTests.cs +++ b/src/System.Collections.Concurrent/tests/ConcurrentStackTests.cs @@ -21,14 +21,14 @@ public static void TestBasicScenarios() cs.Push(1); Task[] tks = new Task[2]; - tks[0] = Task.Factory.StartNew(() => + tks[0] = Task.Run(() => { cs.Push(2); cs.Push(3); cs.Push(4); - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); - tks[1] = Task.Factory.StartNew(() => + tks[1] = Task.Run(() => { int item1, item2; var ret1 = cs.TryPop(out item1); @@ -44,7 +44,7 @@ public static void TestBasicScenarios() { Assert.Equal(1, item1); } - }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + }); Task.WaitAll(tks); } diff --git a/src/System.Collections.NonGeneric/tests/Queue/Queue_Synchronized.cs b/src/System.Collections.NonGeneric/tests/Queue/Queue_Synchronized.cs index 8cce3ee76eac..793d288e3598 100644 --- a/src/System.Collections.NonGeneric/tests/Queue/Queue_Synchronized.cs +++ b/src/System.Collections.NonGeneric/tests/Queue/Queue_Synchronized.cs @@ -162,7 +162,7 @@ public Boolean runTest() { for (int i = 0; i < m_ThreadsToUse; i++) { - ths[i] = Task.Factory.StartNew(new Action(this.StartEnThread), TaskCreationOptions.LongRunning); + ths[i] = Task.Factory.StartNew(new Action(this.StartEnThread), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } m_ThreadCount = m_ThreadsToUse; Task.WaitAll(ths); @@ -190,7 +190,7 @@ public Boolean runTest() m_ThreadCount = m_ThreadsToUse; for (int i = 0; i < m_ThreadsToUse; i++) { - ths[i] = Task.Factory.StartNew(new Action(this.StartDeThread), TaskCreationOptions.LongRunning); + ths[i] = Task.Factory.StartNew(new Action(this.StartDeThread), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } Task.WaitAll(ths); @@ -218,7 +218,7 @@ public Boolean runTest() m_ThreadCount = m_ThreadsToUse; for (int i = 0; i < m_ThreadsToUse; i++) { - ths[i] = Task.Factory.StartNew(new Action(this.StartDeEnThread), TaskCreationOptions.LongRunning); + ths[i] = Task.Factory.StartNew(new Action(this.StartDeEnThread), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } Task.WaitAll(ths); @@ -245,7 +245,7 @@ public Boolean runTest() m_ThreadCount = m_ThreadsToUse; for (int i = 0; i < m_ThreadsToUse; i++) { - ths[i] = Task.Factory.StartNew(new Action(this.StartEnDeThread), TaskCreationOptions.LongRunning); + ths[i] = Task.Factory.StartNew(new Action(this.StartEnDeThread), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } Task.WaitAll(ths); diff --git a/src/System.Collections.NonGeneric/tests/Queue/Queue_get_SyncRoot.cs b/src/System.Collections.NonGeneric/tests/Queue/Queue_get_SyncRoot.cs index 9f926d050a9e..969ff562b4c6 100644 --- a/src/System.Collections.NonGeneric/tests/Queue/Queue_get_SyncRoot.cs +++ b/src/System.Collections.NonGeneric/tests/Queue/Queue_get_SyncRoot.cs @@ -80,15 +80,13 @@ public bool runTest() iCountErrors++; } - //we are going to rumble with the Queues with 2 threads - workers = new Task[iNumberOfWorkers]; ts1 = new Action(SortElements); ts2 = new Action(ReverseElements); for (int iThreads = 0; iThreads < iNumberOfWorkers; iThreads += 2) { - workers[iThreads] = Task.Factory.StartNew(ts1, TaskCreationOptions.LongRunning); - workers[iThreads + 1] = Task.Factory.StartNew(ts2, TaskCreationOptions.LongRunning); + workers[iThreads] = Task.Run(ts1); + workers[iThreads + 1] = Task.Run(ts2); } Task.WaitAll(workers); diff --git a/src/System.Collections.NonGeneric/tests/Stack/Stack_get_SyncRoot.cs b/src/System.Collections.NonGeneric/tests/Stack/Stack_get_SyncRoot.cs index f002ead26ede..d26416d66944 100644 --- a/src/System.Collections.NonGeneric/tests/Stack/Stack_get_SyncRoot.cs +++ b/src/System.Collections.NonGeneric/tests/Stack/Stack_get_SyncRoot.cs @@ -90,14 +90,13 @@ public bool runTest() Console.WriteLine("Err_234dnvf! Expected value not returned, " + (arrSon.SyncRoot == arrMother.SyncRoot)); } - //we are going to rumble with the Stacks with 2 threads var ts1 = new Action(SortElements); var ts2 = new Action(ReverseElements); Task[] tasks = new Task[iNumberOfWorkers]; for (int iThreads = 0; iThreads < iNumberOfWorkers; iThreads += 2) { - tasks[iThreads] = Task.Factory.StartNew(ts1, TaskCreationOptions.LongRunning); - tasks[iThreads + 1] = Task.Factory.StartNew(ts2, TaskCreationOptions.LongRunning); + tasks[iThreads] = Task.Run(ts1); + tasks[iThreads + 1] = Task.Run(ts2); } Task.WaitAll(tasks);