[corlib] Don't use DateTime.Now for measuring elapsed time
[mono.git] / mcs / class / corlib / Test / System.Threading / ThreadPoolTest.cs
index 122eb7a8723632bd3998ebf4b41719adc806a0fb..9dace8484e5e06c64f6c7a83e3e896cebe2a24f7 100644 (file)
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Diagnostics;
 using System.Threading;
 using NUnit.Framework;
 
@@ -91,8 +92,8 @@ namespace MonoTests.System.Threading
                                else
                                        ThreadPool.QueueUserWorkItem (_ => { Interlocked.Increment (ref sum); Interlocked.Increment (ref total); });
                        }
-                       var start = DateTime.Now;
-                       while ((total != n || sum != 0) && (DateTime.Now - start).TotalSeconds < 60)
+                       var sw = Stopwatch.StartNew ();
+                       while ((total != n || sum != 0) && sw.Elapsed.TotalSeconds < 60)
                                Thread.Sleep (1000);
                        Assert.IsTrue (total == n, "#1");
                        Assert.IsTrue (sum   == 0, "#2");
@@ -129,8 +130,8 @@ namespace MonoTests.System.Threading
 
                        workerThreads_new = workerThreads == 1 ? 2 : 1;
                        completionPortThreads_new = completionPortThreads == 1 ? 2 : 1;
-
                        ThreadPool.SetMinThreads (workerThreads_new, completionPortThreads_new);
+
                        ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
                        Assert.IsTrue (workerThreads == workerThreads_new, "#3");
                        Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
@@ -149,8 +150,8 @@ namespace MonoTests.System.Threading
 
                        workerThreads_new = workerThreads == cpuCount ? cpuCount + 1 : cpuCount;
                        completionPortThreads_new = completionPortThreads == cpuCount ? cpuCount + 1 : cpuCount;
-
                        ThreadPool.SetMaxThreads (workerThreads_new, completionPortThreads_new);
+
                        ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
                        Assert.IsTrue (workerThreads == workerThreads_new, "#3");
                        Assert.IsTrue (completionPortThreads == completionPortThreads_new, "#4");
@@ -160,16 +161,20 @@ namespace MonoTests.System.Threading
                public void GetAvailableThreads ()
                {
                        ManualResetEvent mre = new ManualResetEvent (false);
-                       DateTime start = DateTime.Now;
+                       var sw = Stopwatch.StartNew ();
                        int i, workerThreads, completionPortThreads;
 
                        try {
+                               Assert.IsTrue (ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount));
+
                                while (true) {
                                        ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
                                        if (workerThreads == 0)
                                                break;
 
-                                       if ((DateTime.Now - start).TotalSeconds >= 10)
+                                       Console.WriteLine ("workerThreads = {0}, completionPortThreads = {1}", workerThreads, completionPortThreads);
+
+                                       if (sw.Elapsed.TotalSeconds >= 10)
                                                Assert.Fail ("did not reach 0 available threads");
 
                                        ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);