diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index 5c225db7..4a729ff8 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -1,4 +1,4 @@ -name: Build and Test +name: Build and Test (windows-latest) on: push: diff --git a/.github/workflows/mac-gate.yml b/.github/workflows/mac-gate.yml new file mode 100644 index 00000000..14a79b2c --- /dev/null +++ b/.github/workflows/mac-gate.yml @@ -0,0 +1,25 @@ +name: Build and Test (macos-latest) + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 6.0.x + - name: Install dependencies + run: dotnet restore + - name: Build + run: dotnet build --configuration Release --no-restore + - name: Test + run: dotnet test --no-restore --verbosity normal diff --git a/BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs index 17ce3d6e..c11b268f 100644 --- a/BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs @@ -49,7 +49,7 @@ public void TouchUpdatesItemWasAccessed() public void WhenItemIsExpiredShouldDiscardIsTrue() { var item = this.policy.CreateItem(1, 2); - item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks; + item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy.ToTicks(TimeSpan.FromSeconds(11)); this.policy.ShouldDiscard(item).Should().BeTrue(); } @@ -58,7 +58,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() public void WhenItemIsNotExpiredShouldDiscardIsFalse() { var item = this.policy.CreateItem(1, 2); - item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(9).Ticks; + item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy.ToTicks(TimeSpan.FromSeconds(9)); this.policy.ShouldDiscard(item).Should().BeFalse(); } @@ -107,7 +107,7 @@ private LongTickCountLruItem CreateItem(bool wasAccessed, bool isExpir if (isExpired) { - item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks; + item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy.ToTicks(TimeSpan.FromSeconds(11)); } return item; diff --git a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs index 38620529..af297612 100644 --- a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs +++ b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs @@ -17,11 +17,13 @@ namespace BitFaster.Caching.Lru /// public readonly struct TLruLongTicksPolicy : IItemPolicy> { + // On some platforms (e.g. MacOS), stopwatch and timespan have different resolution + private static readonly double stopwatchAdjustmentFactor = Stopwatch.Frequency / (double)TimeSpan.TicksPerSecond; private readonly long timeToLive; public TLruLongTicksPolicy(TimeSpan timeToLive) { - this.timeToLive = timeToLive.Ticks; + this.timeToLive = ToTicks(timeToLive); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -94,5 +96,10 @@ public ItemDestination RouteCold(LongTickCountLruItem item) return ItemDestination.Remove; } + + public static long ToTicks(TimeSpan timespan) + { + return (long)(timespan.Ticks * stopwatchAdjustmentFactor); + } } }