Skip to content

Commit a9af7dc

Browse files
author
Alex Peck
committed
tests
1 parent ca68740 commit a9af7dc

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,51 @@ public void WhenItemIsAddedThenRetrievedHitRatioIsHalf()
118118
lru.GetOrAdd(1, valueFactory.Create);
119119
bool result = lru.TryGet(1, out var value);
120120

121+
#pragma warning disable CS0618 // Type or member is obsolete
121122
lru.HitRatio.Should().Be(0.5);
123+
#pragma warning restore CS0618 // Type or member is obsolete
124+
}
125+
126+
[Fact]
127+
public void MetricsAreEnabled()
128+
{
129+
lru.Metrics.IsEnabled.Should().BeTrue();
130+
}
131+
132+
[Fact]
133+
public void WhenItemIsAddedThenRetrievedMetricHitRatioIsHalf()
134+
{
135+
lru.GetOrAdd(1, valueFactory.Create);
136+
bool result = lru.TryGet(1, out var value);
137+
138+
lru.Metrics.HitRatio.Should().Be(0.5);
139+
}
140+
141+
[Fact]
142+
public void WhenItemIsAddedThenRetrievedMetricHitsIs1()
143+
{
144+
lru.GetOrAdd(1, valueFactory.Create);
145+
bool result = lru.TryGet(1, out var value);
146+
147+
lru.Metrics.Hits.Should().Be(1);
148+
}
149+
150+
[Fact]
151+
public void WhenItemIsAddedThenRetrievedMetricTotalIs2()
152+
{
153+
lru.GetOrAdd(1, valueFactory.Create);
154+
bool result = lru.TryGet(1, out var value);
155+
156+
lru.Metrics.Total.Should().Be(2);
157+
}
158+
159+
[Fact]
160+
public void WhenItemDoesNotExistTryGetIncrementsMiss()
161+
{
162+
lru.GetOrAdd(1, valueFactory.Create);
163+
bool result = lru.TryGet(1, out var value);
164+
165+
lru.Metrics.Misses.Should().Be(1);
122166
}
123167

124168
[Fact]

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ public void WhenItemIsAddedThenRetrievedHitRatioIsHalf()
189189
#pragma warning restore CS0618 // Type or member is obsolete
190190
}
191191

192+
[Fact]
193+
public void WhenItemIsAddedThenRetrievedMetricHitRatioIsHalf()
194+
{
195+
lru.GetOrAdd(1, valueFactory.Create);
196+
bool result = lru.TryGet(1, out var value);
197+
198+
lru.Metrics.HitRatio.Should().Be(0.5);
199+
}
200+
192201
[Fact]
193202
public void WhenKeyIsRequestedItIsCreatedAndCached()
194203
{

BitFaster.Caching.UnitTests/Lru/NoTelemetryPolicyTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ public void HitRatioIsZero()
1717
counter.HitRatio.Should().Be(0);
1818
}
1919

20+
[Fact]
21+
public void TotalIsZero()
22+
{
23+
counter.Total.Should().Be(0);
24+
}
25+
26+
[Fact]
27+
public void HitsIsZero()
28+
{
29+
counter.Hits.Should().Be(0);
30+
}
31+
32+
[Fact]
33+
public void MissesIsZero()
34+
{
35+
counter.Misses.Should().Be(0);
36+
}
37+
38+
[Fact]
39+
public void IsEnabledIsFalse()
40+
{
41+
counter.IsEnabled.Should().BeFalse();
42+
}
43+
2044
[Fact]
2145
public void IncrementHitCountIsNoOp()
2246
{

BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ public class TelemetryPolicyTests
1111
{
1212
private TelemetryPolicy<int, int> telemetryPolicy = default;
1313

14+
[Fact]
15+
public void WhenHitTotalIs1()
16+
{
17+
telemetryPolicy.Total.Should().Be(0);
18+
telemetryPolicy.IncrementHit();
19+
telemetryPolicy.Total.Should().Be(1);
20+
}
21+
22+
[Fact]
23+
public void WhenHitHitsIs1()
24+
{
25+
telemetryPolicy.Hits.Should().Be(0);
26+
telemetryPolicy.IncrementHit();
27+
telemetryPolicy.Hits.Should().Be(1);
28+
}
29+
30+
[Fact]
31+
public void WhenMissMissesIs1()
32+
{
33+
telemetryPolicy.Misses.Should().Be(0);
34+
telemetryPolicy.IncrementMiss();
35+
telemetryPolicy.Misses.Should().Be(1);
36+
}
37+
38+
[Fact]
39+
public void IsEnabledIsTrue()
40+
{
41+
telemetryPolicy.IsEnabled.Should().BeTrue();
42+
}
43+
1444
[Fact]
1545
public void WhenHitCountAndTotalCountAreEqualRatioIs1()
1646
{

BitFaster.Caching.UnitTests/ScopedCacheTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public void WhenItemIsAddedCountIsCorrect()
3838
this.cache.Count.Should().Be(1);
3939
}
4040

41+
[Fact]
42+
public void WhenItemIsAddedThenLookedUpMetricsAreCorrect()
43+
{
44+
this.cache.AddOrUpdate(1, new Disposable());
45+
this.cache.ScopedGetOrAdd(1, k => new Scoped<Disposable>(new Disposable()));
46+
47+
this.cache.Metrics.Misses.Should().Be(0);
48+
this.cache.Metrics.Hits.Should().Be(1);
49+
}
50+
4151
[Fact]
4252
public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem()
4353
{

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> compa
5656
/// <summary>
5757
/// Gets the ratio of hits to misses, where a value of 1 indicates 100% hits.
5858
/// </summary>
59+
[ObsoleteAttribute("This property is obsolete. Use Metrics instead.", false)]
5960
public double HitRatio => this.Metrics.HitRatio;
6061

6162
///<inheritdoc/>

0 commit comments

Comments
 (0)