From 280afc42fc2805b378d18d026fdfffe6e3fcb9a7 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 10:14:47 -0700 Subject: [PATCH 1/6] adjust --- BitFaster.Caching/Lru/TlruLongTicksPolicy.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs index 38620529..fa0c66b7 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 long stopwatchAdjustmentFactor = Stopwatch.Frequency / TimeSpan.TicksPerSecond; private readonly long timeToLive; public TLruLongTicksPolicy(TimeSpan timeToLive) { - this.timeToLive = timeToLive.Ticks; + this.timeToLive = timeToLive.Ticks * stopwatchAdjustmentFactor; } [MethodImpl(MethodImplOptions.AggressiveInlining)] From c8d2165eafde0d166352d2f70302506b0d95c3d4 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 11:03:33 -0700 Subject: [PATCH 2/6] use double --- BitFaster.Caching/Lru/TlruLongTicksPolicy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs index fa0c66b7..bfed1884 100644 --- a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs +++ b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs @@ -18,12 +18,12 @@ namespace BitFaster.Caching.Lru public readonly struct TLruLongTicksPolicy : IItemPolicy> { // On some platforms (e.g. MacOS), stopwatch and timespan have different resolution - private static readonly long stopwatchAdjustmentFactor = Stopwatch.Frequency / TimeSpan.TicksPerSecond; + private static readonly double stopwatchAdjustmentFactor = Stopwatch.Frequency / (double)TimeSpan.TicksPerSecond; private readonly long timeToLive; public TLruLongTicksPolicy(TimeSpan timeToLive) { - this.timeToLive = timeToLive.Ticks * stopwatchAdjustmentFactor; + this.timeToLive = (long)(timeToLive.Ticks * stopwatchAdjustmentFactor); } [MethodImpl(MethodImplOptions.AggressiveInlining)] From 2614c1948ff184e23d911a8f7a6f1dab2733f928 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 11:10:12 -0700 Subject: [PATCH 3/6] mac-gate --- .github/workflows/gate.yml | 14 ++------------ .github/workflows/mac-gate.yml | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/mac-gate.yml diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index 5c225db7..8a842e7e 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -9,7 +9,7 @@ on: jobs: build: - runs-on: windows-latest + runs-on: macos-latest steps: - uses: actions/checkout@v2 @@ -22,14 +22,4 @@ jobs: - name: Build run: dotnet build --configuration Release --no-restore - name: Test - run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov - - name: Publish NuGet artifacts - uses: actions/upload-artifact@v1 - with: - name: NuGet package - path: BitFaster.Caching/bin/Release/ - - name: Publish coverage report to coveralls.io - uses: coverallsapp/github-action@master - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - path-to-lcov: BitFaster.Caching.UnitTests/TestResults/coverage.info \ No newline at end of file + run: dotnet test --no-restore --verbosity normal diff --git a/.github/workflows/mac-gate.yml b/.github/workflows/mac-gate.yml new file mode 100644 index 00000000..8a842e7e --- /dev/null +++ b/.github/workflows/mac-gate.yml @@ -0,0 +1,25 @@ +name: Build and Test + +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 From 68c892c40eeb76467a55ea2afa4f8f15c22de2af Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 11:11:15 -0700 Subject: [PATCH 4/6] rem gate changes --- .github/workflows/gate.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index 8a842e7e..5c225db7 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -9,7 +9,7 @@ on: jobs: build: - runs-on: macos-latest + runs-on: windows-latest steps: - uses: actions/checkout@v2 @@ -22,4 +22,14 @@ jobs: - name: Build run: dotnet build --configuration Release --no-restore - name: Test - run: dotnet test --no-restore --verbosity normal + run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov + - name: Publish NuGet artifacts + uses: actions/upload-artifact@v1 + with: + name: NuGet package + path: BitFaster.Caching/bin/Release/ + - name: Publish coverage report to coveralls.io + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + path-to-lcov: BitFaster.Caching.UnitTests/TestResults/coverage.info \ No newline at end of file From 91144f7b5378c8b5efbe8d1b4d7e4cb83bcc78d0 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 11:12:43 -0700 Subject: [PATCH 5/6] name --- .github/workflows/gate.yml | 2 +- .github/workflows/mac-gate.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index 8a842e7e..14a79b2c 100644 --- a/.github/workflows/mac-gate.yml +++ b/.github/workflows/mac-gate.yml @@ -1,4 +1,4 @@ -name: Build and Test +name: Build and Test (macos-latest) on: push: From 41674439eef81c30bd8d9c363ab9fa8e6197abc5 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 22 Apr 2022 11:25:00 -0700 Subject: [PATCH 6/6] mac tests --- .../Lru/TlruLongTicksPolicyTests.cs | 6 +++--- BitFaster.Caching/Lru/TlruLongTicksPolicy.cs | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) 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 bfed1884..af297612 100644 --- a/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs +++ b/BitFaster.Caching/Lru/TlruLongTicksPolicy.cs @@ -23,7 +23,7 @@ namespace BitFaster.Caching.Lru public TLruLongTicksPolicy(TimeSpan timeToLive) { - this.timeToLive = (long)(timeToLive.Ticks * stopwatchAdjustmentFactor); + this.timeToLive = ToTicks(timeToLive); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -96,5 +96,10 @@ public ItemDestination RouteCold(LongTickCountLruItem item) return ItemDestination.Remove; } + + public static long ToTicks(TimeSpan timespan) + { + return (long)(timespan.Ticks * stopwatchAdjustmentFactor); + } } }