Skip to content

Commit 36a7d57

Browse files
authored
Test all Duration code paths including .NET Standard (#579)
* test duration on std * move * dedicated tests * test fail * res * fix file name ---------
1 parent 4223d10 commit 36a7d57

File tree

6 files changed

+142
-8
lines changed

6 files changed

+142
-8
lines changed

.github/workflows/gate.yml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ jobs:
6262
with:
6363
name: test-results-win6
6464
path: BitFaster.Caching.UnitTests/TestResults/results6.trx
65+
- name: Upload test results (6.0 .NET Std)
66+
uses: actions/upload-artifact@v4
67+
if: success() || failure()
68+
with:
69+
name: test-results-win6-std
70+
path: BitFaster.Caching.UnitTests.Std/TestResults/results6.trx
71+
6572
- name: Publish coverage report to coveralls.io (6.0)
6673
uses: coverallsapp/github-action@master
6774
with:
@@ -71,7 +78,7 @@ jobs:
7178
parallel: true
7279

7380
- name: Publish NuGet artifacts
74-
uses: actions/upload-artifact@v3
81+
uses: actions/upload-artifact@v4
7582
with:
7683
name: NuGet package
7784
path: BitFaster.Caching/bin/Release/
@@ -105,11 +112,17 @@ jobs:
105112
flag-name: mac
106113
parallel: true
107114
- name: Upload test results (6.0)
108-
uses: actions/upload-artifact@v4 # upload test results
109-
if: success() || failure() # run this step even if previous step failed
115+
uses: actions/upload-artifact@v4
116+
if: success() || failure()
110117
with:
111118
name: test-results-mac
112119
path: BitFaster.Caching.UnitTests/TestResults/results.trx
120+
- name: Upload test results (6.0 .NET Std)
121+
uses: actions/upload-artifact@v4
122+
if: success() || failure()
123+
with:
124+
name: test-results-mac-std
125+
path: BitFaster.Caching.UnitTests.Std/TestResults/results.trx
113126

114127
linux:
115128

@@ -140,11 +153,17 @@ jobs:
140153
flag-name: linux
141154
parallel: true
142155
- name: Upload test results (6.0)
143-
uses: actions/upload-artifact@v4 # upload test results
144-
if: success() || failure() # run this step even if previous step failed
156+
uses: actions/upload-artifact@v4
157+
if: success() || failure()
145158
with:
146159
name: test-results-linux
147160
path: BitFaster.Caching.UnitTests/TestResults/results.trx
161+
- name: Upload test results (6.0 .NET Std)
162+
uses: actions/upload-artifact@v4
163+
if: success() || failure()
164+
with:
165+
name: test-results-linux-std
166+
path: BitFaster.Caching.UnitTests.Std/TestResults/results.trx
148167

149168
coverage:
150169

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0</TargetFrameworks>
5+
<LangVersion>9.0</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
</PackageReference>
13+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
15+
<PackageReference Include="Moq" Version="4.20.69" />
16+
<PackageReference Include="ObjectLayoutInspector" Version="0.1.4" />
17+
<PackageReference Include="xunit" Version="2.8.0" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\BitFaster.Caching\BitFaster.Caching.csproj">
27+
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
28+
</ProjectReference>
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

BitFaster.Caching/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
#endif
66

77
[assembly: InternalsVisibleTo("BitFaster.Caching.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f55849315b02d525d40701eee5d8eba39e6a517644e8af3fa15141eab7058e76be808e36cfee8d7e071b5aac37bd5e45c67971602680f7bfc26d8c9ebca95dd33b4e3f17a4c28b01268ee6b110ad7e2106ab8ffd1c7be3143192527ce5f639395e46ab086518e881706c6ee9eb96f0263aa34e5152cf5aecf657d463fecf62ca")]
8+
[assembly: InternalsVisibleTo("BitFaster.Caching.UnitTests.Std, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f55849315b02d525d40701eee5d8eba39e6a517644e8af3fa15141eab7058e76be808e36cfee8d7e071b5aac37bd5e45c67971602680f7bfc26d8c9ebca95dd33b4e3f17a4c28b01268ee6b110ad7e2106ab8ffd1c7be3143192527ce5f639395e46ab086518e881706c6ee9eb96f0263aa34e5152cf5aecf657d463fecf62ca")]

BitFaster.Caching/Duration.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public static Duration SinceEpoch()
6363
}
6464
else
6565
{
66-
// Warning: not currently covered by unit tests
6766
return new Duration(Stopwatch.GetTimestamp());
6867
}
6968
#endif
@@ -92,7 +91,6 @@ public TimeSpan ToTimeSpan()
9291
}
9392
else
9493
{
95-
// Warning: not currently covered by unit tests
9694
return StopwatchTickConverter.FromTicks(raw);
9795
}
9896
#endif
@@ -122,7 +120,6 @@ public static Duration FromTimeSpan(TimeSpan timeSpan)
122120
}
123121
else
124122
{
125-
// Warning: not currently covered by unit tests
126123
return new Duration(StopwatchTickConverter.ToTicks(timeSpan));
127124
}
128125
#endif

BitFaster.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitFaster.Caching.HitRateAn
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitFaster.Caching.ThroughputAnalysis", "BitFaster.Caching.ThroughputAnalysis\BitFaster.Caching.ThroughputAnalysis.csproj", "{EF9968AF-10B2-4205-9C42-19A594BC98C1}"
2020
EndProject
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitFaster.Caching.UnitTests.Std", "BitFaster.Caching.UnitTests.Std\BitFaster.Caching.UnitTests.Std.csproj", "{5FFDA3B7-3918-4F18-808C-B67E29EAA700}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +46,10 @@ Global
4446
{EF9968AF-10B2-4205-9C42-19A594BC98C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
4547
{EF9968AF-10B2-4205-9C42-19A594BC98C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
4648
{EF9968AF-10B2-4205-9C42-19A594BC98C1}.Release|Any CPU.Build.0 = Release|Any CPU
49+
{5FFDA3B7-3918-4F18-808C-B67E29EAA700}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{5FFDA3B7-3918-4F18-808C-B67E29EAA700}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{5FFDA3B7-3918-4F18-808C-B67E29EAA700}.Release|Any CPU.ActiveCfg = Release|Any CPU
52+
{5FFDA3B7-3918-4F18-808C-B67E29EAA700}.Release|Any CPU.Build.0 = Release|Any CPU
4753
EndGlobalSection
4854
GlobalSection(SolutionProperties) = preSolution
4955
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)