|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using BitFaster.Caching.Lru; |
| 5 | +using FluentAssertions; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace BitFaster.Caching.UnitTests.Std |
| 10 | +{ |
| 11 | + public class DurationTests |
| 12 | + { |
| 13 | + public static readonly ulong epsilon = (ulong)Duration.FromMilliseconds(20).raw; |
| 14 | + |
| 15 | + private readonly ITestOutputHelper testOutputHelper; |
| 16 | + |
| 17 | + public DurationTests(ITestOutputHelper testOutputHelper) |
| 18 | + { |
| 19 | + this.testOutputHelper = testOutputHelper; |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void SinceEpoch() |
| 24 | + { |
| 25 | + // On .NET Standard, only windows uses TickCount64 |
| 26 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 27 | + { |
| 28 | + Duration.SinceEpoch().raw.Should().BeCloseTo(Duration.GetTickCount64(), 15); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + // eps is 1/200 of a second |
| 33 | + ulong eps = (ulong)(Stopwatch.Frequency / 200); |
| 34 | + Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void ToTimeSpan() |
| 40 | + { |
| 41 | + // On .NET Standard, only windows uses TickCount64 |
| 42 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 43 | + { |
| 44 | + new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + // for Stopwatch.GetTimestamp() this is number of ticks |
| 49 | + new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void FromTimeSpan() |
| 55 | + { |
| 56 | + // On .NET Standard, only windows uses TickCount64 |
| 57 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 58 | + { |
| 59 | + Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw |
| 60 | + .Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw |
| 65 | + .Should().Be(Stopwatch.Frequency); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // This is for diagnostic purposes when tests run on different operating systems. |
| 70 | + [Fact] |
| 71 | + public void OutputTimeParameters() |
| 72 | + { |
| 73 | + this.testOutputHelper.WriteLine($"Stopwatch.Frequency {Stopwatch.Frequency}"); |
| 74 | + this.testOutputHelper.WriteLine($"TimeSpan.TicksPerSecond {TimeSpan.TicksPerSecond}"); |
| 75 | + this.testOutputHelper.WriteLine($"stopwatchAdjustmentFactor {StopwatchTickConverter.stopwatchAdjustmentFactor}"); |
| 76 | + var d = Duration.SinceEpoch(); |
| 77 | + this.testOutputHelper.WriteLine($"Duration.SinceEpoch {d.raw} ({d.ToTimeSpan()})"); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments