[Mono.Posix] Don't use DateTime.Now for measuring elapsed time
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Sun, 2 Jul 2017 11:28:40 +0000 (13:28 +0200)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Sun, 2 Jul 2017 21:31:34 +0000 (23:31 +0200)
Use Stopwatch or DateTime.UtcNow instead which aren't affected
by things like daylight-saving time

mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs
mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs

index cb2809377a0eb7e877433b5e8a878a284d015fae..624d94ad027a0dc96febaf0f8ddbbe09f82bb6ad 100644 (file)
@@ -153,7 +153,7 @@ namespace Mono.Remoting.Channels.Unix
                        _pool = pool;\r
                        _client = client;\r
                        _stream = new BufferedStream (client.GetStream());\r
-                       _controlTime = DateTime.Now;\r
+                       _controlTime = DateTime.UtcNow;\r
                        _buffer = new byte[UnixMessageIO.DefaultStreamBufferSize];\r
                }\r
 \r
@@ -270,7 +270,7 @@ namespace Mono.Remoting.Channels.Unix
                {\r
                        lock (_pool)\r
                        {\r
-                               entry.ControlTime = DateTime.Now;       // Initialize timeout\r
+                               entry.ControlTime = DateTime.UtcNow;    // Initialize timeout\r
                                _pool.Add (entry);\r
                                Monitor.Pulse (_pool);\r
                        }\r
@@ -295,7 +295,7 @@ namespace Mono.Remoting.Channels.Unix
                                for (int n=0; n < _pool.Count; n++)\r
                                {\r
                                        UnixConnection entry = (UnixConnection)_pool[n];\r
-                                       if ( (DateTime.Now - entry.ControlTime).TotalSeconds > UnixConnectionPool.KeepAliveSeconds)\r
+                                       if ( (DateTime.UtcNow - entry.ControlTime).TotalSeconds > UnixConnectionPool.KeepAliveSeconds)\r
                                        {\r
                                                CancelConnection (entry);\r
                                                _pool.RemoveAt(n);\r
index 1516c51e3e5868fcc52dc72df252690d40d2d815..b571e183c9109b0e4eee01ac32effe8968606f68 100644 (file)
@@ -10,6 +10,7 @@
 using NUnit.Framework;
 
 using System;
+using System.Diagnostics;
 using System.Text;
 using System.Threading;
 using Mono.Unix;
@@ -25,12 +26,12 @@ namespace MonoTests.Mono.Unix {
                static Thread CreateWaitSignalThread (UnixSignal signal, int timeout)
                {
                        Thread t1 = new Thread(delegate() {
-                                               DateTime start = DateTime.Now;
+                                               var sw = Stopwatch.StartNew ();
                                                bool r = signal.WaitOne (timeout, false);
-                                               DateTime end = DateTime.Now;
+                                               sw.Stop ();
                                                Assert.AreEqual (signal.Count, 1);
                                                Assert.AreEqual (r, true);
-                                               if ((end - start) > new TimeSpan (0, 0, timeout/1000))
+                                               if (sw.Elapsed > new TimeSpan (0, 0, timeout/1000))
                                                        throw new InvalidOperationException ("Signal slept too long");
                                        });
                        return t1;
@@ -248,12 +249,12 @@ namespace MonoTests.Mono.Unix {
                {
                        Thread t1 = new Thread (delegate () {
                                        using (UnixSignal a = new UnixSignal (Signum.SIGINT)) {
-                                               DateTime start = DateTime.Now;
+                                               var sw = Stopwatch.StartNew ();
                                                bool r = a.WaitOne (5000, false);
-                                               DateTime end = DateTime.Now;
+                                               sw.Stop ();
                                                Assert.AreEqual (a.Count, 1);
                                                Assert.AreEqual (r, true);
-                                               if ((end - start) > new TimeSpan (0, 0, 5))
+                                               if (sw.Elapsed > new TimeSpan (0, 0, 5))
                                                        throw new InvalidOperationException ("Signal slept too long");
                                        }
                        });
@@ -272,12 +273,12 @@ namespace MonoTests.Mono.Unix {
                {
                        Thread t1 = new Thread (delegate () {
                                        using (UnixSignal a = new UnixSignal (Signum.SIGINT)) {
-                                               DateTime start = DateTime.Now;
+                                               var sw = Stopwatch.StartNew ();
                                                int idx = UnixSignal.WaitAny (new UnixSignal[]{a}, 5000);
-                                               DateTime end = DateTime.Now;
+                                               sw.Stop ();
                                                Assert.AreEqual (idx, 0);
                                                Assert.AreEqual (a.Count, 1);
-                                               if ((end - start) > new TimeSpan (0, 0, 5))
+                                               if (sw.Elapsed > new TimeSpan (0, 0, 5))
                                                        throw new InvalidOperationException ("Signal slept too long");
                                        }
                        });
@@ -297,13 +298,13 @@ namespace MonoTests.Mono.Unix {
                        Thread t1 = new Thread (delegate () {
                                        using (UnixSignal a = new UnixSignal (Signum.SIGINT))
                                        using (UnixSignal b = new UnixSignal (Signum.SIGTERM)) {
-                                               DateTime start = DateTime.Now;
+                                               var sw = Stopwatch.StartNew ();
                                                int idx = UnixSignal.WaitAny (new UnixSignal[]{a, b}, 5000);
-                                               DateTime end = DateTime.Now;
+                                               sw.Stop ();
                                                Assert.AreEqual (idx, 1);
                                                Assert.AreEqual (a.Count, 0);
                                                Assert.AreEqual (b.Count, 1);
-                                               if ((end - start) > new TimeSpan (0, 0, 5))
+                                               if (sw.Elapsed > new TimeSpan (0, 0, 5))
                                                        throw new InvalidOperationException ("Signal slept too long");
                                        }
                        });
@@ -321,12 +322,12 @@ namespace MonoTests.Mono.Unix {
                public void TestNoEmit ()
                {
                        using (UnixSignal u = new UnixSignal (Signum.SIGINT)) {
-                               DateTime start = DateTime.Now;
+                               var sw = Stopwatch.StartNew ();
                                bool r = u.WaitOne (5100, false);
                                Assert.AreEqual (r, false);
-                               DateTime end = DateTime.Now;
-                               if ((end - start) < new TimeSpan (0, 0, 5))
-                                       throw new InvalidOperationException ("Signal didn't block for 5s; blocked for " + (end-start).ToString());
+                               sw.Stop ();
+                               if (sw.Elapsed < new TimeSpan (0, 0, 5))
+                                       throw new InvalidOperationException ("Signal didn't block for 5s; blocked for " + sw.Elapsed.TotalSeconds.ToString());
                        }
                }