|
| 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 | + |
| 11 | +namespace BitFaster.Caching.UnitTests.Lru |
| 12 | +{ |
| 13 | + public class TLruTicksPolicyTests |
| 14 | + { |
| 15 | + private readonly TLruTicksPolicy<int, int> policy = new TLruTicksPolicy<int, int>(TimeSpan.FromSeconds(10)); |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void CreateItemInitializesKeyAndValue() |
| 19 | + { |
| 20 | + var item = this.policy.CreateItem(1, 2); |
| 21 | + |
| 22 | + item.Key.Should().Be(1); |
| 23 | + item.Value.Should().Be(2); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void CreateItemInitializesTimestampToNow() |
| 28 | + { |
| 29 | + var item = this.policy.CreateItem(1, 2); |
| 30 | + |
| 31 | + item.TickCount.Should().BeCloseTo(Environment.TickCount, 20); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void TouchUpdatesItemWasAccessed() |
| 36 | + { |
| 37 | + var item = this.policy.CreateItem(1, 2); |
| 38 | + item.WasAccessed = false; |
| 39 | + |
| 40 | + this.policy.Touch(item); |
| 41 | + |
| 42 | + item.WasAccessed.Should().BeTrue(); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void WhenItemIsExpiredShouldDiscardIsTrue() |
| 47 | + { |
| 48 | + var item = this.policy.CreateItem(1, 2); |
| 49 | + item.TickCount = Environment.TickCount - (int)TimeSpan.FromSeconds(11).ToEnvTicks(); |
| 50 | + |
| 51 | + this.policy.ShouldDiscard(item).Should().BeTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void WhenItemIsNotExpiredShouldDiscardIsFalse() |
| 56 | + { |
| 57 | + var item = this.policy.CreateItem(1, 2); |
| 58 | + item.TickCount = Environment.TickCount - (int)TimeSpan.FromSeconds(9).ToEnvTicks(); |
| 59 | + |
| 60 | + this.policy.ShouldDiscard(item).Should().BeFalse(); |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [InlineData(false, true, ItemDestination.Remove)] |
| 65 | + [InlineData(true, true, ItemDestination.Remove)] |
| 66 | + [InlineData(true, false, ItemDestination.Warm)] |
| 67 | + [InlineData(false, false, ItemDestination.Cold)] |
| 68 | + public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 69 | + { |
| 70 | + var item = CreateItem(wasAccessed, isExpired); |
| 71 | + |
| 72 | + this.policy.RouteHot(item).Should().Be(expectedDestination); |
| 73 | + } |
| 74 | + |
| 75 | + [Theory] |
| 76 | + [InlineData(false, true, ItemDestination.Remove)] |
| 77 | + [InlineData(true, true, ItemDestination.Remove)] |
| 78 | + [InlineData(true, false, ItemDestination.Warm)] |
| 79 | + [InlineData(false, false, ItemDestination.Cold)] |
| 80 | + public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 81 | + { |
| 82 | + var item = CreateItem(wasAccessed, isExpired); |
| 83 | + |
| 84 | + this.policy.RouteWarm(item).Should().Be(expectedDestination); |
| 85 | + } |
| 86 | + |
| 87 | + [Theory] |
| 88 | + [InlineData(false, true, ItemDestination.Remove)] |
| 89 | + [InlineData(true, true, ItemDestination.Remove)] |
| 90 | + [InlineData(true, false, ItemDestination.Warm)] |
| 91 | + [InlineData(false, false, ItemDestination.Remove)] |
| 92 | + public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expectedDestination) |
| 93 | + { |
| 94 | + var item = CreateItem(wasAccessed, isExpired); |
| 95 | + |
| 96 | + this.policy.RouteCold(item).Should().Be(expectedDestination); |
| 97 | + } |
| 98 | + |
| 99 | + private TickCountLruItem<int, int> CreateItem(bool wasAccessed, bool isExpired) |
| 100 | + { |
| 101 | + var item = this.policy.CreateItem(1, 2); |
| 102 | + |
| 103 | + item.WasAccessed = wasAccessed; |
| 104 | + |
| 105 | + if (isExpired) |
| 106 | + { |
| 107 | + item.TickCount = Environment.TickCount - TimeSpan.FromSeconds(11).ToEnvTicks(); |
| 108 | + } |
| 109 | + |
| 110 | + return item; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public static class TimeSpanExtensions |
| 115 | + { |
| 116 | + public static int ToEnvTicks(this TimeSpan ts) |
| 117 | + { |
| 118 | + return (int)ts.TotalMilliseconds; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments