|
| 1 | +using FluentAssertions; |
| 2 | +using FluentAssertions.Extensions; |
| 3 | +using BitFaster.Caching.Lru; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | +using System.Diagnostics; |
| 11 | + |
| 12 | +namespace BitFaster.Caching.UnitTests.Lru |
| 13 | +{ |
| 14 | + public class TLruLongTicksPolicyTests |
| 15 | + { |
| 16 | + private readonly TLruLongTicksPolicy<int, int> policy = new TLruLongTicksPolicy<int, int>(TimeSpan.FromSeconds(10)); |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void CreateItemInitializesKeyAndValue() |
| 20 | + { |
| 21 | + var item = this.policy.CreateItem(1, 2); |
| 22 | + |
| 23 | + item.Key.Should().Be(1); |
| 24 | + item.Value.Should().Be(2); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void CreateItemInitializesTimestampToNow() |
| 29 | + { |
| 30 | + var item = this.policy.CreateItem(1, 2); |
| 31 | + |
| 32 | + // seconds = ticks / Stopwatch.Frequency |
| 33 | + ulong epsilon = (ulong)(TimeSpan.FromMilliseconds(20).TotalSeconds * Stopwatch.Frequency); |
| 34 | + item.TickCount.Should().BeCloseTo(Stopwatch.GetTimestamp(), epsilon); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void TouchUpdatesItemWasAccessed() |
| 39 | + { |
| 40 | + var item = this.policy.CreateItem(1, 2); |
| 41 | + item.WasAccessed = false; |
| 42 | + |
| 43 | + this.policy.Touch(item); |
| 44 | + |
| 45 | + item.WasAccessed.Should().BeTrue(); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void WhenItemIsExpiredShouldDiscardIsTrue() |
| 50 | + { |
| 51 | + var item = this.policy.CreateItem(1, 2); |
| 52 | + item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks; |
| 53 | + |
| 54 | + this.policy.ShouldDiscard(item).Should().BeTrue(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void WhenItemIsNotExpiredShouldDiscardIsFalse() |
| 59 | + { |
| 60 | + var item = this.policy.CreateItem(1, 2); |
| 61 | + item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(9).Ticks; |
| 62 | + |
| 63 | + this.policy.ShouldDiscard(item).Should().BeFalse(); |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [InlineData(false, true, ItemDestination.Remove)] |
| 68 | + [InlineData(true, true, ItemDestination.Remove)] |
| 69 | + [InlineData(true, false, ItemDestination.Warm)] |
| 70 | + [InlineData(false, false, ItemDestination.Cold)] |
| 71 | + public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 72 | + { |
| 73 | + var item = CreateItem(wasAccessed, isExpired); |
| 74 | + |
| 75 | + this.policy.RouteHot(item).Should().Be(expectedDestination); |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [InlineData(false, true, ItemDestination.Remove)] |
| 80 | + [InlineData(true, true, ItemDestination.Remove)] |
| 81 | + [InlineData(true, false, ItemDestination.Warm)] |
| 82 | + [InlineData(false, false, ItemDestination.Cold)] |
| 83 | + public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 84 | + { |
| 85 | + var item = CreateItem(wasAccessed, isExpired); |
| 86 | + |
| 87 | + this.policy.RouteWarm(item).Should().Be(expectedDestination); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory] |
| 91 | + [InlineData(false, true, ItemDestination.Remove)] |
| 92 | + [InlineData(true, true, ItemDestination.Remove)] |
| 93 | + [InlineData(true, false, ItemDestination.Warm)] |
| 94 | + [InlineData(false, false, ItemDestination.Remove)] |
| 95 | + public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 96 | + { |
| 97 | + var item = CreateItem(wasAccessed, isExpired); |
| 98 | + |
| 99 | + this.policy.RouteCold(item).Should().Be(expectedDestination); |
| 100 | + } |
| 101 | + |
| 102 | + private LongTickCountLruItem<int, int> CreateItem(bool wasAccessed, bool isExpired) |
| 103 | + { |
| 104 | + var item = this.policy.CreateItem(1, 2); |
| 105 | + |
| 106 | + item.WasAccessed = wasAccessed; |
| 107 | + |
| 108 | + if (isExpired) |
| 109 | + { |
| 110 | + item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks; |
| 111 | + } |
| 112 | + |
| 113 | + return item; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments