• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Runtime.InteropServices;
21 using Grpc.Core.Internal;
22 using Grpc.Core.Utils;
23 using NUnit.Framework;
24 
25 namespace Grpc.Core.Internal.Tests
26 {
27     public class TimespecTest
28     {
29         [Test]
Now_IsInUtc()30         public void Now_IsInUtc()
31         {
32             Assert.AreEqual(DateTimeKind.Utc, Timespec.Now.ToDateTime().Kind);
33         }
34 
35         [Test]
Now_AgreesWithUtcNow()36         public void Now_AgreesWithUtcNow()
37         {
38             var timespec = Timespec.Now;
39             var utcNow = DateTime.UtcNow;
40 
41             TimeSpan difference = utcNow - timespec.ToDateTime();
42 
43             // This test is inherently a race - but the two timestamps
44             // should really be way less that a minute apart.
45             Assert.IsTrue(difference.TotalSeconds < 60);
46         }
47 
48         [Test]
InfFutureMatchesNativeValue()49         public void InfFutureMatchesNativeValue()
50         {
51             Assert.AreEqual(Timespec.NativeInfFuture, Timespec.InfFuture);
52         }
53 
54         [Test]
InfPastMatchesNativeValue()55         public void InfPastMatchesNativeValue()
56         {
57             Assert.AreEqual(Timespec.NativeInfPast, Timespec.InfPast);
58         }
59 
60         [Test]
TimespecSizeIsNativeSize()61         public void TimespecSizeIsNativeSize()
62         {
63             #pragma warning disable 0618
64             // We need to use the obsolete non-generic version of Marshal.SizeOf because the generic version is not available in net45
65             Assert.AreEqual(Timespec.NativeSize, Marshal.SizeOf(typeof(Timespec)));
66             #pragma warning restore 0618
67         }
68 
69         [Test]
ToDateTime()70         public void ToDateTime()
71         {
72             Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
73                 new Timespec(0, 0).ToDateTime());
74 
75             Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 10, DateTimeKind.Utc).AddTicks(50),
76                 new Timespec(10, 5000).ToDateTime());
77 
78             Assert.AreEqual(new DateTime(2015, 7, 21, 4, 21, 48, DateTimeKind.Utc),
79                 new Timespec(1437452508, 0).ToDateTime());
80 
81             // before epoch
82             Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 55, DateTimeKind.Utc).AddTicks(10),
83                 new Timespec(-5, 1000).ToDateTime());
84 
85             // infinity
86             Assert.AreEqual(DateTime.MaxValue, Timespec.InfFuture.ToDateTime());
87             Assert.AreEqual(DateTime.MinValue, Timespec.InfPast.ToDateTime());
88 
89             // nanos are rounded to ticks are rounded up
90             Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(1),
91                 new Timespec(0, 99).ToDateTime());
92 
93             // Illegal inputs
94             Assert.Throws(typeof(InvalidOperationException),
95                 () => new Timespec(0, -2).ToDateTime());
96             Assert.Throws(typeof(InvalidOperationException),
97                 () => new Timespec(0, 1000 * 1000 * 1000).ToDateTime());
98             Assert.Throws(typeof(InvalidOperationException),
99                 () => new Timespec(0, 0, ClockType.Monotonic).ToDateTime());
100         }
101 
102         [Test]
ToDateTime_ReturnsUtc()103         public void ToDateTime_ReturnsUtc()
104         {
105             Assert.AreEqual(DateTimeKind.Utc, new Timespec(1437452508, 0).ToDateTime().Kind);
106             Assert.AreNotEqual(DateTimeKind.Unspecified, new Timespec(1437452508, 0).ToDateTime().Kind);
107         }
108 
109         [Test]
ToDateTime_Overflow()110         public void ToDateTime_Overflow()
111         {
112             var timespec = new Timespec(long.MaxValue - 100, 0);
113             Assert.AreNotEqual(Timespec.InfFuture, timespec);
114             Assert.AreEqual(DateTime.MaxValue, timespec.ToDateTime());
115 
116             Assert.AreEqual(DateTime.MinValue, new Timespec(long.MinValue + 100, 0).ToDateTime());
117         }
118 
119         [Test]
ToDateTime_OutOfDateTimeRange()120         public void ToDateTime_OutOfDateTimeRange()
121         {
122             // DateTime range goes up to year 9999, 20000 years from now should
123             // be out of range.
124             long seconds = 20000L * 365L * 24L * 3600L;
125 
126             var timespec = new Timespec(seconds, 0);
127             Assert.AreNotEqual(Timespec.InfFuture, timespec);
128             Assert.AreEqual(DateTime.MaxValue, timespec.ToDateTime());
129 
130             Assert.AreEqual(DateTime.MinValue, new Timespec(-seconds, 0).ToDateTime());
131         }
132 
133         [Test]
FromDateTime()134         public void FromDateTime()
135         {
136             Assert.AreEqual(new Timespec(0, 0),
137                 Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)));
138 
139             Assert.AreEqual(new Timespec(10, 5000),
140                 Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 10, DateTimeKind.Utc).AddTicks(50)));
141 
142             Assert.AreEqual(new Timespec(1437452508, 0),
143                 Timespec.FromDateTime(new DateTime(2015, 7, 21, 4, 21, 48, DateTimeKind.Utc)));
144 
145             // before epoch
146             Assert.AreEqual(new Timespec(-5, 1000),
147                 Timespec.FromDateTime(new DateTime(1969, 12, 31, 23, 59, 55, DateTimeKind.Utc).AddTicks(10)));
148 
149             // infinity
150             Assert.AreEqual(Timespec.InfFuture, Timespec.FromDateTime(DateTime.MaxValue));
151             Assert.AreEqual(Timespec.InfPast, Timespec.FromDateTime(DateTime.MinValue));
152 
153             // illegal inputs
154             Assert.Throws(typeof(ArgumentException),
155                 () => Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)));
156         }
157 
158         [Test]
159         [Category("Performance")]
160         [Ignore("Prevent running on Jenkins")]
NowBenchmark()161         public void NowBenchmark()
162         {
163             // approx Timespec.Now latency <33ns
164             BenchmarkUtil.RunBenchmark(10000000, 1000000000, () => { var now = Timespec.Now; });
165         }
166 
167         [Test]
168         [Category("Performance")]
169         [Ignore("Prevent running on Jenkins")]
PreciseNowBenchmark()170         public void PreciseNowBenchmark()
171         {
172             // approx Timespec.PreciseNow latency <18ns (when compiled with GRPC_TIMERS_RDTSC)
173             BenchmarkUtil.RunBenchmark(10000000, 1000000000, () => { var now = Timespec.PreciseNow; });
174         }
175     }
176 }
177