Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core.Tests/Internal/TimespecTest.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2
3 // Copyright 2015, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #endregion
33
34 using System;
35 using System.Runtime.InteropServices;
36 using Grpc.Core.Internal;
37 using Grpc.Core.Utils;
38 using NUnit.Framework;
39
40 namespace Grpc.Core.Internal.Tests
41 {
42 public class TimespecTest
43 {
44 [Test]
45 public void Now_IsInUtc()
46 {
47 Assert.AreEqual(DateTimeKind.Utc, Timespec.Now.ToDateTime().Kind);
48 }
49
50 [Test]
51 public void Now_AgreesWithUtcNow()
52 {
53 var timespec = Timespec.Now;
54 var utcNow = DateTime.UtcNow;
55
56 TimeSpan difference = utcNow - timespec.ToDateTime();
57
58 // This test is inherently a race - but the two timestamps
59 // should really be way less that a minute apart.
60 Assert.IsTrue(difference.TotalSeconds < 60);
61 }
62
63 [Test]
64 public void InfFuture()
65 {
66 var timespec = Timespec.InfFuture;
67 }
68
69 [Test]
70 public void InfPast()
71 {
72 var timespec = Timespec.InfPast;
73 }
74
75 [Test]
76 public void TimespecSizeIsNativeSize()
77 {
78 Assert.AreEqual(Timespec.NativeSize, Marshal.SizeOf(typeof(Timespec) ));
79 }
80
81 [Test]
82 public void ToDateTime()
83 {
84 Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
85 new Timespec(0, 0).ToDateTime());
86
87 Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 10, DateTimeKind.Utc) .AddTicks(50),
88 new Timespec(10, 5000).ToDateTime());
89
90 Assert.AreEqual(new DateTime(2015, 7, 21, 4, 21, 48, DateTimeKind.Ut c),
91 new Timespec(1437452508, 0).ToDateTime());
92
93 // before epoch
94 Assert.AreEqual(new DateTime(1969, 12, 31, 23, 59, 55, DateTimeKind. Utc).AddTicks(10),
95 new Timespec(-5, 1000).ToDateTime());
96
97 // infinity
98 Assert.AreEqual(DateTime.MaxValue, Timespec.InfFuture.ToDateTime());
99 Assert.AreEqual(DateTime.MinValue, Timespec.InfPast.ToDateTime());
100
101 // nanos are rounded to ticks are rounded up
102 Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc). AddTicks(1),
103 new Timespec(0, 99).ToDateTime());
104
105 // Illegal inputs
106 Assert.Throws(typeof(InvalidOperationException),
107 () => new Timespec(0, -2).ToDateTime());
108 Assert.Throws(typeof(InvalidOperationException),
109 () => new Timespec(0, 1000 * 1000 * 1000).ToDateTime());
110 Assert.Throws(typeof(InvalidOperationException),
111 () => new Timespec(0, 0, GPRClockType.Monotonic).ToDateTime());
112 }
113
114 [Test]
115 public void ToDateTime_ReturnsUtc()
116 {
117 Assert.AreEqual(DateTimeKind.Utc, new Timespec(1437452508, 0).ToDate Time().Kind);
118 Assert.AreNotEqual(DateTimeKind.Unspecified, new Timespec(1437452508 , 0).ToDateTime().Kind);
119 }
120
121 [Test]
122 public void ToDateTime_Overflow()
123 {
124 var timespec = new Timespec(long.MaxValue - 100, 0);
125 Assert.AreNotEqual(Timespec.InfFuture, timespec);
126 Assert.AreEqual(DateTime.MaxValue, timespec.ToDateTime());
127
128 Assert.AreEqual(DateTime.MinValue, new Timespec(long.MinValue + 100, 0).ToDateTime());
129 }
130
131 [Test]
132 public void ToDateTime_OutOfDateTimeRange()
133 {
134 // DateTime range goes up to year 9999, 20000 years from now should
135 // be out of range.
136 long seconds = 20000L * 365L * 24L * 3600L;
137
138 var timespec = new Timespec(seconds, 0);
139 Assert.AreNotEqual(Timespec.InfFuture, timespec);
140 Assert.AreEqual(DateTime.MaxValue, timespec.ToDateTime());
141
142 Assert.AreEqual(DateTime.MinValue, new Timespec(-seconds, 0).ToDateT ime());
143 }
144
145 [Test]
146 public void FromDateTime()
147 {
148 Assert.AreEqual(new Timespec(0, 0),
149 Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTime Kind.Utc)));
150
151 Assert.AreEqual(new Timespec(10, 5000),
152 Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 10, DateTim eKind.Utc).AddTicks(50)));
153
154 Assert.AreEqual(new Timespec(1437452508, 0),
155 Timespec.FromDateTime(new DateTime(2015, 7, 21, 4, 21, 48, DateT imeKind.Utc)));
156
157 // before epoch
158 Assert.AreEqual(new Timespec(-5, 1000),
159 Timespec.FromDateTime(new DateTime(1969, 12, 31, 23, 59, 55, Dat eTimeKind.Utc).AddTicks(10)));
160
161 // infinity
162 Assert.AreEqual(Timespec.InfFuture, Timespec.FromDateTime(DateTime.M axValue));
163 Assert.AreEqual(Timespec.InfPast, Timespec.FromDateTime(DateTime.Min Value));
164
165 // illegal inputs
166 Assert.Throws(typeof(ArgumentException),
167 () => Timespec.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, Da teTimeKind.Unspecified)));
168 }
169
170 [Test]
171 [Category("Performance")]
172 [Ignore("Prevent running on Jenkins")]
173 public void NowBenchmark()
174 {
175 // approx Timespec.Now latency <33ns
176 BenchmarkUtil.RunBenchmark(10000000, 1000000000, () => { var now = T imespec.Now; });
177 }
178
179 [Test]
180 [Category("Performance")]
181 [Ignore("Prevent running on Jenkins")]
182 public void PreciseNowBenchmark()
183 {
184 // approx Timespec.PreciseNow latency <18ns (when compiled with GRPC _TIMERS_RDTSC)
185 BenchmarkUtil.RunBenchmark(10000000, 1000000000, () => { var now = T imespec.PreciseNow; });
186 }
187 }
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698