diff options
author | Alexander Köplinger <alex.koeplinger@outlook.com> | 2017-07-02 14:28:40 +0300 |
---|---|---|
committer | Alexander Köplinger <alex.koeplinger@outlook.com> | 2017-07-03 00:31:34 +0300 |
commit | ca55ce871f12960c58952a1926c064c33b9cac62 (patch) | |
tree | 7d806d6daefbca084488d46904043a1f049ad70c /mcs/class/Mono.Posix | |
parent | b3e3e4cc405d95b83c395540aec81762f73bdd74 (diff) |
[Mono.Posix] Don't use DateTime.Now for measuring elapsed time
Use Stopwatch or DateTime.UtcNow instead which aren't affected
by things like daylight-saving time
Diffstat (limited to 'mcs/class/Mono.Posix')
-rw-r--r-- | mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs | 6 | ||||
-rw-r--r-- | mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs | 33 |
2 files changed, 20 insertions, 19 deletions
diff --git a/mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs b/mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs index cb2809377a0..624d94ad027 100644 --- a/mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs +++ b/mcs/class/Mono.Posix/Mono.Remoting.Channels.Unix/UnixConnectionPool.cs @@ -153,7 +153,7 @@ namespace Mono.Remoting.Channels.Unix _pool = pool;
_client = client;
_stream = new BufferedStream (client.GetStream());
- _controlTime = DateTime.Now;
+ _controlTime = DateTime.UtcNow;
_buffer = new byte[UnixMessageIO.DefaultStreamBufferSize];
}
@@ -270,7 +270,7 @@ namespace Mono.Remoting.Channels.Unix {
lock (_pool)
{
- entry.ControlTime = DateTime.Now; // Initialize timeout
+ entry.ControlTime = DateTime.UtcNow; // Initialize timeout
_pool.Add (entry);
Monitor.Pulse (_pool);
}
@@ -295,7 +295,7 @@ namespace Mono.Remoting.Channels.Unix for (int n=0; n < _pool.Count; n++)
{
UnixConnection entry = (UnixConnection)_pool[n];
- if ( (DateTime.Now - entry.ControlTime).TotalSeconds > UnixConnectionPool.KeepAliveSeconds)
+ if ( (DateTime.UtcNow - entry.ControlTime).TotalSeconds > UnixConnectionPool.KeepAliveSeconds)
{
CancelConnection (entry);
_pool.RemoveAt(n);
diff --git a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs index 1516c51e3e5..b571e183c91 100644 --- a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs +++ b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixSignalTest.cs @@ -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()); } } |