Skip to content

Commit 768fd7f

Browse files
authored
Fix TLru long ticks policy on macos (#118)
* adjust * use double * mac-gate * rem gate changes * name * mac tests
1 parent e52e4eb commit 768fd7f

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

.github/workflows/gate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Test
1+
name: Build and Test (windows-latest)
22

33
on:
44
push:

.github/workflows/mac-gate.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Build and Test (macos-latest)
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: macos-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Setup .NET Core
17+
uses: actions/setup-dotnet@v1
18+
with:
19+
dotnet-version: 6.0.x
20+
- name: Install dependencies
21+
run: dotnet restore
22+
- name: Build
23+
run: dotnet build --configuration Release --no-restore
24+
- name: Test
25+
run: dotnet test --no-restore --verbosity normal

BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void TouchUpdatesItemWasAccessed()
4949
public void WhenItemIsExpiredShouldDiscardIsTrue()
5050
{
5151
var item = this.policy.CreateItem(1, 2);
52-
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks;
52+
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));
5353

5454
this.policy.ShouldDiscard(item).Should().BeTrue();
5555
}
@@ -58,7 +58,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue()
5858
public void WhenItemIsNotExpiredShouldDiscardIsFalse()
5959
{
6060
var item = this.policy.CreateItem(1, 2);
61-
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(9).Ticks;
61+
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(9));
6262

6363
this.policy.ShouldDiscard(item).Should().BeFalse();
6464
}
@@ -107,7 +107,7 @@ private LongTickCountLruItem<int, int> CreateItem(bool wasAccessed, bool isExpir
107107

108108
if (isExpired)
109109
{
110-
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks;
110+
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));
111111
}
112112

113113
return item;

BitFaster.Caching/Lru/TlruLongTicksPolicy.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ namespace BitFaster.Caching.Lru
1717
/// </remarks>
1818
public readonly struct TLruLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
1919
{
20+
// On some platforms (e.g. MacOS), stopwatch and timespan have different resolution
21+
private static readonly double stopwatchAdjustmentFactor = Stopwatch.Frequency / (double)TimeSpan.TicksPerSecond;
2022
private readonly long timeToLive;
2123

2224
public TLruLongTicksPolicy(TimeSpan timeToLive)
2325
{
24-
this.timeToLive = timeToLive.Ticks;
26+
this.timeToLive = ToTicks(timeToLive);
2527
}
2628

2729
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -94,5 +96,10 @@ public ItemDestination RouteCold(LongTickCountLruItem<K, V> item)
9496

9597
return ItemDestination.Remove;
9698
}
99+
100+
public static long ToTicks(TimeSpan timespan)
101+
{
102+
return (long)(timespan.Ticks * stopwatchAdjustmentFactor);
103+
}
97104
}
98105
}

0 commit comments

Comments
 (0)