From 4d9444ebb0451f4f5f9d1536044ab3f47355e2da Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Sun, 2 Feb 2025 21:52:20 +0000 Subject: [PATCH 1/3] Migrate to Shouldly --- .../BitFaster.Caching.UnitTests.Std.csproj | 8 +- .../DurationTests.cs | 16 +- .../Atomic/AsyncAtomicFactoryTests.cs | 48 +- .../Atomic/AtomicFactoryAsyncCacheTests.cs | 85 +- .../Atomic/AtomicFactoryCacheTests.cs | 84 +- .../AtomicFactoryScopedAsyncCacheTests.cs | 23 +- .../Atomic/AtomicFactoryScopedCacheTests.cs | 29 +- .../Atomic/AtomicFactorySoakTests.cs | 5 +- .../Atomic/AtomicFactoryTests.cs | 59 +- .../ConcurrentDictionaryExtensionTests.cs | 45 +- .../ScopedAsyncAtomicFactorySoakTests.cs | 8 +- .../Atomic/ScopedAsyncAtomicFactoryTests.cs | 58 +- .../Atomic/ScopedAtomicFactorySoakTests.cs | 8 +- .../Atomic/ScopedAtomicFactoryTests.cs | 57 +- .../BitFaster.Caching.UnitTests.csproj | 12 +- BitFaster.Caching.UnitTests/BitOpsTests.cs | 25 +- .../Buffers/MpmcBoundedBufferSoakTests.cs | 6 +- .../Buffers/MpmcBoundedBufferTests.cs | 41 +- .../Buffers/MpscBoundedBufferSoakTests.cs | 6 +- .../Buffers/MpscBoundedBufferTests.cs | 62 +- .../Buffers/StripedMpscBufferTests.cs | 33 +- .../CacheEventProxyBaseTests.cs | 22 +- .../CacheEventsTests.cs | 3 +- .../CacheMetricsTests.cs | 5 +- .../CachePolicyTests.cs | 10 +- BitFaster.Caching.UnitTests/CacheTests.cs | 27 +- .../ConcurrentDictionarySizeTests.cs | 6 +- .../Counters/CounterSoakTests.cs | 4 +- .../Counters/StripedLongAdderTests.cs | 6 +- BitFaster.Caching.UnitTests/Disposable.cs | 4 +- .../DisposableValueFactory.cs | 8 +- BitFaster.Caching.UnitTests/DurationTests.cs | 46 +- .../ExpireAfterAccessTests.cs | 11 +- .../ExpireAfterWriteTests.cs | 11 +- .../Lfu/CmSketchTests.cs | 37 +- .../Lfu/ConcurrentLfuBuilderTests.cs | 120 +- .../Lfu/ConcurrentLfuCoreTests.cs | 62 +- .../Lfu/ConcurrentLfuSoakTests.cs | 24 +- .../Lfu/ConcurrentLfuTests.cs | 172 ++- .../Lfu/ConcurrentTLfuSoakTests.cs | 3 - .../Lfu/ConcurrentTLfuTests.cs | 45 +- .../Lfu/LfuCapacityPartitionTests.cs | 24 +- .../Lfu/LfuInfoTests.cs | 6 +- .../Lfu/LfuNodeListTests.cs | 12 +- .../Lfu/TestScheduler.cs | 4 - .../Lfu/TimerWheelTests.cs | 56 +- .../Lru/AfterAccessPolicyTests.cs | 34 +- .../Lru/CacheExtTests.cs | 20 +- .../Lru/CapacityPartitionExtensionsTests.cs | 14 +- .../Lru/ClassicLruTests.cs | 187 ++- .../Lru/ConcurrentLruAfterAccessTests.cs | 36 +- .../Lru/ConcurrentLruAfterDiscreteTests.cs | 38 +- .../Lru/ConcurrentLruBuilderTests.cs | 1114 ++++++++--------- .../Lru/ConcurrentLruSoakTests.cs | 16 +- .../Lru/ConcurrentLruTests.cs | 330 ++--- .../Lru/ConcurrentTLruTests.cs | 60 +- .../Lru/DiscretePolicyTests.cs | 34 +- .../Lru/DisposableItem.cs | 2 - .../Lru/DisposableValueFactory.cs | 4 +- .../Lru/EqualCapacityPartitionTests.cs | 14 +- .../Lru/FastConcurrentLruTests.cs | 14 +- .../Lru/FastConcurrentTLruTests.cs | 17 +- .../Lru/FavorWarmPartitionTests.cs | 18 +- .../Lru/LruInfoTests.cs | 6 +- .../Lru/LruItemMemoryLayoutDumps.cs | 3 +- .../Lru/LruPolicyTests.cs | 26 +- .../Lru/NoTelemetryPolicyTests.cs | 22 +- .../Lru/StopwatchTickConverterTests.cs | 4 +- .../Lru/TLruTickCount64PolicyTests .cs | 38 +- .../Lru/TelemetryPolicyTests.cs | 52 +- .../Lru/TestCapacityPartition.cs | 7 +- .../Lru/TlruDateTimePolicyTests.cs | 30 +- .../Lru/TlruStopwatchPolicyTests.cs | 38 +- .../Lru/TlruTicksPolicyTests.cs | 34 +- .../Lru/ValueFactory.cs | 5 +- .../ReferenceCountTests.cs | 14 +- .../Retry/DelayedMessageBus.cs | 6 +- .../Retry/RetryFact.cs | 4 +- .../Scheduler/BackgroundSchedulerTests.cs | 20 +- .../Scheduler/ForegroundSchedulerTests.cs | 20 +- .../Scheduler/NullSchedulerTests.cs | 19 +- .../Scheduler/SchedulerTestExtensions.cs | 3 - .../Scheduler/ThreadPoolSchedulerTests.cs | 16 +- .../ScopedAsyncCacheTestBase.cs | 74 +- .../ScopedAsyncCacheTests.cs | 12 +- .../ScopedCacheSoakTests.cs | 4 +- .../ScopedCacheTestBase.cs | 71 +- .../ScopedCacheTests.cs | 14 +- .../ScopedSoakTests.cs | 10 +- BitFaster.Caching.UnitTests/ScopedTests.cs | 22 +- .../SingletonCacheTests.cs | 24 +- BitFaster.Caching.UnitTests/Threaded.cs | 3 - BitFaster.Caching.UnitTests/Timed.cs | 4 +- BitFaster.Caching.UnitTests/TypePropsTests.cs | 4 +- 94 files changed, 1944 insertions(+), 2063 deletions(-) diff --git a/BitFaster.Caching.UnitTests.Std/BitFaster.Caching.UnitTests.Std.csproj b/BitFaster.Caching.UnitTests.Std/BitFaster.Caching.UnitTests.Std.csproj index 1f91474b..1faad828 100644 --- a/BitFaster.Caching.UnitTests.Std/BitFaster.Caching.UnitTests.Std.csproj +++ b/BitFaster.Caching.UnitTests.Std/BitFaster.Caching.UnitTests.Std.csproj @@ -10,16 +10,16 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/BitFaster.Caching.UnitTests.Std/DurationTests.cs b/BitFaster.Caching.UnitTests.Std/DurationTests.cs index 7a58df9f..e67fd624 100644 --- a/BitFaster.Caching.UnitTests.Std/DurationTests.cs +++ b/BitFaster.Caching.UnitTests.Std/DurationTests.cs @@ -2,9 +2,9 @@ using System.Diagnostics; using System.Runtime.InteropServices; using BitFaster.Caching.Lru; -using FluentAssertions; using Xunit; using Xunit.Abstractions; +using Shouldly; namespace BitFaster.Caching.UnitTests.Std { @@ -25,13 +25,11 @@ public void SinceEpoch() // On .NET Standard, only windows uses TickCount64 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Duration.SinceEpoch().raw.Should().BeCloseTo(Duration.GetTickCount64(), 15); + Duration.SinceEpoch().raw.ShouldBe(Duration.GetTickCount64()); } else { - // eps is 1/200 of a second - ulong eps = (ulong)(Stopwatch.Frequency / 200); - Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); + Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp()); } } @@ -41,12 +39,12 @@ public void ToTimeSpan() // On .NET Standard, only windows uses TickCount64 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); + new Duration(1000).ToTimeSpan().ShouldBe(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); } else { // for Stopwatch.GetTimestamp() this is number of ticks - new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); + new Duration(1 * Stopwatch.Frequency).ToTimeSpan().ShouldBe(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); } } @@ -57,12 +55,12 @@ public void FromTimeSpan() if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds); + .ShouldBe((long)TimeSpan.FromSeconds(1).TotalMilliseconds); } else { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be(Stopwatch.Frequency); + .ShouldBe(Stopwatch.Frequency); } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs b/BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs index fdb8a6f8..30774ed2 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -14,8 +14,8 @@ public void DefaultCtorValueIsNotCreated() { var a = new AsyncAtomicFactory(); - a.IsValueCreated.Should().BeFalse(); - a.ValueIfCreated.Should().Be(0); + a.IsValueCreated.ShouldBeFalse(); + a.ValueIfCreated.ShouldBe(0); } [Fact] @@ -23,28 +23,28 @@ public void WhenValuePassedToCtorValueIsStored() { var a = new AsyncAtomicFactory(1); - a.ValueIfCreated.Should().Be(1); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(1); + a.IsValueCreated.ShouldBeTrue(); } [Fact] public async Task WhenValueCreatedValueReturned() { var a = new AsyncAtomicFactory(); - (await a.GetValueAsync(1, k => Task.FromResult(2))).Should().Be(2); + (await a.GetValueAsync(1, k => Task.FromResult(2))).ShouldBe(2); - a.ValueIfCreated.Should().Be(2); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(2); + a.IsValueCreated.ShouldBeTrue(); } [Fact] public async Task WhenValueCreatedWithArgValueReturned() { var a = new AsyncAtomicFactory(); - (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).Should().Be(8); + (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).ShouldBe(8); - a.ValueIfCreated.Should().Be(8); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(8); + a.IsValueCreated.ShouldBeTrue(); } [Fact] @@ -52,7 +52,7 @@ public async Task WhenValueCreatedGetValueReturnsOriginalValue() { var a = new AsyncAtomicFactory(); await a.GetValueAsync(1, k => Task.FromResult(2)); - (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(2); + (await a.GetValueAsync(1, k => Task.FromResult(3))).ShouldBe(2); } [Fact] @@ -60,7 +60,7 @@ public async Task WhenValueCreatedArgGetValueReturnsOriginalValue() { var a = new AsyncAtomicFactory(); await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7); - (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).Should().Be(8); + (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).ShouldBe(8); } [Fact] @@ -70,9 +70,9 @@ public async Task WhenValueCreateThrowsValueIsNotStored() Func getOrAdd = async () => { await a.GetValueAsync(1, k => throw new ArithmeticException()); }; - await getOrAdd.Should().ThrowAsync(); + var ex = await getOrAdd.ShouldThrowAsync();; - (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(3); + (await a.GetValueAsync(1, k => Task.FromResult(3))).ShouldBe(3); } [Fact] @@ -108,10 +108,10 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner() await enter.Task; resume.SetResult(true); - (await first).Should().Be(result); - (await second).Should().Be(result); + (await first).ShouldBe(result); + (await second).ShouldBe(result); - winnerCount.Should().Be(1); + winnerCount.ShouldBe(1); } [Fact] @@ -119,7 +119,7 @@ public void WhenValueNotCreatedHashCodeIsZero() { new AsyncAtomicFactory() .GetHashCode() - .Should().Be(0); + .ShouldBe(0); } [Fact] @@ -127,7 +127,7 @@ public void WhenValueCreatedHashCodeIsValueHashCode() { new AsyncAtomicFactory(1) .GetHashCode() - .Should().Be(1); + .ShouldBe(1); } [Fact] @@ -136,7 +136,7 @@ public void WhenValueNotCreatedEqualsFalse() var a = new AsyncAtomicFactory(); var b = new AsyncAtomicFactory(); - a.Equals(b).Should().BeFalse(); + a.Equals(b).ShouldBeFalse(); } [Fact] @@ -145,7 +145,7 @@ public void WhenOtherValueNotCreatedEqualsFalse() var a = new AsyncAtomicFactory(1); var b = new AsyncAtomicFactory(); - a.Equals(b).Should().BeFalse(); + a.Equals(b).ShouldBeFalse(); } [Fact] @@ -153,7 +153,7 @@ public void WhenArgNullEqualsFalse() { new AsyncAtomicFactory(1) .Equals(null) - .Should().BeFalse(); + .ShouldBeFalse(); } [Fact] @@ -163,7 +163,7 @@ public void WhenArgObjectValuesAreSameEqualsTrue() new AsyncAtomicFactory(1) .Equals(other) - .Should().BeTrue(); + .ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs index 7459de31..b2a52b4c 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs @@ -2,11 +2,10 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using BitFaster.Caching.Lru; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; using Moq; @@ -31,23 +30,23 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new AtomicFactoryAsyncCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void WhenCreatedCapacityPropertyWrapsInnerCache() { - this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity); + this.cache.Policy.Eviction.Value.Capacity.ShouldBe(capacity); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); this.cache.AddOrUpdate(2, 2); - this.cache.Count.Should().Be(1); + this.cache.Count.ShouldBe(1); } [Fact] @@ -56,8 +55,8 @@ public async Task WhenItemIsAddedThenLookedUpMetricsAreCorrect() this.cache.AddOrUpdate(1, 1); await this.cache.GetOrAddAsync(1, k => Task.FromResult(k)); - this.cache.Metrics.Value.Misses.Should().Be(0); - this.cache.Metrics.Value.Hits.Should().Be(1); + this.cache.Metrics.Value.Misses.ShouldBe(0); + this.cache.Metrics.Value.Hits.ShouldBe(1); } [Fact] @@ -65,8 +64,8 @@ public async Task WhenItemIsAddedWithArgValueIsCorrect() { await this.cache.GetOrAddAsync(1, (k, a) => Task.FromResult(k + a), 2); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(3); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(3); } [Fact] @@ -77,7 +76,7 @@ public void WhenNoInnerEventsNoOuterEvents() var cache = new AtomicFactoryAsyncCache(inner.Object); - cache.Events.HasValue.Should().BeFalse(); + cache.Events.HasValue.ShouldBeFalse(); } [Fact] @@ -88,7 +87,7 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(1); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } // backcompat: remove conditional compile @@ -101,9 +100,9 @@ public void WhenUpdatedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, 2); this.cache.AddOrUpdate(1, 3); - this.updatedItems.First().Key.Should().Be(1); - this.updatedItems.First().OldValue.Should().Be(2); - this.updatedItems.First().NewValue.Should().Be(3); + this.updatedItems.First().Key.ShouldBe(1); + this.updatedItems.First().OldValue.ShouldBe(2); + this.updatedItems.First().NewValue.ShouldBe(3); } #endif @@ -112,8 +111,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() { this.cache.AddOrUpdate(1, 1); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(1); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -122,8 +121,8 @@ public void WhenKeyExistsAddOrUpdateUpdatesExistingItem() this.cache.AddOrUpdate(1, 1); this.cache.AddOrUpdate(1, 2); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } [Fact] @@ -133,13 +132,13 @@ public void WhenClearedItemsAreRemoved() this.cache.Clear(); - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); } [Fact] public void WhenItemDoesNotExistTryGetReturnsFalse() { - this.cache.TryGet(1, out var value).Should().BeFalse(); + this.cache.TryGet(1, out var value).ShouldBeFalse(); } [Fact] @@ -147,8 +146,8 @@ public async Task WhenKeyDoesNotExistGetOrAddAsyncAddsValue() { await this.cache.GetOrAddAsync(1, k => Task.FromResult(k)); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(1); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -160,26 +159,26 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue() this.cache.Policy.Eviction.Value.Trim(1); - this.cache.TryGet(0, out var value).Should().BeFalse(); + this.cache.TryGet(0, out var value).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { - this.cache.TryRemove(1).Should().BeFalse(); + this.cache.TryRemove(1).ShouldBeFalse(); } [Fact] public void WhenKeyExistsTryRemoveReturnsTrue() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(1).Should().BeTrue(); + this.cache.TryRemove(1).ShouldBeTrue(); } [Fact] public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { - this.cache.TryUpdate(1, 1).Should().BeFalse(); + this.cache.TryUpdate(1, 1).ShouldBeFalse(); } [Fact] @@ -187,38 +186,38 @@ public void WhenKeyExistsTryUpdateReturnsTrue() { this.cache.AddOrUpdate(1, 1); - this.cache.TryUpdate(1, 2).Should().BeTrue(); + this.cache.TryUpdate(1, 2).ShouldBeTrue(); this.cache.TryGet(1, out var value); - value.Should().Be(2); + value.ShouldBe(2); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); - cache.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + cache.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] public void WhenItemsAddedGenericEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); - cache.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); + cache.ShouldBe(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); } [Fact] public void WhenItemsAddedEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); var enumerable = (IEnumerable)cache; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); } [Fact] @@ -230,7 +229,7 @@ public async Task WhenFactoryThrowsEmptyValueIsNotCounted() } catch { } - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); } [Fact] @@ -243,7 +242,7 @@ public async Task WhenFactoryThrowsEmptyValueIsNotEnumerable() catch { } // IEnumerable.Count() instead of Count property - cache.Count().Should().Be(0); + cache.Count().ShouldBe(0); } [Fact] @@ -255,7 +254,7 @@ public async Task WhenFactoryThrowsEmptyKeyIsNotEnumerable() } catch { } - cache.Keys.Count().Should().Be(0); + cache.Keys.Count().ShouldBe(0); } // backcompat: remove conditional compile @@ -266,7 +265,7 @@ public void WhenRemovedValueIsReturned() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(1, out var value); - value.Should().Be(1); + value.ShouldBe(1); } [Fact] @@ -275,21 +274,21 @@ public void WhenNotRemovedValueIsDefault() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(2, out var value); - value.Should().Be(0); + value.ShouldBe(0); } [Fact] public void WhenRemoveKeyValueAndValueDoesntMatchDontRemove() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(new KeyValuePair(1, 2)).Should().BeFalse(); + this.cache.TryRemove(new KeyValuePair(1, 2)).ShouldBeFalse(); } [Fact] public void WhenRemoveKeyValueAndValueDoesMatchThenRemove() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); + this.cache.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); } [Fact] @@ -299,7 +298,7 @@ public void WhenRemoveKeyValueAndValueIsNotCreatedDoesNotRemove() this.innerCache.AddOrUpdate(1, new AsyncAtomicFactory()); // try to remove with the default value (0) - this.cache.TryRemove(new KeyValuePair(1, 0)).Should().BeFalse(); + this.cache.TryRemove(new KeyValuePair(1, 0)).ShouldBeFalse(); } #endif diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs index 0c8bd66c..8566bf3c 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs @@ -4,7 +4,7 @@ using System.Linq; using BitFaster.Caching.Lru; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; using Moq; @@ -30,23 +30,23 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new AtomicFactoryCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void WhenCreatedCapacityPropertyWrapsInnerCache() { - this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity); + this.cache.Policy.Eviction.Value.Capacity.ShouldBe(capacity); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); this.cache.AddOrUpdate(2, 2); - this.cache.Count.Should().Be(1); + this.cache.Count.ShouldBe(1); } [Fact] @@ -55,8 +55,8 @@ public void WhenItemIsAddedThenLookedUpMetricsAreCorrect() this.cache.AddOrUpdate(1, 1); this.cache.GetOrAdd(1, k => k); - this.cache.Metrics.Value.Misses.Should().Be(0); - this.cache.Metrics.Value.Hits.Should().Be(1); + this.cache.Metrics.Value.Misses.ShouldBe(0); + this.cache.Metrics.Value.Hits.ShouldBe(1); } [Fact] @@ -64,8 +64,8 @@ public void WhenItemIsAddedWithArgValueIsCorrect() { this.cache.GetOrAdd(1, (k, a) => k + a, 2); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(3); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(3); } [Fact] @@ -76,7 +76,7 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(1); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } // backcompat: remove conditional compile @@ -87,7 +87,7 @@ public void WhenRemovedValueIsReturned() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(1, out var value); - value.Should().Be(1); + value.ShouldBe(1); } [Fact] @@ -96,21 +96,21 @@ public void WhenNotRemovedValueIsDefault() this.cache.AddOrUpdate(1, 1); this.cache.TryRemove(2, out var value); - value.Should().Be(0); + value.ShouldBe(0); } [Fact] public void WhenRemoveKeyValueAndValueDoesntMatchDontRemove() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(new KeyValuePair(1, 2)).Should().BeFalse(); + this.cache.TryRemove(new KeyValuePair(1, 2)).ShouldBeFalse(); } [Fact] public void WhenRemoveKeyValueAndValueDoesMatchThenRemove() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); + this.cache.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); } [Fact] @@ -120,7 +120,7 @@ public void WhenRemoveKeyValueAndValueIsNotCreatedDoesNotRemove() this.innerCache.AddOrUpdate(1, new AtomicFactory()); // try to remove with the default value (0) - this.cache.TryRemove(new KeyValuePair(1, 0)).Should().BeFalse(); + this.cache.TryRemove(new KeyValuePair(1, 0)).ShouldBeFalse(); } [Fact] @@ -131,9 +131,9 @@ public void WhenUpdatedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, 2); this.cache.AddOrUpdate(1, 3); - this.updatedItems.First().Key.Should().Be(1); - this.updatedItems.First().OldValue.Should().Be(2); - this.updatedItems.First().NewValue.Should().Be(3); + this.updatedItems.First().Key.ShouldBe(1); + this.updatedItems.First().OldValue.ShouldBe(2); + this.updatedItems.First().NewValue.ShouldBe(3); } #endif [Fact] @@ -144,7 +144,7 @@ public void WhenNoInnerEventsNoOuterEvents() var cache = new AtomicFactoryCache(inner.Object); - cache.Events.HasValue.Should().BeFalse(); + cache.Events.HasValue.ShouldBeFalse(); } [Fact] @@ -152,8 +152,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() { this.cache.AddOrUpdate(1, 1); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(1); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -162,8 +162,8 @@ public void WhenKeyExistsAddOrUpdateUpdatesExistingItem() this.cache.AddOrUpdate(1, 1); this.cache.AddOrUpdate(1, 2); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } [Fact] @@ -173,13 +173,13 @@ public void WhenClearedItemsAreRemoved() this.cache.Clear(); - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); } [Fact] public void WhenItemDoesNotExistTryGetReturnsFalse() { - this.cache.TryGet(1, out var value).Should().BeFalse(); + this.cache.TryGet(1, out var value).ShouldBeFalse(); } [Fact] @@ -187,8 +187,8 @@ public void WhenKeyDoesNotExistGetOrAddAddsValue() { this.cache.GetOrAdd(1, k => k); - this.cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(1); + this.cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -200,26 +200,26 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue() this.cache.Policy.Eviction.Value.Trim(1); - this.cache.TryGet(0, out var value).Should().BeFalse(); + this.cache.TryGet(0, out var value).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { - this.cache.TryRemove(1).Should().BeFalse(); + this.cache.TryRemove(1).ShouldBeFalse(); } [Fact] public void WhenKeyExistsTryRemoveReturnsTrue() { this.cache.AddOrUpdate(1, 1); - this.cache.TryRemove(1).Should().BeTrue(); + this.cache.TryRemove(1).ShouldBeTrue(); } [Fact] public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { - this.cache.TryUpdate(1, 1).Should().BeFalse(); + this.cache.TryUpdate(1, 1).ShouldBeFalse(); } [Fact] @@ -227,38 +227,38 @@ public void WhenKeyExistsTryUpdateReturnsTrue() { this.cache.AddOrUpdate(1, 1); - this.cache.TryUpdate(1, 2).Should().BeTrue(); + this.cache.TryUpdate(1, 2).ShouldBeTrue(); this.cache.TryGet(1, out var value); - value.Should().Be(2); + value.ShouldBe(2); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); - cache.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + cache.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] public void WhenItemsAddedGenericEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); - cache.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); + cache.ShouldBe(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); } [Fact] public void WhenItemsAddedEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, 1); cache.AddOrUpdate(2, 2); var enumerable = (IEnumerable)cache; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); } [Fact] @@ -270,7 +270,7 @@ public void WhenFactoryThrowsEmptyValueIsNotCounted() } catch { } - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); } [Fact] @@ -283,7 +283,7 @@ public void WhenFactoryThrowsEmptyValueIsNotEnumerable() catch { } // IEnumerable.Count() instead of Count property - cache.Count().Should().Be(0); + cache.Count().ShouldBe(0); } [Fact] @@ -295,7 +295,7 @@ public void WhenFactoryThrowsEmptyKeyIsNotEnumerable() } catch { } - cache.Keys.Count().Should().Be(0); + cache.Keys.Count().ShouldBe(0); } private void OnItemRemoved(object sender, ItemRemovedEventArgs e) diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs index e9244c6c..757e700d 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using BitFaster.Caching.Lru; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; using Moq; @@ -23,7 +20,7 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new AtomicFactoryScopedAsyncCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -35,7 +32,7 @@ public async Task WhenScopeIsDisposedTryGetReturnsFalse() scope.Dispose(); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -43,7 +40,7 @@ public async Task WhenKeyDoesNotExistGetOrAddAsyncAddsValue() { await this.cache.ScopedGetOrAddAsync(1, k => Task.FromResult(new Scoped(new Disposable()))); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); } [Fact] @@ -54,7 +51,7 @@ public async Task GetOrAddAsyncDisposedScopeThrows() Func getOrAdd = async () => { await this.cache.ScopedGetOrAddAsync(1, k => Task.FromResult(scope)); }; - await getOrAdd.Should().ThrowAsync(); + var ex = await getOrAdd.ShouldThrowAsync();; } [Fact] @@ -65,7 +62,7 @@ public void WhenNoInnerEventsNoOuterEvents() var cache = new AtomicFactoryScopedAsyncCache(inner.Object); - cache.Events.HasValue.Should().BeFalse(); + cache.Events.HasValue.ShouldBeFalse(); } // Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal. @@ -77,11 +74,11 @@ public void WhenEntryIsUpdatedOldEntryIsDisposed() this.cache.AddOrUpdate(1, disposable1); - this.cache.TryUpdate(1, disposable2).Should().BeTrue(); - disposable1.IsDisposed.Should().BeTrue(); + this.cache.TryUpdate(1, disposable2).ShouldBeTrue(); + disposable1.IsDisposed.ShouldBeTrue(); - this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); - disposable2.IsDisposed.Should().BeTrue(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeTrue(); + disposable2.IsDisposed.ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs index 58254424..dcf2d1c4 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lru; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; using Moq; @@ -23,7 +20,7 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new AtomicFactoryScopedCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -35,7 +32,7 @@ public void WhenScopeIsDisposedTryGetReturnsFalse() scope.Dispose(); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -43,7 +40,7 @@ public void WhenKeyDoesNotExistGetOrAddAddsValue() { this.cache.ScopedGetOrAdd(1, k => new Scoped(new Disposable())); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); } [Fact] @@ -54,7 +51,7 @@ public void GetOrAddDisposedScopeThrows() Action getOrAdd = () => { this.cache.ScopedGetOrAdd(1, k => scope); }; - getOrAdd.Should().Throw(); + getOrAdd.ShouldThrow(); } [Fact] @@ -65,7 +62,7 @@ public void WhenNoInnerEventsNoOuterEvents() var cache = new AtomicFactoryScopedCache(inner.Object); - cache.Events.HasValue.Should().BeFalse(); + cache.Events.HasValue.ShouldBeFalse(); } // Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal. @@ -77,11 +74,11 @@ public void WhenEntryIsUpdatedOldEntryIsDisposed() this.cache.AddOrUpdate(1, disposable1); - this.cache.TryUpdate(1, disposable2).Should().BeTrue(); - disposable1.IsDisposed.Should().BeTrue(); + this.cache.TryUpdate(1, disposable2).ShouldBeTrue(); + disposable1.IsDisposed.ShouldBeTrue(); - this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); - disposable2.IsDisposed.Should().BeTrue(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeTrue(); + disposable2.IsDisposed.ShouldBeTrue(); } [Fact] @@ -93,7 +90,7 @@ public void WhenFactoryThrowsEmptyValueIsNotCounted() } catch { } - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); } [Fact] @@ -106,7 +103,7 @@ public void WhenFactoryThrowsEmptyValueIsNotEnumerable() catch { } // IEnumerable.Count() instead of Count property - cache.Count().Should().Be(0); + cache.Count().ShouldBe(0); } [Fact] @@ -118,7 +115,7 @@ public void WhenFactoryThrowsEmptyKeyIsNotEnumerable() } catch { } - cache.Keys.Count().Should().Be(0); + cache.Keys.Count().ShouldBe(0); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs index b4ab2708..ab590429 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs @@ -2,8 +2,7 @@ using System.Linq; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -27,7 +26,7 @@ await Threaded.Run(threads, (r) => } }); - counters.Sum(x => x).Should().Be(items); + counters.Sum(x => x).ShouldBe(items); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryTests.cs index dfe9a32c..1925edb1 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryTests.cs @@ -1,9 +1,8 @@ - -using System; +using System; using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -15,8 +14,8 @@ public void DefaultCtorValueIsNotCreated() { var a = new AtomicFactory(); - a.IsValueCreated.Should().BeFalse(); - a.ValueIfCreated.Should().Be(0); + a.IsValueCreated.ShouldBeFalse(); + a.ValueIfCreated.ShouldBe(0); } [Fact] @@ -24,28 +23,28 @@ public void WhenValuePassedToCtorValueIsStored() { var a = new AtomicFactory(1); - a.ValueIfCreated.Should().Be(1); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(1); + a.IsValueCreated.ShouldBeTrue(); } [Fact] public void WhenValueCreatedValueReturned() { var a = new AtomicFactory(); - a.GetValue(1, k => 2).Should().Be(2); + a.GetValue(1, k => 2).ShouldBe(2); - a.ValueIfCreated.Should().Be(2); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(2); + a.IsValueCreated.ShouldBeTrue(); } [Fact] public void WhenValueCreatedWithArgValueReturned() { var a = new AtomicFactory(); - a.GetValue(1, (k, a) => k + a, 7).Should().Be(8); + a.GetValue(1, (k, a) => k + a, 7).ShouldBe(8); - a.ValueIfCreated.Should().Be(8); - a.IsValueCreated.Should().BeTrue(); + a.ValueIfCreated.ShouldBe(8); + a.IsValueCreated.ShouldBeTrue(); } [Fact] @@ -53,7 +52,7 @@ public void WhenValueCreatedGetValueReturnsOriginalValue() { var a = new AtomicFactory(); a.GetValue(1, k => 2); - a.GetValue(1, k => 3).Should().Be(2); + a.GetValue(1, k => 3).ShouldBe(2); } [Fact] @@ -61,7 +60,7 @@ public void WhenValueCreatedArgGetValueReturnsOriginalValue() { var a = new AtomicFactory(); a.GetValue(1, (k, a) => k + a, 7); - a.GetValue(1, (k, a) => k + a, 9).Should().Be(8); + a.GetValue(1, (k, a) => k + a, 9).ShouldBe(8); } [Fact] @@ -69,7 +68,7 @@ public void WhenValueNotCreatedHashCodeIsZero() { new AtomicFactory() .GetHashCode() - .Should().Be(0); + .ShouldBe(0); } [Fact] @@ -77,7 +76,7 @@ public void WhenValueCreatedHashCodeIsValueHashCode() { new AtomicFactory(1) .GetHashCode() - .Should().Be(1); + .ShouldBe(1); } [Fact] @@ -86,7 +85,7 @@ public void WhenValueNotCreatedEqualsFalse() var a = new AtomicFactory(); var b = new AtomicFactory(); - a.Equals(b).Should().BeFalse(); + a.Equals(b).ShouldBeFalse(); } [Fact] @@ -95,7 +94,7 @@ public void WhenOtherValueNotCreatedEqualsFalse() var a = new AtomicFactory(1); var b = new AtomicFactory(); - a.Equals(b).Should().BeFalse(); + a.Equals(b).ShouldBeFalse(); } [Fact] @@ -103,7 +102,7 @@ public void WhenArgNullEqualsFalse() { new AtomicFactory(1) .Equals(null) - .Should().BeFalse(); + .ShouldBeFalse(); } [Fact] @@ -113,7 +112,7 @@ public void WhenArgObjectValuesAreSameEqualsTrue() new AtomicFactory(1) .Equals(other) - .Should().BeTrue(); + .ShouldBeTrue(); } [Fact] @@ -161,10 +160,10 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner() factory.WaitOne(); resume.Set(); - (await first).Should().Be(result); - (await second).Should().Be(result); + (await first).ShouldBe(result); + (await second).ShouldBe(result); - winnerCount.Should().Be(1); + winnerCount.ShouldBe(1); } [Fact] @@ -219,8 +218,8 @@ public async Task WhenCallersRunConcurrentlyAndFailExceptionIsPropogated() Func act1 = () => first; Func act2 = () => second; - await act1.Should().ThrowAsync(); - await act2.Should().ThrowAsync(); + await act1.ShouldThrowAsync();; + await act2.ShouldThrowAsync();; if (throwCount == 1) { @@ -228,7 +227,7 @@ public async Task WhenCallersRunConcurrentlyAndFailExceptionIsPropogated() } } - timesContended.Should().BeGreaterThan(0); + timesContended.ShouldBeGreaterThan(0); } [Fact] @@ -271,11 +270,11 @@ public async Task WhenCallersRunConcurrentlyAndFailNewCallerStartsClean() Func act1 = () => first; Func act2 = () => second; - await act1.Should().ThrowAsync(); - await act2.Should().ThrowAsync(); + await act1.ShouldThrowAsync();; + await act2.ShouldThrowAsync();; // verify exception is no longer cached - atomicFactory.GetValue(1, k => k).Should().Be(1); + atomicFactory.GetValue(1, k => k).ShouldBe(1); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/ConcurrentDictionaryExtensionTests.cs b/BitFaster.Caching.UnitTests/Atomic/ConcurrentDictionaryExtensionTests.cs index 02473a91..59e38b55 100644 --- a/BitFaster.Caching.UnitTests/Atomic/ConcurrentDictionaryExtensionTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/ConcurrentDictionaryExtensionTests.cs @@ -1,9 +1,8 @@ - +using System.Collections.Generic; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -18,8 +17,8 @@ public void WhenItemIsAddedItCanBeRetrieved() { dictionary.GetOrAdd(1, k => k); - dictionary.TryGetValue(1, out int value).Should().BeTrue(); - value.Should().Be(1); + dictionary.TryGetValue(1, out int value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -27,8 +26,8 @@ public async Task WhenItemIsAddedAsyncItCanBeRetrieved() { await dictionaryAsync.GetOrAddAsync(1, k => Task.FromResult(k)); - dictionaryAsync.TryGetValue(1, out int value).Should().BeTrue(); - value.Should().Be(1); + dictionaryAsync.TryGetValue(1, out int value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -36,8 +35,8 @@ public void WhenItemIsAddedWithArgItCanBeRetrieved() { dictionary.GetOrAdd(1, (k,a) => k + a, 2); - dictionary.TryGetValue(1, out int value).Should().BeTrue(); - value.Should().Be(3); + dictionary.TryGetValue(1, out int value).ShouldBeTrue(); + value.ShouldBe(3); } [Fact] @@ -45,20 +44,20 @@ public async Task WhenItemIsAddedWithArgAsyncItCanBeRetrieved() { await dictionaryAsync.GetOrAddAsync(1, (k, a) => Task.FromResult(k + a), 2); - dictionaryAsync.TryGetValue(1, out int value).Should().BeTrue(); - value.Should().Be(3); + dictionaryAsync.TryGetValue(1, out int value).ShouldBeTrue(); + value.ShouldBe(3); } [Fact] public void WhenKeyDoesNotExistTryGetReturnsFalse() { - dictionary.TryGetValue(1, out int _).Should().BeFalse(); + dictionary.TryGetValue(1, out int _).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistAsyncTryGetReturnsFalse() { - dictionaryAsync.TryGetValue(1, out int _).Should().BeFalse(); + dictionaryAsync.TryGetValue(1, out int _).ShouldBeFalse(); } [Fact] @@ -66,8 +65,8 @@ public void WhenItemIsAddedItCanBeRemovedByKey() { dictionary.GetOrAdd(1, k => k); - dictionary.TryRemove(1, out int value).Should().BeTrue(); - value.Should().Be(1); + dictionary.TryRemove(1, out int value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -75,8 +74,8 @@ public async Task WhenItemIsAddedAsyncItCanBeRemovedByKey() { await dictionaryAsync.GetOrAddAsync(1, k => Task.FromResult(k)); - dictionaryAsync.TryRemove(1, out int value).Should().BeTrue(); - value.Should().Be(1); + dictionaryAsync.TryRemove(1, out int value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -84,8 +83,8 @@ public void WhenItemIsAddedItCanBeRemovedByKvp() { dictionary.GetOrAdd(1, k => k); - dictionary.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); - dictionary.TryGetValue(1, out _).Should().BeFalse(); + dictionary.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); + dictionary.TryGetValue(1, out _).ShouldBeFalse(); } [Fact] @@ -93,20 +92,20 @@ public async Task WhenItemIsAddedAsyncItCanBeRemovedByKvp() { await dictionaryAsync.GetOrAddAsync(1, k => Task.FromResult(k)); - dictionaryAsync.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); - dictionaryAsync.TryGetValue(1, out _).Should().BeFalse(); + dictionaryAsync.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); + dictionaryAsync.TryGetValue(1, out _).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { - dictionary.TryRemove(1, out int _).Should().BeFalse(); + dictionary.TryRemove(1, out int _).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistAsyncTryRemoveReturnsFalse() { - dictionaryAsync.TryRemove(1, out int _).Should().BeFalse(); + dictionaryAsync.TryRemove(1, out int _).ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactorySoakTests.cs b/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactorySoakTests.cs index f8c94e9d..4d0a9c0c 100644 --- a/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactorySoakTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactorySoakTests.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -32,7 +32,7 @@ await Threaded.RunAsync(threads, async (r) => { using (lifetime) { - lifetime.Value.IsDisposed.Should().BeFalse(); + lifetime.Value.IsDisposed.ShouldBeFalse(); } break; @@ -41,7 +41,7 @@ await Threaded.RunAsync(threads, async (r) => } }); - counters.Sum(x => x).Should().Be(items); + counters.Sum(x => x).ShouldBe(items); } [Fact] @@ -67,7 +67,7 @@ await Threaded.RunAsync(threads, async (r) => { using (lifetime) { - lifetime.Value.IsDisposed.Should().BeFalse(); + lifetime.Value.IsDisposed.ShouldBeFalse(); } break; diff --git a/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactoryTests.cs b/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactoryTests.cs index bc941309..559eff1f 100644 --- a/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactoryTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactoryTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -14,7 +14,7 @@ public void WhenScopeIsNotCreatedScopeIfCreatedReturnsNull() { var atomicFactory = new ScopedAsyncAtomicFactory(); - atomicFactory.ScopeIfCreated.Should().BeNull(); + atomicFactory.ScopeIfCreated.ShouldBeNull(); } [Fact] @@ -23,16 +23,16 @@ public void WhenScopeIsCreatedScopeIfCreatedReturnsScope() var expectedDisposable = new Disposable(); var atomicFactory = new ScopedAsyncAtomicFactory(expectedDisposable); - atomicFactory.ScopeIfCreated.Should().NotBeNull(); - atomicFactory.ScopeIfCreated.TryCreateLifetime(out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + atomicFactory.ScopeIfCreated.ShouldNotBeNull(); + atomicFactory.ScopeIfCreated.TryCreateLifetime(out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] public void WhenNotInitTryCreateReturnsFalse() { var sa = new ScopedAsyncAtomicFactory(); - sa.TryCreateLifetime(out var l).Should().BeFalse(); + sa.TryCreateLifetime(out var l).ShouldBeFalse(); } [Fact] @@ -41,8 +41,8 @@ public void WhenCreatedTryCreateLifetimeReturnsScope() var expectedDisposable = new Disposable(); var sa = new ScopedAsyncAtomicFactory(expectedDisposable); - sa.TryCreateLifetime(out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + sa.TryCreateLifetime(out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] @@ -55,8 +55,8 @@ public async Task WhenCreateFromValueLifetimeContainsValue() return Task.FromResult(new Scoped(new IntHolder() { actualNumber = 2 })); }); - result.r.Should().BeTrue(); - result.l.Value.actualNumber.Should().Be(1); + result.r.ShouldBeTrue(); + result.l.Value.actualNumber.ShouldBe(1); } [Fact] @@ -69,8 +69,8 @@ public async Task WhenCreateFromFactoryLifetimeContainsValue() return Task.FromResult(new Scoped(new IntHolder() { actualNumber = 2 })); }); - result.r.Should().BeTrue(); - result.l.Value.actualNumber.Should().Be(2); + result.r.ShouldBeTrue(); + result.l.Value.actualNumber.ShouldBe(2); } [Fact] @@ -81,8 +81,8 @@ public async Task WhenCreateFromFactoryArgLifetimeContainsValue() (bool r, Lifetime l) result = await atomicFactory.TryCreateLifetimeAsync(1, factory); - result.r.Should().BeTrue(); - result.l.Value.actualNumber.Should().Be(8); + result.r.ShouldBeTrue(); + result.l.Value.actualNumber.ShouldBe(8); } [Fact] @@ -96,8 +96,8 @@ public async Task WhenScopeIsDisposedTryCreateReturnsFalse() return Task.FromResult(new Scoped(new IntHolder() { actualNumber = 2 })); }); - result.r.Should().BeFalse(); - result.l.Should().BeNull(); + result.r.ShouldBeFalse(); + result.l.ShouldBeNull(); } [Fact] @@ -108,7 +108,7 @@ public void WhenValueIsCreatedDisposeDisposesValue() atomicFactory.Dispose(); - holder.disposed.Should().BeTrue(); + holder.disposed.ShouldBeTrue(); } [Fact] @@ -147,13 +147,13 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner() var result1 = await first; var result2 = await second; - result1.r.Should().BeTrue(); - result2.r.Should().BeTrue(); + result1.r.ShouldBeTrue(); + result2.r.ShouldBeTrue(); - result1.l.Value.actualNumber.Should().Be(winningNumber); - result2.l.Value.actualNumber.Should().Be(winningNumber); + result1.l.Value.actualNumber.ShouldBe(winningNumber); + result2.l.Value.actualNumber.ShouldBe(winningNumber); - winnerCount.Should().Be(1); + winnerCount.ShouldBe(1); } [Fact] @@ -179,10 +179,10 @@ public async Task WhenDisposedWhileInitResultIsDisposed() var result = await first; - result.r.Should().BeFalse(); - result.l.Should().BeNull(); + result.r.ShouldBeFalse(); + result.l.ShouldBeNull(); - holder.disposed.Should().BeTrue(); + holder.disposed.ShouldBeTrue(); } [Fact] @@ -211,17 +211,17 @@ public async Task WhenDisposedWhileThrowingNextInitIsDisposed() // If we create an item, to be in a consistent state we should dispose it. Func tryCreateAsync = async () => { await first; }; - await tryCreateAsync.Should().ThrowAsync(); + var ex = await tryCreateAsync.ShouldThrowAsync();; (bool r, Lifetime l) result = await atomicFactory.TryCreateLifetimeAsync(1, k => { return Task.FromResult(new Scoped(holder)); }); - result.r.Should().BeFalse(); - result.l.Should().BeNull(); + result.r.ShouldBeFalse(); + result.l.ShouldBeNull(); - holder.disposed.Should().BeTrue(); + holder.disposed.ShouldBeTrue(); } private static AsyncValueFactoryArg> CreateArgFactory(int arg) diff --git a/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactorySoakTests.cs b/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactorySoakTests.cs index d99c6200..d7387de1 100644 --- a/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactorySoakTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactorySoakTests.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Threading.Tasks; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -30,7 +30,7 @@ await Threaded.Run(threads, (r) => { using (lifetime) { - lifetime.Value.IsDisposed.Should().BeFalse(); + lifetime.Value.IsDisposed.ShouldBeFalse(); } break; @@ -40,7 +40,7 @@ await Threaded.Run(threads, (r) => } }); - counters.Sum(x => x).Should().Be(items); + counters.Sum(x => x).ShouldBe(items); } [Fact] @@ -65,7 +65,7 @@ await Threaded.Run(threads, (r) => { using (lifetime) { - lifetime.Value.IsDisposed.Should().BeFalse(); + lifetime.Value.IsDisposed.ShouldBeFalse(); } break; diff --git a/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactoryTests.cs b/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactoryTests.cs index 072375be..d9704330 100644 --- a/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactoryTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactoryTests.cs @@ -1,6 +1,5 @@ - -using BitFaster.Caching.Atomic; -using FluentAssertions; +using BitFaster.Caching.Atomic; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Atomic @@ -13,9 +12,9 @@ public void WhenInitializedWithValueTryCreateLifetimeCreatesLifetimeWithValue() var expectedDisposable = new Disposable(); var sa = new ScopedAtomicFactory(expectedDisposable); - sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var lifetime).Should().BeTrue(); + sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var lifetime).ShouldBeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] @@ -24,9 +23,9 @@ public void WhenInitializedWithFactoryTryCreateLifetimeCreatesLifetimeWithValue( var expectedDisposable = new Disposable(); var sa = new ScopedAtomicFactory(); - sa.TryCreateLifetime(1, k => new Scoped(expectedDisposable), out var lifetime).Should().BeTrue(); + sa.TryCreateLifetime(1, k => new Scoped(expectedDisposable), out var lifetime).ShouldBeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] @@ -35,10 +34,10 @@ public void WhenInitializedWithFactoryValueIsCached() var expectedDisposable = new Disposable(); var sa = new ScopedAtomicFactory(); - sa.TryCreateLifetime(1, k => new Scoped(expectedDisposable), out var lifetime1).Should().BeTrue(); - sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var lifetime2).Should().BeTrue(); + sa.TryCreateLifetime(1, k => new Scoped(expectedDisposable), out var lifetime1).ShouldBeTrue(); + sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var lifetime2).ShouldBeTrue(); - lifetime2.Value.Should().Be(expectedDisposable); + lifetime2.Value.ShouldBe(expectedDisposable); } [Fact] @@ -50,11 +49,11 @@ public void WhenInitializedWithFactoryArgValueIsCached() var factory1 = new ValueFactoryArg>((k, v) => { expectedDisposable.State = v; return new Scoped(expectedDisposable); }, 1); var factory2 = new ValueFactoryArg>((k, v) => { expectedDisposable.State = v; return new Scoped(expectedDisposable); }, 2); - sa.TryCreateLifetime(1, factory1, out var lifetime1).Should().BeTrue(); - sa.TryCreateLifetime(1, factory2, out var lifetime2).Should().BeTrue(); + sa.TryCreateLifetime(1, factory1, out var lifetime1).ShouldBeTrue(); + sa.TryCreateLifetime(1, factory2, out var lifetime2).ShouldBeTrue(); - lifetime2.Value.Should().Be(expectedDisposable); - lifetime2.Value.State.Should().Be(1); + lifetime2.Value.ShouldBe(expectedDisposable); + lifetime2.Value.State.ShouldBe(1); } [Fact] @@ -62,7 +61,7 @@ public void WhenScopeIsNotCreatedScopeIfCreatedReturnsNull() { var sa = new ScopedAtomicFactory(); - sa.ScopeIfCreated.Should().BeNull(); + sa.ScopeIfCreated.ShouldBeNull(); } [Fact] @@ -71,16 +70,16 @@ public void WhenScopeIsCreatedScopeIfCreatedReturnsScope() var expectedDisposable = new Disposable(); var sa = new ScopedAtomicFactory(expectedDisposable); - sa.ScopeIfCreated.Should().NotBeNull(); - sa.ScopeIfCreated.TryCreateLifetime(out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + sa.ScopeIfCreated.ShouldNotBeNull(); + sa.ScopeIfCreated.TryCreateLifetime(out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] public void WhenNotInitTryCreateReturnsFalse() { var sa = new ScopedAtomicFactory(); - sa.TryCreateLifetime(out var l).Should().BeFalse(); + sa.TryCreateLifetime(out var l).ShouldBeFalse(); } [Fact] @@ -89,8 +88,8 @@ public void WhenCreatedTryCreateLifetimeReturnsScope() var expectedDisposable = new Disposable(); var sa = new ScopedAtomicFactory(expectedDisposable); - sa.TryCreateLifetime(out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(expectedDisposable); + sa.TryCreateLifetime(out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(expectedDisposable); } [Fact] @@ -99,7 +98,7 @@ public void WhenScopeDisposedTryCreateLifetimeReturnsFalse() var sa = new ScopedAtomicFactory(); sa.Dispose(); - sa.TryCreateLifetime(out var lifetime).Should().BeFalse(); + sa.TryCreateLifetime(out var lifetime).ShouldBeFalse(); } [Fact] @@ -108,7 +107,7 @@ public void WhenInitializedWithValueThenDisposedCreateLifetimeIsFalse() var sa = new ScopedAtomicFactory(new Disposable()); sa.Dispose(); - sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var l).Should().BeFalse(); + sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var l).ShouldBeFalse(); } [Fact] @@ -117,7 +116,7 @@ public void WhenCreatedThenDisposedCreateLifetimeIsFalse() var sa = new ScopedAtomicFactory(); sa.Dispose(); - sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var l).Should().BeFalse(); + sa.TryCreateLifetime(1, k => new Scoped(new Disposable()), out var l).ShouldBeFalse(); } [Fact] @@ -126,17 +125,17 @@ public void WhenInitializedLifetimeKeepsValueAlive() var disposable = new Disposable(); var sa = new ScopedAtomicFactory(); - sa.TryCreateLifetime(1, k => new Scoped(disposable), out var lifetime1).Should().BeTrue(); - sa.TryCreateLifetime(1, k => null, out var lifetime2).Should().BeTrue(); + sa.TryCreateLifetime(1, k => new Scoped(disposable), out var lifetime1).ShouldBeTrue(); + sa.TryCreateLifetime(1, k => null, out var lifetime2).ShouldBeTrue(); sa.Dispose(); - disposable.IsDisposed.Should().BeFalse(); + disposable.IsDisposed.ShouldBeFalse(); lifetime1.Dispose(); - disposable.IsDisposed.Should().BeFalse(); + disposable.IsDisposed.ShouldBeFalse(); lifetime2.Dispose(); - disposable.IsDisposed.Should().BeTrue(); + disposable.IsDisposed.ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj index 93a5a179..b8b0a674 100644 --- a/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj +++ b/BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj @@ -1,4 +1,4 @@ - + net48;netcoreapp3.1;net6.0 @@ -10,20 +10,20 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + diff --git a/BitFaster.Caching.UnitTests/BitOpsTests.cs b/BitFaster.Caching.UnitTests/BitOpsTests.cs index bfc956c2..6b52cfff 100644 --- a/BitFaster.Caching.UnitTests/BitOpsTests.cs +++ b/BitFaster.Caching.UnitTests/BitOpsTests.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -17,7 +12,7 @@ public class BitOpsTests [InlineData(536870913, 1073741824)] public void IntCeilingPowerOfTwo(int input, int power) { - BitOps.CeilingPowerOfTwo(input).Should().Be(power); + BitOps.CeilingPowerOfTwo(input).ShouldBe(power); } [Theory] @@ -28,7 +23,7 @@ public void IntCeilingPowerOfTwo(int input, int power) [InlineData(34359738368, 34359738368)] public void LongCeilingPowerOfTwo(long input, long power) { - BitOps.CeilingPowerOfTwo(input).Should().Be(power); + BitOps.CeilingPowerOfTwo(input).ShouldBe(power); } [Theory] @@ -39,7 +34,7 @@ public void LongCeilingPowerOfTwo(long input, long power) public void UIntCeilingPowerOfTwo(uint input, uint power) { - BitOps.CeilingPowerOfTwo(input).Should().Be(power); + BitOps.CeilingPowerOfTwo(input).ShouldBe(power); } [Theory] @@ -51,7 +46,7 @@ public void UIntCeilingPowerOfTwo(uint input, uint power) public void UlongCeilingPowerOfTwo(ulong input, ulong power) { - BitOps.CeilingPowerOfTwo(input).Should().Be(power); + BitOps.CeilingPowerOfTwo(input).ShouldBe(power); } [Theory] @@ -65,7 +60,7 @@ public void UlongCeilingPowerOfTwo(ulong input, ulong power) public void LongTrailingZeroCount(long input, int count) { - BitOps.TrailingZeroCount(input).Should().Be(count); + BitOps.TrailingZeroCount(input).ShouldBe(count); } [Theory] @@ -79,25 +74,25 @@ public void LongTrailingZeroCount(long input, int count) public void ULongTrailingZeroCount(ulong input, int count) { - BitOps.TrailingZeroCount(input).Should().Be(count); + BitOps.TrailingZeroCount(input).ShouldBe(count); } [Fact] public void IntBitCount() { - BitOps.BitCount(666).Should().Be(5); + BitOps.BitCount(666).ShouldBe(5); } [Fact] public void LongtBitCount() { - BitOps.BitCount(666L).Should().Be(5); + BitOps.BitCount(666L).ShouldBe(5); } [Fact] public void ULongtBitCount() { - BitOps.BitCount(666UL).Should().Be(5); + BitOps.BitCount(666UL).ShouldBe(5); } } } diff --git a/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferSoakTests.cs b/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferSoakTests.cs index 01fab64f..4ad20552 100644 --- a/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferSoakTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Buffers; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -33,7 +33,7 @@ await Threaded.Run(4, () => { } - buffer.Count.Should().Be(1024); + buffer.Count.ShouldBe(1024); }); } @@ -80,7 +80,7 @@ public async Task WhileBufferIsFilledCountCanBeTaken() while (!fill.IsCompleted) { int newcount = buffer.Count; - newcount.Should().BeGreaterThanOrEqualTo(count); + newcount.ShouldBeGreaterThanOrEqualTo(count); count = newcount; } }); diff --git a/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferTests.cs b/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferTests.cs index ede26594..fbf13035 100644 --- a/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferTests.cs +++ b/BitFaster.Caching.UnitTests/Buffers/MpmcBoundedBufferTests.cs @@ -1,11 +1,6 @@ using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Buffers; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Buffers @@ -19,19 +14,19 @@ public void WhenSizeIsLessThan1CtorThrows() { Action constructor = () => { var x = new MpmcBoundedBuffer(-1); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void SizeIsPowerOfTwo() { - buffer.Capacity.Should().Be(16); + buffer.Capacity.ShouldBe(16); } [Fact] public void WhenBufferIsEmptyCountIsZero() { - buffer.Count.Should().Be(0); + buffer.Count.ShouldBe(0); } [Fact] @@ -39,22 +34,22 @@ public void WhenBufferHasOneItemCountIsOne() { // head < tail buffer.TryAdd(1); - buffer.Count.Should().Be(1); + buffer.Count.ShouldBe(1); } [Fact] public void WhenBufferHas15ItemCountIs15() { - buffer.TryAdd(0).Should().Be(BufferStatus.Success); - buffer.TryTake(out var _).Should().Be(BufferStatus.Success); + buffer.TryAdd(0).ShouldBe(BufferStatus.Success); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Success); for (var i = 0; i < 15; i++) { - buffer.TryAdd(0).Should().Be(BufferStatus.Success); + buffer.TryAdd(0).ShouldBe(BufferStatus.Success); } // head = 1, tail = 0 : head > tail - buffer.Count.Should().Be(15); + buffer.Count.ShouldBe(15); } [Fact] @@ -62,24 +57,24 @@ public void WhenBufferIsFullTryAddIsFalse() { for (var i = 0; i < 16; i++) { - buffer.TryAdd(i).Should().Be(BufferStatus.Success); + buffer.TryAdd(i).ShouldBe(BufferStatus.Success); } - buffer.TryAdd(666).Should().Be(BufferStatus.Full); + buffer.TryAdd(666).ShouldBe(BufferStatus.Full); } [Fact] public void WhenBufferIsEmptyTryTakeIsFalse() { - buffer.TryTake(out var _).Should().Be(BufferStatus.Empty); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Empty); } [Fact] public void WhenItemAddedItCanBeTaken() { - buffer.TryAdd(123).Should().Be(BufferStatus.Success); - buffer.TryTake(out var item).Should().Be(BufferStatus.Success); - item.Should().Be(123); + buffer.TryAdd(123).ShouldBe(BufferStatus.Success); + buffer.TryTake(out var item).ShouldBe(BufferStatus.Success); + item.ShouldBe(123); } [Fact] @@ -88,12 +83,12 @@ public void WhenItemsAreAddedClearRemovesItems() buffer.TryAdd(1); buffer.TryAdd(2); - buffer.Count.Should().Be(2); + buffer.Count.ShouldBe(2); buffer.Clear(); - buffer.Count.Should().Be(0); - buffer.TryTake(out var _).Should().Be(BufferStatus.Empty); + buffer.Count.ShouldBe(0); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Empty); } } } diff --git a/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferSoakTests.cs b/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferSoakTests.cs index edc77a36..0d94f731 100644 --- a/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferSoakTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Buffers; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -33,7 +33,7 @@ await Threaded.Run(4, () => { } - buffer.Count.Should().Be(1024); + buffer.Count.ShouldBe(1024); }); } @@ -99,7 +99,7 @@ public async Task WhileBufferIsFilledCountCanBeTaken() while (!fill.IsCompleted) { int newcount = buffer.Count; - newcount.Should().BeGreaterThanOrEqualTo(count); + newcount.ShouldBeGreaterThanOrEqualTo(count); count = newcount; } }); diff --git a/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferTests.cs b/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferTests.cs index a4a65242..3059ac64 100644 --- a/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferTests.cs +++ b/BitFaster.Caching.UnitTests/Buffers/MpscBoundedBufferTests.cs @@ -1,6 +1,6 @@ using System; using BitFaster.Caching.Buffers; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Buffers @@ -14,19 +14,19 @@ public void WhenSizeIsLessThan1CtorThrows() { Action constructor = () => { var x = new MpscBoundedBuffer(-1); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void SizeIsPowerOfTwo() { - buffer.Capacity.Should().Be(16); + buffer.Capacity.ShouldBe(16); } [Fact] public void WhenBufferIsEmptyCountIsZero() { - buffer.Count.Should().Be(0); + buffer.Count.ShouldBe(0); } [Fact] @@ -34,22 +34,22 @@ public void WhenBufferHasOneItemCountIsOne() { // head < tail buffer.TryAdd("1"); - buffer.Count.Should().Be(1); + buffer.Count.ShouldBe(1); } [Fact] public void WhenBufferHas15ItemCountIs15() { - buffer.TryAdd("1").Should().Be(BufferStatus.Success); - buffer.TryTake(out var _).Should().Be(BufferStatus.Success); + buffer.TryAdd("1").ShouldBe(BufferStatus.Success); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Success); for (var i = 0; i < 15; i++) { - buffer.TryAdd("0").Should().Be(BufferStatus.Success); + buffer.TryAdd("0").ShouldBe(BufferStatus.Success); } // head = 1, tail = 0 : head > tail - buffer.Count.Should().Be(15); + buffer.Count.ShouldBe(15); } [Fact] @@ -57,24 +57,24 @@ public void WhenBufferIsFullTryAddIsFalse() { for (var i = 0; i < 16; i++) { - buffer.TryAdd(i.ToString()).Should().Be(BufferStatus.Success); + buffer.TryAdd(i.ToString()).ShouldBe(BufferStatus.Success); } - buffer.TryAdd("666").Should().Be(BufferStatus.Full); + buffer.TryAdd("666").ShouldBe(BufferStatus.Full); } [Fact] public void WhenBufferIsEmptyTryTakeIsFalse() { - buffer.TryTake(out var _).Should().Be(BufferStatus.Empty); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Empty); } [Fact] public void WhenItemAddedItCanBeTaken() { - buffer.TryAdd("123").Should().Be(BufferStatus.Success); - buffer.TryTake(out var item).Should().Be(BufferStatus.Success); - item.Should().Be("123"); + buffer.TryAdd("123").ShouldBe(BufferStatus.Success); + buffer.TryTake(out var item).ShouldBe(BufferStatus.Success); + item.ShouldBe("123"); } [Fact] @@ -83,12 +83,12 @@ public void WhenItemsAreAddedClearRemovesItems() buffer.TryAdd("1"); buffer.TryAdd("2"); - buffer.Count.Should().Be(2); + buffer.Count.ShouldBe(2); buffer.Clear(); - buffer.Count.Should().Be(0); - buffer.TryTake(out var _).Should().Be(BufferStatus.Empty); + buffer.Count.ShouldBe(0); + buffer.TryTake(out var _).ShouldBe(BufferStatus.Empty); } [Fact] @@ -97,7 +97,7 @@ public void WhenBufferEmptyDrainReturnsZero() var outputBuffer = new string[16]; var output = new ArraySegment(outputBuffer); - buffer.DrainTo(output).Should().Be(0); + buffer.DrainTo(output).ShouldBe(0); } #if NETCOREAPP3_0_OR_GREATER @@ -110,11 +110,11 @@ public void WhenBufferContainsItemsDrainArrayTakesItems() var outputBuffer = new string[16]; - buffer.DrainTo(outputBuffer.AsSpan()).Should().Be(3); + buffer.DrainTo(outputBuffer.AsSpan()).ShouldBe(3); - outputBuffer[0].Should().Be("1"); - outputBuffer[1].Should().Be("2"); - outputBuffer[2].Should().Be("3"); + outputBuffer[0].ShouldBe("1"); + outputBuffer[1].ShouldBe("2"); + outputBuffer[2].ShouldBe("3"); } #endif @@ -128,11 +128,11 @@ public void WhenBufferContainsItemsDrainSegmentTakesItems() var outputBuffer = new string[16]; var output = new ArraySegment(outputBuffer); - buffer.DrainTo(output).Should().Be(3); + buffer.DrainTo(output).ShouldBe(3); - outputBuffer[0].Should().Be("1"); - outputBuffer[1].Should().Be("2"); - outputBuffer[2].Should().Be("3"); + outputBuffer[0].ShouldBe("1"); + outputBuffer[1].ShouldBe("2"); + outputBuffer[2].ShouldBe("3"); } [Fact] @@ -145,11 +145,11 @@ public void WhenSegmentUsesOffsetItemsDrainedToOffset() var outputBuffer = new string[16]; var output = new ArraySegment(outputBuffer, 6, 10); - buffer.DrainTo(output).Should().Be(3); + buffer.DrainTo(output).ShouldBe(3); - outputBuffer[6].Should().Be("1"); - outputBuffer[7].Should().Be("2"); - outputBuffer[8].Should().Be("3"); + outputBuffer[6].ShouldBe("1"); + outputBuffer[7].ShouldBe("2"); + outputBuffer[8].ShouldBe("3"); } } } diff --git a/BitFaster.Caching.UnitTests/Buffers/StripedMpscBufferTests.cs b/BitFaster.Caching.UnitTests/Buffers/StripedMpscBufferTests.cs index 0c621f0d..9016d0db 100644 --- a/BitFaster.Caching.UnitTests/Buffers/StripedMpscBufferTests.cs +++ b/BitFaster.Caching.UnitTests/Buffers/StripedMpscBufferTests.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using BitFaster.Caching.Buffers; -using FluentAssertions; +using BitFaster.Caching.Buffers; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Buffers @@ -18,23 +13,23 @@ public class StripedMpscBufferTests [Fact] public void CapacityReturnsCapacity() { - buffer.Capacity.Should().Be(32); + buffer.Capacity.ShouldBe(32); } [Fact] public void CountReturnsCount() { - buffer.Count.Should().Be(0); + buffer.Count.ShouldBe(0); for (var i = 0; i < stripeCount; i++) { for (var j = 0; j < bufferSize; j++) { - buffer.TryAdd(1.ToString()).Should().Be(BufferStatus.Success); + buffer.TryAdd(1.ToString()).ShouldBe(BufferStatus.Success); } } - buffer.Count.Should().Be(buffer.Capacity); + buffer.Count.ShouldBe(buffer.Capacity); } [Fact] @@ -44,18 +39,18 @@ public void WhenBufferIsFullTryAddReturnsFull() { for (var j = 0; j < bufferSize; j++) { - buffer.TryAdd(1.ToString()).Should().Be(BufferStatus.Success); + buffer.TryAdd(1.ToString()).ShouldBe(BufferStatus.Success); } } - buffer.TryAdd("1").Should().Be(BufferStatus.Full); + buffer.TryAdd("1").ShouldBe(BufferStatus.Full); } [Fact] public void WhenBufferIsEmptyDrainReturnsZero() { var array = new string[bufferSize]; - buffer.DrainTo(array).Should().Be(0); + buffer.DrainTo(array).ShouldBe(0); } [Fact] @@ -70,7 +65,7 @@ public void WhenBufferIsFullDrainReturnsItemCount() } var array = new string[bufferSize * stripeCount]; - buffer.DrainTo(array).Should().Be(stripeCount * bufferSize); + buffer.DrainTo(array).ShouldBe(stripeCount * bufferSize); } [Fact] @@ -85,7 +80,7 @@ public void WhenDrainBufferIsSmallerThanStripedBufferDrainReturnsBufferItemCount } var array = new string[bufferSize]; - buffer.DrainTo(array).Should().Be(bufferSize); + buffer.DrainTo(array).ShouldBe(bufferSize); } [Fact] @@ -100,7 +95,7 @@ public void WhenDrainBufferIsSmallerThanStripedBufferDrainReturnsBufferItemCount } var array = new string[bufferSize+4]; - buffer.DrainTo(array).Should().Be(bufferSize+4); + buffer.DrainTo(array).ShouldBe(bufferSize+4); } [Fact] @@ -112,7 +107,7 @@ public void WhenBufferIsPartFullDrainReturnsItems() } var array = new string[bufferSize * stripeCount]; - buffer.DrainTo(array).Should().Be(bufferSize); + buffer.DrainTo(array).ShouldBe(bufferSize); } [Fact] @@ -129,7 +124,7 @@ public void WhenBufferIsClearedDrainReturns0() buffer.Clear(); var array = new string[bufferSize * stripeCount]; - buffer.DrainTo(array).Should().Be(0); + buffer.DrainTo(array).ShouldBe(0); } } } diff --git a/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs b/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs index 6cbd4908..0da88486 100644 --- a/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs +++ b/BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using BitFaster.Caching.Atomic; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -28,7 +28,7 @@ public void WheRemovedEventHandlerIsRegisteredItIsFired() this.testCacheEvents.FireRemoved(1, new AtomicFactory(1), ItemRemovedReason.Removed); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } [Fact] @@ -39,7 +39,7 @@ public void WhenRemovedEventHandlerIsAddedThenRemovedItIsNotFired() this.testCacheEvents.FireRemoved(1, new AtomicFactory(1), ItemRemovedReason.Removed); - this.removedItems.Count.Should().Be(0); + this.removedItems.Count.ShouldBe(0); } [Fact] @@ -51,7 +51,7 @@ public void WhenTwoRemovedEventHandlersAddedThenOneRemovedEventIsFired() this.testCacheEvents.FireRemoved(1, new AtomicFactory(1), ItemRemovedReason.Removed); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } // backcompat: remove conditional compile @@ -63,9 +63,9 @@ public void WheUpdatedEventHandlerIsRegisteredItIsFired() this.testCacheEvents.FireUpdated(1, new AtomicFactory(2), new AtomicFactory(3)); - this.updatedItems.First().Key.Should().Be(1); - this.updatedItems.First().OldValue.Should().Be(2); - this.updatedItems.First().NewValue.Should().Be(3); + this.updatedItems.First().Key.ShouldBe(1); + this.updatedItems.First().OldValue.ShouldBe(2); + this.updatedItems.First().NewValue.ShouldBe(3); } [Fact] @@ -76,7 +76,7 @@ public void WhenUpdatedEventHandlerIsAddedThenRemovedItIsNotFired() this.testCacheEvents.FireUpdated(1, new AtomicFactory(2), new AtomicFactory(3)); - this.updatedItems.Count.Should().Be(0); + this.updatedItems.Count.ShouldBe(0); } [Fact] @@ -88,7 +88,7 @@ public void WhenTwoUpdatedEventHandlersAddedThenOneRemovedEventIsFired() this.testCacheEvents.FireUpdated(1, new AtomicFactory(2), new AtomicFactory(3)); - this.updatedItems.First().Key.Should().Be(1); + this.updatedItems.First().Key.ShouldBe(1); } #endif private void OnItemRemoved(object sender, ItemRemovedEventArgs e) @@ -156,9 +156,9 @@ public void WhenUpdatedEventHandlerIsRegisteredAndProxyUsesDefaultUpdateTranslat this.testCacheEvents.FireUpdated(1, new AtomicFactory(2), new AtomicFactory(3)); #if NETCOREAPP3_0_OR_GREATER - this.updatedItems.First().Key.Should().Be(1); + this.updatedItems.First().Key.ShouldBe(1); #else - this.updatedItems.Should().BeEmpty(); + this.updatedItems.ShouldBeEmpty(); #endif } diff --git a/BitFaster.Caching.UnitTests/CacheEventsTests.cs b/BitFaster.Caching.UnitTests/CacheEventsTests.cs index e51002d2..9f54199b 100644 --- a/BitFaster.Caching.UnitTests/CacheEventsTests.cs +++ b/BitFaster.Caching.UnitTests/CacheEventsTests.cs @@ -1,5 +1,4 @@ - -using Moq; +using Moq; using Xunit; namespace BitFaster.Caching.UnitTests diff --git a/BitFaster.Caching.UnitTests/CacheMetricsTests.cs b/BitFaster.Caching.UnitTests/CacheMetricsTests.cs index 62bf3368..82f61566 100644 --- a/BitFaster.Caching.UnitTests/CacheMetricsTests.cs +++ b/BitFaster.Caching.UnitTests/CacheMetricsTests.cs @@ -1,5 +1,4 @@ - -using FluentAssertions; +using Shouldly; using Moq; using Xunit; @@ -15,7 +14,7 @@ public void WhenInterfaceDefaultUpdatedInvokedReturnZero() var metrics = new Mock(); metrics.CallBase = true; - metrics.Object.Updated.Should().Be(0); + metrics.Object.Updated.ShouldBe(0); } } #endif diff --git a/BitFaster.Caching.UnitTests/CachePolicyTests.cs b/BitFaster.Caching.UnitTests/CachePolicyTests.cs index 8674ad37..f74c40e2 100644 --- a/BitFaster.Caching.UnitTests/CachePolicyTests.cs +++ b/BitFaster.Caching.UnitTests/CachePolicyTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using Moq; using Xunit; @@ -14,10 +14,10 @@ public void WhenCtorFieldsAreAssigned() var cp = new CachePolicy(new Optional(eviction.Object), new Optional(expire.Object)); - cp.Eviction.Value.Should().Be(eviction.Object); - cp.ExpireAfterWrite.Value.Should().Be(expire.Object); - cp.ExpireAfterAccess.HasValue.Should().BeFalse(); - cp.ExpireAfter.HasValue.Should().BeFalse(); + cp.Eviction.Value.ShouldBe(eviction.Object); + cp.ExpireAfterWrite.Value.ShouldBe(expire.Object); + cp.ExpireAfterAccess.HasValue.ShouldBeFalse(); + cp.ExpireAfter.HasValue.ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/CacheTests.cs b/BitFaster.Caching.UnitTests/CacheTests.cs index 5cdeafa0..7d109add 100644 --- a/BitFaster.Caching.UnitTests/CacheTests.cs +++ b/BitFaster.Caching.UnitTests/CacheTests.cs @@ -1,8 +1,7 @@ - -using System; -using System.Collections.Generic; +using System; using System.Threading.Tasks; -using FluentAssertions; +using System.Collections.Generic; +using Shouldly; using Moq; using Xunit; @@ -25,7 +24,7 @@ public void WhenCacheInterfaceDefaultGetOrAddFallback() cache.Object.GetOrAdd( 1, (k, a) => k + a, - 2).Should().Be(3); + 2).ShouldBe(3); } [Fact] @@ -36,7 +35,7 @@ public void WhenCacheInterfaceDefaultTryRemoveKeyThrows() Action tryRemove = () => { cache.Object.TryRemove(1, out var value); }; - tryRemove.Should().Throw(); + tryRemove.ShouldThrow(); } [Fact] @@ -47,7 +46,7 @@ public void WhenCacheInterfaceDefaultTryRemoveKeyValueThrows() Action tryRemove = () => { cache.Object.TryRemove(new KeyValuePair(1, 1)); }; - tryRemove.Should().Throw(); + tryRemove.ShouldThrow(); } [Fact] @@ -64,7 +63,7 @@ public async Task WhenAsyncCacheInterfaceDefaultGetOrAddFallback() (k, a) => Task.FromResult(k + a), 2); - r.Should().Be(3); + r.ShouldBe(3); } [Fact] @@ -75,7 +74,7 @@ public void WhenAsyncCacheInterfaceDefaultTryRemoveKeyThrows() Action tryRemove = () => { cache.Object.TryRemove(1, out var value); }; - tryRemove.Should().Throw(); + tryRemove.ShouldThrow(); } [Fact] @@ -86,7 +85,7 @@ public void WhenAsyncCacheInterfaceDefaultTryRemoveKeyValueThrows() Action tryRemove = () => { cache.Object.TryRemove(new KeyValuePair(1, 1)); }; - tryRemove.Should().Throw(); + tryRemove.ShouldThrow(); } [Fact] @@ -98,7 +97,7 @@ public void WhenScopedCacheInterfaceDefaultGetOrAddFallback() Func>, Lifetime> evaluate = (k, f) => { var scope = f(k); - scope.TryCreateLifetime(out var lifetime).Should().BeTrue(); + scope.TryCreateLifetime(out var lifetime).ShouldBeTrue(); return lifetime; }; @@ -109,7 +108,7 @@ public void WhenScopedCacheInterfaceDefaultGetOrAddFallback() (k, a) => new Scoped(new Disposable(k + a)), 2); - l.Value.State.Should().Be(3); + l.Value.State.ShouldBe(3); } [Fact] @@ -121,7 +120,7 @@ public async Task WhenScopedAsyncCacheInterfaceDefaultGetOrAddFallback() Func>>, ValueTask>> evaluate = async (k, f) => { var scope = await f(k); - scope.TryCreateLifetime(out var lifetime).Should().BeTrue(); + scope.TryCreateLifetime(out var lifetime).ShouldBeTrue(); return lifetime; }; @@ -134,7 +133,7 @@ public async Task WhenScopedAsyncCacheInterfaceDefaultGetOrAddFallback() (k, a) => Task.FromResult(new Scoped(new Disposable(k + a))), 2); - lifetime.Value.State.Should().Be(3); + lifetime.Value.State.ShouldBe(3); } #endif } diff --git a/BitFaster.Caching.UnitTests/ConcurrentDictionarySizeTests.cs b/BitFaster.Caching.UnitTests/ConcurrentDictionarySizeTests.cs index 8665f5d3..1ae5f724 100644 --- a/BitFaster.Caching.UnitTests/ConcurrentDictionarySizeTests.cs +++ b/BitFaster.Caching.UnitTests/ConcurrentDictionarySizeTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -21,7 +21,7 @@ public ConcurrentDictionarySizeTests(ITestOutputHelper testOutputHelper) [InlineData(7199370, 7199370)] public void NextPrimeGreaterThan(int input, int nextPrime) { - ConcurrentDictionarySize.NextPrimeGreaterThan(input).Should().Be(nextPrime); + ConcurrentDictionarySize.NextPrimeGreaterThan(input).ShouldBe(nextPrime); } [Theory] @@ -35,7 +35,7 @@ public void NextPrimeGreaterThan(int input, int nextPrime) [InlineData(2003828731, 250478587)] // test overflow public void Estimate(int input, int optimal) { - ConcurrentDictionarySize.Estimate(input).Should().Be(optimal); + ConcurrentDictionarySize.Estimate(input).ShouldBe(optimal); } } } diff --git a/BitFaster.Caching.UnitTests/Counters/CounterSoakTests.cs b/BitFaster.Caching.UnitTests/Counters/CounterSoakTests.cs index e9fb1e4f..91f91e61 100644 --- a/BitFaster.Caching.UnitTests/Counters/CounterSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Counters/CounterSoakTests.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; using BitFaster.Caching.Counters; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Counters @@ -21,7 +21,7 @@ await Threaded.Run(4, () => } }); - adder.Count().Should().Be(400_000); + adder.Count().ShouldBe(400_000); } } } diff --git a/BitFaster.Caching.UnitTests/Counters/StripedLongAdderTests.cs b/BitFaster.Caching.UnitTests/Counters/StripedLongAdderTests.cs index 4062e2af..0e32eb78 100644 --- a/BitFaster.Caching.UnitTests/Counters/StripedLongAdderTests.cs +++ b/BitFaster.Caching.UnitTests/Counters/StripedLongAdderTests.cs @@ -1,5 +1,5 @@ using BitFaster.Caching.Counters; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Counters @@ -9,7 +9,7 @@ public class StripedLongAdderTests [Fact] public void InitialValueIsZero() { - new Counter().Count().Should().Be(0); + new Counter().Count().ShouldBe(0); } [Fact] @@ -19,7 +19,7 @@ public void WhenIncrementedOneIsAdded() adder.Increment(); - adder.Count().Should().Be(1); + adder.Count().ShouldBe(1); } } } diff --git a/BitFaster.Caching.UnitTests/Disposable.cs b/BitFaster.Caching.UnitTests/Disposable.cs index 2ca08414..8b671a8e 100644 --- a/BitFaster.Caching.UnitTests/Disposable.cs +++ b/BitFaster.Caching.UnitTests/Disposable.cs @@ -1,5 +1,5 @@ using System; -using FluentAssertions; +using Shouldly; namespace BitFaster.Caching.UnitTests { @@ -15,7 +15,7 @@ public Disposable() { } public void Dispose() { - this.IsDisposed.Should().BeFalse(); + this.IsDisposed.ShouldBeFalse(); IsDisposed = true; } } diff --git a/BitFaster.Caching.UnitTests/DisposableValueFactory.cs b/BitFaster.Caching.UnitTests/DisposableValueFactory.cs index 0d1fb472..45275365 100644 --- a/BitFaster.Caching.UnitTests/DisposableValueFactory.cs +++ b/BitFaster.Caching.UnitTests/DisposableValueFactory.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BitFaster.Caching.UnitTests +namespace BitFaster.Caching.UnitTests { public class DisposableValueFactory { diff --git a/BitFaster.Caching.UnitTests/DurationTests.cs b/BitFaster.Caching.UnitTests/DurationTests.cs index 38fbce72..22c8e816 100644 --- a/BitFaster.Caching.UnitTests/DurationTests.cs +++ b/BitFaster.Caching.UnitTests/DurationTests.cs @@ -2,7 +2,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -25,24 +25,22 @@ public void SinceEpoch() #if NETCOREAPP3_0_OR_GREATER if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - // eps is 1/200 of a second - ulong eps = (ulong)(Stopwatch.Frequency / 200); - Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); + Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp()); } else { - Duration.SinceEpoch().raw.Should().BeCloseTo(Environment.TickCount64, 15); + Duration.SinceEpoch().raw.ShouldBe(Environment.TickCount64); } #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Duration.SinceEpoch().raw.Should().BeCloseTo(Duration.GetTickCount64(), 15); + Duration.SinceEpoch().raw.ShouldBe(Duration.GetTickCount64()); } else { // eps is 1/200 of a second ulong eps = (ulong)(Stopwatch.Frequency / 200); - Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); + Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp()); } #endif } @@ -53,21 +51,21 @@ public void ToTimeSpan() #if NETCOREAPP3_0_OR_GREATER if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - new Duration(100).ToTimeSpan().Should().BeCloseTo(new TimeSpan(100), TimeSpan.FromMilliseconds(50)); + new Duration(100).ToTimeSpan().ShouldBe(new TimeSpan(100), TimeSpan.FromMilliseconds(50)); } else { - new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); + new Duration(1000).ToTimeSpan().ShouldBe(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); } #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); + new Duration(1000).ToTimeSpan().ShouldBe(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); } else { // for Stopwatch.GetTimestamp() this is number of ticks - new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); + new Duration(1 * Stopwatch.Frequency).ToTimeSpan().ShouldBe(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); } #endif } @@ -79,23 +77,23 @@ public void FromTimeSpan() if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be(Stopwatch.Frequency); + .ShouldBe(Stopwatch.Frequency); } else { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds); + .ShouldBe((long)TimeSpan.FromSeconds(1).TotalMilliseconds); } #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds); + .ShouldBe((long)TimeSpan.FromSeconds(1).TotalMilliseconds); } else { Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw - .Should().Be(Stopwatch.Frequency); + .ShouldBe(Stopwatch.Frequency); } #endif } @@ -105,7 +103,7 @@ public void RoundTripMilliseconds() { Duration.FromMilliseconds(2000) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromMilliseconds(2000), TimeSpan.FromMilliseconds(50)); + .ShouldBe(TimeSpan.FromMilliseconds(2000), TimeSpan.FromMilliseconds(50)); } [Fact] @@ -113,7 +111,7 @@ public void RoundTripSeconds() { Duration.FromSeconds(2) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(50)); + .ShouldBe(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(50)); } [Fact] @@ -121,7 +119,7 @@ public void RoundTripMinutes() { Duration.FromMinutes(2) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(100)); + .ShouldBe(TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(100)); } [Fact] @@ -129,7 +127,7 @@ public void RoundTripHours() { Duration.FromHours(2) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromHours(2), TimeSpan.FromMilliseconds(100)); + .ShouldBe(TimeSpan.FromHours(2), TimeSpan.FromMilliseconds(100)); } [Fact] @@ -137,7 +135,7 @@ public void RoundTripDays() { Duration.FromDays(2) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); + .ShouldBe(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); } [Fact] @@ -145,7 +143,7 @@ public void OperatorPlus() { (Duration.FromDays(2) + Duration.FromDays(2)) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromDays(4), TimeSpan.FromMilliseconds(100)); + .ShouldBe(TimeSpan.FromDays(4), TimeSpan.FromMilliseconds(100)); } [Fact] @@ -153,21 +151,21 @@ public void OperatorMinus() { (Duration.FromDays(4) - Duration.FromDays(2)) .ToTimeSpan() - .Should().BeCloseTo(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); + .ShouldBe(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); } [Fact] public void OperatorGreater() { (Duration.FromDays(4) > Duration.FromDays(2)) - .Should().BeTrue(); + .ShouldBeTrue(); } [Fact] public void OperatorLess() { (Duration.FromDays(4) < Duration.FromDays(2)) - .Should().BeFalse(); + .ShouldBeFalse(); } // This is for diagnostic purposes when tests run on different operating systems. diff --git a/BitFaster.Caching.UnitTests/ExpireAfterAccessTests.cs b/BitFaster.Caching.UnitTests/ExpireAfterAccessTests.cs index 4c64737e..f91e21e7 100644 --- a/BitFaster.Caching.UnitTests/ExpireAfterAccessTests.cs +++ b/BitFaster.Caching.UnitTests/ExpireAfterAccessTests.cs @@ -1,5 +1,4 @@ -using System; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -17,25 +16,25 @@ public ExpireAfterAccessTests() [Fact] public void TimeToExpireReturnsCtorArg() { - expiryCalculator.TimeToExpire.Should().Be(expiry.ToTimeSpan()); + expiryCalculator.TimeToExpire.ShouldBe(expiry.ToTimeSpan()); } [Fact] public void AfterCreateReturnsTimeToExpire() { - expiryCalculator.GetExpireAfterCreate(1, 2).Should().Be(expiry); + expiryCalculator.GetExpireAfterCreate(1, 2).ShouldBe(expiry); } [Fact] public void AfteReadReturnsTimeToExpire() { - expiryCalculator.GetExpireAfterRead(1, 2, Duration.SinceEpoch()).Should().Be(expiry); + expiryCalculator.GetExpireAfterRead(1, 2, Duration.SinceEpoch()).ShouldBe(expiry); } [Fact] public void AfteUpdateReturnsTimeToExpire() { - expiryCalculator.GetExpireAfterUpdate(1, 2, Duration.SinceEpoch()).Should().Be(expiry); + expiryCalculator.GetExpireAfterUpdate(1, 2, Duration.SinceEpoch()).ShouldBe(expiry); } } } diff --git a/BitFaster.Caching.UnitTests/ExpireAfterWriteTests.cs b/BitFaster.Caching.UnitTests/ExpireAfterWriteTests.cs index 57774eed..84d98b0c 100644 --- a/BitFaster.Caching.UnitTests/ExpireAfterWriteTests.cs +++ b/BitFaster.Caching.UnitTests/ExpireAfterWriteTests.cs @@ -1,5 +1,4 @@ -using System; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -17,26 +16,26 @@ public ExpireAfterWriteTests() [Fact] public void TimeToExpireReturnsCtorArg() { - expiryCalculator.TimeToExpire.Should().Be(expiry.ToTimeSpan()); + expiryCalculator.TimeToExpire.ShouldBe(expiry.ToTimeSpan()); } [Fact] public void AfterCreateReturnsTimeToExpire() { - expiryCalculator.GetExpireAfterCreate(1, 2).Should().Be(expiry); + expiryCalculator.GetExpireAfterCreate(1, 2).ShouldBe(expiry); } [Fact] public void AfteReadReturnsCurrentTimeToExpire() { var current = new Duration(123); - expiryCalculator.GetExpireAfterRead(1, 2, current).Should().Be(current); + expiryCalculator.GetExpireAfterRead(1, 2, current).ShouldBe(current); } [Fact] public void AfteUpdateReturnsTimeToExpire() { - expiryCalculator.GetExpireAfterUpdate(1, 2, Duration.SinceEpoch()).Should().Be(expiry); + expiryCalculator.GetExpireAfterUpdate(1, 2, Duration.SinceEpoch()).ShouldBe(expiry); } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs index ba32b42e..879ae7c9 100644 --- a/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs @@ -1,7 +1,6 @@ - -using System.Collections.Generic; +using System.Collections.Generic; using BitFaster.Caching.Lfu; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -40,11 +39,11 @@ public void Repro() } } - baseline.Size.Should().Be(sketch.Size); + baseline.Size.ShouldBe(sketch.Size); for (int i = 0; i < 1_048_576; i++) { - sketch.EstimateFrequency(i).Should().Be(baseline.EstimateFrequency(i)); + sketch.EstimateFrequency(i).ShouldBe(baseline.EstimateFrequency(i)); } } @@ -54,14 +53,14 @@ public void WhenCapacityIsZeroDefaultsSelected() { sketch = new CmSketchCore(0, EqualityComparer.Default); - sketch.ResetSampleSize.Should().Be(10); + sketch.ResetSampleSize.ShouldBe(10); } [SkippableFact] public void WhenIncrementedOnceCountIsOne() { sketch.Increment(1); - sketch.EstimateFrequency(1).Should().Be(1); + sketch.EstimateFrequency(1).ShouldBe(1); } [SkippableFact] @@ -72,7 +71,7 @@ public void WhenIncrementedMoreThanMaxCountIsMaximum() sketch.Increment(1); } - sketch.EstimateFrequency(1).Should().Be(15); + sketch.EstimateFrequency(1).ShouldBe(15); } [SkippableFact] @@ -82,8 +81,8 @@ public void WhenTwoItemsIncrementedCountIsIndependent() sketch.Increment(1); sketch.Increment(2); - sketch.EstimateFrequency(1).Should().Be(2); - sketch.EstimateFrequency(2).Should().Be(1); + sketch.EstimateFrequency(1).ShouldBe(2); + sketch.EstimateFrequency(2).ShouldBe(1); } [SkippableFact] @@ -99,15 +98,15 @@ public void WhenSampleSizeExceededCountIsReset() if (sketch.Size != i) { - i.Should().NotBe(1, "sketch should not be reset on the first iteration. Resize logic is broken"); + i.ShouldNotBe(1, "sketch should not be reset on the first iteration. Resize logic is broken"); reset = true; break; } } - reset.Should().BeTrue(); - sketch.Size.Should().BeLessThan(10 * 64); + reset.ShouldBeTrue(); + sketch.Size.ShouldBeLessThan(10 * 64); } [SkippableFact] @@ -119,8 +118,8 @@ public void WhenClearedCountIsReset() sketch.Clear(); - sketch.EstimateFrequency(1).Should().Be(0); - sketch.EstimateFrequency(2).Should().Be(0); + sketch.EstimateFrequency(1).ShouldBe(0); + sketch.EstimateFrequency(2).ShouldBe(0); } [SkippableFact] @@ -150,19 +149,19 @@ public void HeavyHitters() { if ((i == 0) || (i == 1) || (i == 3) || (i == 5) || (i == 7) || (i == 9)) { - popularity[i].Should().BeLessThanOrEqualTo(popularity[2]); + popularity[i].ShouldBeLessThanOrEqualTo(popularity[2]); } else if (i == 2) { - popularity[2].Should().BeLessThanOrEqualTo(popularity[4]); + popularity[2].ShouldBeLessThanOrEqualTo(popularity[4]); } else if (i == 4) { - popularity[4].Should().BeLessThanOrEqualTo(popularity[6]); + popularity[4].ShouldBeLessThanOrEqualTo(popularity[6]); } else if (i == 6) { - popularity[6].Should().BeLessThanOrEqualTo(popularity[8]); + popularity[6].ShouldBeLessThanOrEqualTo(popularity[8]); } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuBuilderTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuBuilderTests.cs index 343a7958..46c7098f 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuBuilderTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuBuilderTests.cs @@ -2,7 +2,7 @@ using BitFaster.Caching.Atomic; using BitFaster.Caching.Lfu; using BitFaster.Caching.Scheduler; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -17,7 +17,7 @@ public void TestConcurrencyLevel() Action constructor = () => { var x = b.Build(); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -27,7 +27,7 @@ public void TestIntCapacity() .WithCapacity(3) .Build(); - lfu.Policy.Eviction.Value.Capacity.Should().Be(3); + lfu.Policy.Eviction.Value.Capacity.ShouldBe(3); } [Fact] @@ -38,7 +38,7 @@ public void TestScheduler() .Build(); var clfu = lfu as ConcurrentLfu; - clfu.Scheduler.Should().BeOfType(); + clfu.Scheduler.ShouldBeOfType(); } [Fact] @@ -49,7 +49,7 @@ public void TestComparer() .Build(); lfu.GetOrAdd("a", k => 1); - lfu.TryGet("A", out var value).Should().BeTrue(); + lfu.TryGet("A", out var value).ShouldBeTrue(); } [Fact] @@ -59,9 +59,9 @@ public void TestExpireAfterAccess() .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) .Build(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -72,7 +72,7 @@ public void TestExpireAfterReadAndExpireAfterWriteThrows() .WithExpireAfterWrite(TimeSpan.FromSeconds(2)); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -82,10 +82,10 @@ public void TestExpireAfter() .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) .Build(); - expireAfter.Policy.ExpireAfter.HasValue.Should().BeTrue(); + expireAfter.Policy.ExpireAfter.HasValue.ShouldBeTrue(); - expireAfter.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfter.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -96,10 +96,10 @@ public void TestAsyncExpireAfter() .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) .Build(); - expireAfter.Policy.ExpireAfter.HasValue.Should().BeTrue(); + expireAfter.Policy.ExpireAfter.HasValue.ShouldBeTrue(); - expireAfter.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfter.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } @@ -111,7 +111,7 @@ public void TestExpireAfterWriteAndExpireAfterThrows() .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -122,7 +122,7 @@ public void TestExpireAfterAccessAndExpireAfterThrows() .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -134,7 +134,7 @@ public void TestExpireAfterAccessAndWriteAndExpireAfterThrows() .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -145,7 +145,7 @@ public void TestScopedWithExpireAfterThrows() .AsScopedCache(); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -157,7 +157,7 @@ public void TestScopedAtomicWithExpireAfterThrows() .WithAtomicGetOrAdd(); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -169,7 +169,7 @@ public void TestAsyncScopedWithExpireAfterThrows() .AsScopedCache(); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -182,7 +182,7 @@ public void TestAsyncScopedAtomicWithExpireAfterThrows() .WithAtomicGetOrAdd(); Action act = () => builder.Build(); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] @@ -193,9 +193,9 @@ public void TestScopedWithExpireAfterWrite() .AsScopedCache() .Build(); - expireAfterWrite.Policy.ExpireAfterWrite.HasValue.Should().BeTrue(); - expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterWrite.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); + expireAfterWrite.Policy.ExpireAfterWrite.HasValue.ShouldBeTrue(); + expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterWrite.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); } [Fact] @@ -206,9 +206,9 @@ public void TestScopedWithExpireAfterAccess() .AsScopedCache() .Build(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -219,9 +219,9 @@ public void TestAtomicWithExpireAfterWrite() .WithAtomicGetOrAdd() .Build(); - expireAfterWrite.Policy.ExpireAfterWrite.HasValue.Should().BeTrue(); - expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterWrite.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); + expireAfterWrite.Policy.ExpireAfterWrite.HasValue.ShouldBeTrue(); + expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterWrite.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); } [Fact] @@ -232,9 +232,9 @@ public void TestAtomicWithExpireAfterAccess() .WithAtomicGetOrAdd() .Build(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -246,9 +246,9 @@ public void TestScopedAtomicWithExpireAfterWrite() .WithAtomicGetOrAdd() .Build(); - expireAfterWrite.Policy.ExpireAfterWrite.HasValue.Should().BeTrue(); - expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterWrite.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); + expireAfterWrite.Policy.ExpireAfterWrite.HasValue.ShouldBeTrue(); + expireAfterWrite.Policy.ExpireAfterWrite.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterWrite.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); } [Fact] @@ -260,9 +260,9 @@ public void TestScopedAtomicWithExpireAfterAccess() .WithAtomicGetOrAdd() .Build(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } // 1 @@ -274,8 +274,8 @@ public void WithScopedValues() .WithCapacity(3) .Build(); - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); } // 2 @@ -287,7 +287,7 @@ public void WithAtomicFactory() .WithCapacity(3) .Build(); - lru.Should().BeOfType>(); + lru.ShouldBeOfType>(); } // 3 @@ -299,7 +299,7 @@ public void AsAsync() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 4 @@ -312,8 +312,8 @@ public void WithAtomicWithScope() .WithCapacity(3) .Build(); - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); } // 5 @@ -326,8 +326,8 @@ public void WithScopedWithAtomic() .WithCapacity(3) .Build(); - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); } // 6 @@ -340,9 +340,9 @@ public void AsAsyncWithScoped() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); } // 7 @@ -355,8 +355,8 @@ public void WithScopedAsAsync() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); + lru.ShouldBeAssignableTo>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); } // 8 @@ -369,7 +369,7 @@ public void WithAtomicAsAsync() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 9 @@ -382,7 +382,7 @@ public void AsAsyncWithAtomic() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 10 @@ -396,7 +396,7 @@ public void WithAtomicWithScopedAsAsync() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 11 @@ -410,7 +410,7 @@ public void WithAtomicAsAsyncWithScoped() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 12 @@ -424,7 +424,7 @@ public void WithScopedWithAtomicAsAsync() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 13 @@ -438,7 +438,7 @@ public void WithScopedAsAsyncWithAtomic() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 14 @@ -452,7 +452,7 @@ public void AsAsyncWithScopedWithAtomic() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } // 15 @@ -466,7 +466,7 @@ public void AsAsyncWithAtomicWithScoped() .WithCapacity(3) .Build(); - lru.Should().BeAssignableTo>(); + lru.ShouldBeAssignableTo>(); } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuCoreTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuCoreTests.cs index f3318658..e9506e03 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuCoreTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuCoreTests.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; using BitFaster.Caching.Lfu; using BitFaster.Caching.UnitTests.Retry; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -29,7 +29,7 @@ public ConcurrentLfuCoreTests() [Fact] public void EvictionPolicyCapacityReturnsCapacity() { - lfu.Policy.Eviction.Value.Capacity.Should().Be(capacity); + lfu.Policy.Eviction.Value.Capacity.ShouldBe(capacity); } [Fact] @@ -38,8 +38,8 @@ public void WhenKeyIsRequestedItIsCreatedAndCached() var result1 = lfu.GetOrAdd(1, valueFactory.Create); var result2 = lfu.GetOrAdd(1, valueFactory.Create); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } #if NETCOREAPP3_0_OR_GREATER [Fact] @@ -48,8 +48,8 @@ public void WhenKeyIsRequestedWithArgItIsCreatedAndCached() var result1 = lfu.GetOrAdd(1, valueFactory.Create, 9); var result2 = lfu.GetOrAdd(1, valueFactory.Create, 17); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } #endif [Fact] @@ -59,8 +59,8 @@ public async Task WhenKeyIsRequesteItIsCreatedAndCachedAsync() var result1 = await asyncLfu.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await asyncLfu.GetOrAddAsync(1, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } #if NETCOREAPP3_0_OR_GREATER @@ -71,8 +71,8 @@ public async Task WhenKeyIsRequestedWithArgItIsCreatedAndCachedAsync() var result1 = await asyncLfu.GetOrAddAsync(1, valueFactory.CreateAsync, 9); var result2 = await asyncLfu.GetOrAddAsync(1, valueFactory.CreateAsync, 17); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } #endif @@ -82,8 +82,8 @@ public void WhenItemIsUpdatedItIsUpdated() lfu.GetOrAdd(1, k => k); lfu.AddOrUpdate(1, 2); - lfu.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + lfu.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } [RetryFact] @@ -91,8 +91,8 @@ public void WhenItemDoesNotExistUpdatedAddsItem() { lfu.AddOrUpdate(1, 2); - lfu.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + lfu.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } @@ -101,8 +101,8 @@ public void WhenKeyExistsTryRemoveRemovesItem() { lfu.GetOrAdd(1, k => k); - lfu.TryRemove(1).Should().BeTrue(); - lfu.TryGet(1, out _).Should().BeFalse(); + lfu.TryRemove(1).ShouldBeTrue(); + lfu.TryGet(1, out _).ShouldBeFalse(); } #if NETCOREAPP3_0_OR_GREATER @@ -111,8 +111,8 @@ public void WhenKeyExistsTryRemoveReturnsValue() { lfu.GetOrAdd(1, valueFactory.Create); - lfu.TryRemove(1, out var value).Should().BeTrue(); - value.Should().Be(1); + lfu.TryRemove(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -120,8 +120,8 @@ public void WhenItemExistsTryRemoveRemovesItem() { lfu.GetOrAdd(1, k => k); - lfu.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); - lfu.TryGet(1, out _).Should().BeFalse(); + lfu.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); + lfu.TryGet(1, out _).ShouldBeFalse(); } [Fact] @@ -129,8 +129,8 @@ public void WhenItemDoesntMatchTryRemoveDoesNotRemove() { lfu.GetOrAdd(1, k => k); - lfu.TryRemove(new KeyValuePair(1, 2)).Should().BeFalse(); - lfu.TryGet(1, out var value).Should().BeTrue(); + lfu.TryRemove(new KeyValuePair(1, 2)).ShouldBeFalse(); + lfu.TryGet(1, out var value).ShouldBeTrue(); } #endif @@ -142,8 +142,8 @@ public void WhenClearedCacheIsEmpty() lfu.Clear(); - lfu.Count.Should().Be(0); - lfu.TryGet(1, out var _).Should().BeFalse(); + lfu.Count.ShouldBe(0); + lfu.TryGet(1, out var _).ShouldBeFalse(); } [Fact] @@ -155,12 +155,12 @@ public void TrimRemovesNItems() } DoMaintenance(lfu); - lfu.Count.Should().Be(20); + lfu.Count.ShouldBe(20); lfu.Policy.Eviction.Value.Trim(5); DoMaintenance(lfu); - lfu.Count.Should().Be(15); + lfu.Count.ShouldBe(15); } [Fact] @@ -170,10 +170,10 @@ public void WhenItemsAddedGenericEnumerateContainsKvps() lfu.GetOrAdd(2, k => k); var enumerator = lfu.GetEnumerator(); - enumerator.MoveNext().Should().BeTrue(); - enumerator.Current.Should().Be(new KeyValuePair(1, 1)); - enumerator.MoveNext().Should().BeTrue(); - enumerator.Current.Should().Be(new KeyValuePair(2, 2)); + enumerator.MoveNext().ShouldBeTrue(); + enumerator.Current.ShouldBe(new KeyValuePair(1, 1)); + enumerator.MoveNext().ShouldBeTrue(); + enumerator.Current.ShouldBe(new KeyValuePair(2, 2)); } [Fact] @@ -183,7 +183,7 @@ public void WhenItemsAddedEnumerateContainsKvps() lfu.GetOrAdd(2, k => k); var enumerable = (IEnumerable)lfu; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, 1), new KeyValuePair(2, 2) }); } } diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs index 9c9f29f9..c30418f7 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs @@ -7,7 +7,7 @@ using BitFaster.Caching.Buffers; using BitFaster.Caching.Lfu; using BitFaster.Caching.Scheduler; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -107,7 +107,7 @@ public void VerifyMisses() this.output.WriteLine($"Cache misses {cache.Metrics.Value.Misses} (sampled {samplePercent}%)"); this.output.WriteLine($"Maintenance ops {cache.Scheduler.RunCount}"); - cache.Metrics.Value.Misses.Should().Be(iterations); + cache.Metrics.Value.Misses.ShouldBe(iterations); } private void VerifyHits(ConcurrentLfu cache, int iterations, int minSamples) @@ -140,7 +140,7 @@ private void VerifyHits(ConcurrentLfu cache, int iterations, int minSa this.output.WriteLine($"Error: {cache.Scheduler.LastException.Value}"); } - cache.Metrics.Value.Hits.Should().BeGreaterThanOrEqualTo(minSamples); + cache.Metrics.Value.Hits.ShouldBeGreaterThanOrEqualTo(minSamples); // verify this doesn't block or throw var b = cache.Scheduler as BackgroundThreadScheduler; @@ -287,7 +287,7 @@ await Threaded.Run(threads, i => this.output.WriteLine($"Cache misses {cache.Metrics.Value.Misses} (sampled {samplePercent}%)"); this.output.WriteLine($"Maintenance ops {cache.Scheduler.RunCount}"); - cache.Metrics.Value.Misses.Should().Be(loopIterations * threads); + cache.Metrics.Value.Misses.ShouldBe(loopIterations * threads); RunIntegrityCheck(cache, this.output); } @@ -308,7 +308,7 @@ public async Task WhenConcurrentUpdateAndRemoveKvp() for (var i = 0; i < 100_000; i++) { cache.AddOrUpdate(5, "a"); - cache.TryGet(5, out _).Should().BeTrue("key 'a' should not be deleted"); + cache.TryGet(5, out _).ShouldBeTrue("key 'a' should not be deleted"); cache.AddOrUpdate(5, "x"); } @@ -422,8 +422,8 @@ public void Validate(ITestOutputHelper output) cache.DoMaintenance(); // buffers should be empty after maintenance - this.readBuffer.Count.Should().Be(0); - this.writeBuffer.Count.Should().Be(0); + this.readBuffer.Count.ShouldBe(0); + this.writeBuffer.Count.ShouldBe(0); // all the items in the LRUs must exist in the dictionary. // no items should be marked as removed after maintenance has run @@ -435,7 +435,7 @@ public void Validate(ITestOutputHelper output) VerifyDictionaryInLrus(); // cache must be within capacity - cache.Count.Should().BeLessThanOrEqualTo(cache.Capacity, "capacity out of valid range"); + cache.Count.ShouldBeLessThanOrEqualTo(cache.Capacity, "capacity out of valid range"); } private void VerifyLruInDictionary(LfuNodeList lfuNodes, ITestOutputHelper output) @@ -444,10 +444,10 @@ private void VerifyLruInDictionary(LfuNodeList lfuNodes, ITestOutputHelper while (node != null) { - node.WasRemoved.Should().BeFalse(); - node.WasDeleted.Should().BeFalse(); + node.WasRemoved.ShouldBeFalse(); + node.WasDeleted.ShouldBeFalse(); - cache.TryGet(node.Key, out _).Should().BeTrue($"Orphaned node with key {node.Key} detected."); + cache.TryGet(node.Key, out _).ShouldBeTrue($"Orphaned node with key {node.Key} detected."); node = node.Next; } @@ -458,7 +458,7 @@ private void VerifyDictionaryInLrus() foreach (var kvp in this.cache) { var exists = Exists(kvp, this.windowLru) || Exists(kvp, this.probationLru) || Exists(kvp, this.protectedLru); - exists.Should().BeTrue($"key {kvp.Key} must exist in LRU lists"); + exists.ShouldBeTrue($"key {kvp.Key} must exist in LRU lists"); } } diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs index 0f3756bc..f14d4a29 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs @@ -1,14 +1,12 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Threading.Tasks; -using BitFaster.Caching.Buffers; using BitFaster.Caching.Lfu; using BitFaster.Caching.Scheduler; using BitFaster.Caching.UnitTests.Lru; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -31,7 +29,7 @@ public void WhenCapacityIsLessThan3CtorThrows() { Action constructor = () => { var x = new ConcurrentLfu(2); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -39,7 +37,7 @@ public void WhenCapacityIsValidCacheIsCreated() { var x = new ConcurrentLfu(3); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -47,14 +45,14 @@ public void WhenConcurrencyIsLessThan1CtorThrows() { Action constructor = () => { var x = new ConcurrentLfu(0, 20, new ForegroundScheduler(), EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void DefaultSchedulerIsThreadPool() { var cache = new ConcurrentLfu(20); - cache.Scheduler.Should().BeOfType(); + cache.Scheduler.ShouldBeOfType(); } [Fact] @@ -63,8 +61,8 @@ public void WhenKeyIsRequestedItIsCreatedAndCached() var result1 = cache.GetOrAdd(1, valueFactory.Create); var result2 = cache.GetOrAdd(1, valueFactory.Create); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -73,8 +71,8 @@ public void WhenKeyIsRequestedWithArgItIsCreatedAndCached() var result1 = cache.GetOrAdd(1, valueFactory.Create, 9); var result2 = cache.GetOrAdd(1, valueFactory.Create, 17); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -83,8 +81,8 @@ public async Task WhenKeyIsRequesteItIsCreatedAndCachedAsync() var result1 = await cache.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await cache.GetOrAddAsync(1, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -93,8 +91,8 @@ public async Task WhenKeyIsRequestedWithArgItIsCreatedAndCachedAsync() var result1 = await cache.GetOrAddAsync(1, valueFactory.CreateAsync, 9); var result2 = await cache.GetOrAddAsync(1, valueFactory.CreateAsync, 17); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -113,7 +111,7 @@ public void WhenItemsAddedExceedsCapacityItemsAreDiscarded() cache.DoMaintenance(); LogLru(); - cache.Count.Should().Be(20); + cache.Count.ShouldBe(20); } [Fact] @@ -131,8 +129,8 @@ public void WhenItemIsEvictedItIsDisposed() dcache.DoMaintenance(); LogLru(); - dcache.Count.Should().Be(20); - disposables.Count(d => d.IsDisposed).Should().Be(5); + dcache.Count.ShouldBe(20); + disposables.Count(d => d.IsDisposed).ShouldBe(5); } // protected 15 @@ -185,16 +183,16 @@ public void WhenNewItemsAreAddedTheyArePromotedBasedOnFrequency() // W[24] Protected[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 21, 22, 23] Probation[1, 2, 3, 25] // W[24] Protected[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 21, 22, 23, 25] Probation[1, 2, 3, 4] - cache.Count.Should().Be(20); + cache.Count.ShouldBe(20); // W [24] Protected [5,6,7,8,9,10,11,12,13,14,20,21,22,23,25] Probation [] cache.Trim(4); cache.DoMaintenance(); LogLru(); - cache.TryGet(1, out var value1).Should().BeFalse(); - cache.TryGet(2, out var value2).Should().BeFalse(); - cache.Count.Should().Be(16); + cache.TryGet(1, out var value1).ShouldBeFalse(); + cache.TryGet(2, out var value2).ShouldBeFalse(); + cache.Count.ShouldBe(16); } [Fact] @@ -230,7 +228,7 @@ public void ReadPromotesProbation() cache.DoMaintenance(); LogLru(); - cache.TryGet(16, out var value1).Should().BeTrue(); + cache.TryGet(16, out var value1).ShouldBeTrue(); } // when probation item is written it is moved to protected @@ -247,7 +245,7 @@ public void WritePromotesProbation() LogLru(); // W [24] Protected [16] Probation [2,6,7,8,9,10,11,12,13,14,15,17,18,19,20,21,22,23] - cache.TryUpdate(16, -16).Should().BeTrue(); + cache.TryUpdate(16, -16).ShouldBeTrue(); cache.DoMaintenance(); LogLru(); @@ -267,7 +265,7 @@ public void WritePromotesProbation() cache.DoMaintenance(); LogLru(); - cache.TryGet(16, out var value1).Should().BeTrue(); + cache.TryGet(16, out var value1).ShouldBeTrue(); } [Fact] @@ -302,7 +300,7 @@ public void ReadUpdatesProtectedLruOrder() cache.DoMaintenance(); LogLru(); - cache.TryGet(7, out var _).Should().BeTrue(); + cache.TryGet(7, out var _).ShouldBeTrue(); } [Fact] @@ -327,7 +325,7 @@ public void WriteUpdatesProtectedLruOrder() // W [19] Protected [8,9,7] Probation [0,1,2,3,4,5,6,10,11,12,13,14,15,16,17,18] // element 7 now moved to back of LRU - cache.TryUpdate(7, -7).Should().BeTrue(); + cache.TryUpdate(7, -7).ShouldBeTrue(); cache.DoMaintenance(); LogLru(); @@ -337,7 +335,7 @@ public void WriteUpdatesProtectedLruOrder() cache.DoMaintenance(); LogLru(); - cache.TryGet(7, out var _).Should().BeTrue(); + cache.TryGet(7, out var _).ShouldBeTrue(); } [Fact] @@ -402,8 +400,8 @@ public void WhenHitRateChangesWindowSizeIsAdapted() cache.DoMaintenance(); LogLru(); - cache.TryGet(666, out var _).Should().BeTrue(); - cache.TryGet(667, out var _).Should().BeTrue(); + cache.TryGet(666, out var _).ShouldBeTrue(); + cache.TryGet(667, out var _).ShouldBeTrue(); this.output.WriteLine($"Scheduler ran {cache.Scheduler.RunCount} times."); } @@ -415,18 +413,18 @@ public void ReadSchedulesMaintenanceWhenBufferIsFull() cache = new ConcurrentLfu(1, 20, scheduler, EqualityComparer.Default); cache.GetOrAdd(1, k => k); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); cache.DoMaintenance(); for (int i = 0; i < ConcurrentLfu.DefaultBufferSize; i++) { - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); cache.GetOrAdd(1, k => k); } // read buffer is now full, next read triggers maintenance cache.GetOrAdd(1, k => k); - scheduler.RunCount.Should().Be(2); + scheduler.RunCount.ShouldBe(2); } [Fact] @@ -436,7 +434,7 @@ public void WhenReadBufferIsFullReadsAreDropped() cache = new ConcurrentLfu(1, 20, scheduler, EqualityComparer.Default); cache.GetOrAdd(1, k => k); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); cache.DoMaintenance(); for (int i = 0; i < ConcurrentLfu.DefaultBufferSize * 2; i++) @@ -446,7 +444,7 @@ public void WhenReadBufferIsFullReadsAreDropped() cache.DoMaintenance(); - cache.Metrics.Value.Hits.Should().Be(ConcurrentLfu.DefaultBufferSize); + cache.Metrics.Value.Hits.ShouldBe(ConcurrentLfu.DefaultBufferSize); } [Fact] @@ -462,7 +460,7 @@ public void WhenWriteBufferIsFullAddDoesMaintenance() cache.DoMaintenance(); // remove the item but don't flush, it is now in the write buffer and maintenance is scheduled - cache.TryRemove(-1).Should().BeTrue(); + cache.TryRemove(-1).ShouldBeTrue(); // add buffer size items, last iteration will invoke maintenance on the foreground since write // buffer is full and test scheduler did not do any work @@ -473,7 +471,7 @@ public void WhenWriteBufferIsFullAddDoesMaintenance() // pending write (to remove -1) should be flushed by the 128th write calling maintenance // directly within AfterWrite - cache.TryGet(-1, out var _).Should().BeFalse(); + cache.TryGet(-1, out var _).ShouldBeFalse(); } // backcompat: remove conditional compile @@ -487,7 +485,7 @@ public void WhenWriteBufferIsFullUpdatesAreDropped() cache = new ConcurrentLfu(1, capacity, scheduler, EqualityComparer.Default); cache.GetOrAdd(1, k => k); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); cache.DoMaintenance(); for (int i = 0; i < bufferSize * 2; i++) @@ -497,32 +495,32 @@ public void WhenWriteBufferIsFullUpdatesAreDropped() cache.DoMaintenance(); - cache.Metrics.Value.Updated.Should().Be(bufferSize); + cache.Metrics.Value.Updated.ShouldBe(bufferSize); } #endif [Fact] public void EvictionPolicyReturnsCapacity() { - cache.Policy.Eviction.Value.Capacity.Should().Be(20); + cache.Policy.Eviction.Value.Capacity.ShouldBe(20); } [Fact] public void ExpireAfterWriteIsDisabled() { - cache.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + cache.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] public void EventsAreDisabled() { - cache.Events.HasValue.Should().BeFalse(); + cache.Events.HasValue.ShouldBeFalse(); } [Fact] public void MetricsAreEnabled() { - cache.Metrics.HasValue.Should().BeTrue(); + cache.Metrics.HasValue.ShouldBeTrue(); } [Fact] @@ -533,9 +531,9 @@ public void WhenItemIsAddedThenRetrievedMetricHitRatioIsHalf() cache.DoMaintenance(); - cache.Metrics.Value.HitRatio.Should().Be(0.5); - cache.Metrics.Value.Hits.Should().Be(1); - cache.Metrics.Value.Misses.Should().Be(1); + cache.Metrics.Value.HitRatio.ShouldBe(0.5); + cache.Metrics.Value.Hits.ShouldBe(1); + cache.Metrics.Value.Misses.ShouldBe(1); } [Fact] @@ -553,41 +551,41 @@ public void WhenItemIsEvictedMetricRecordsCount() cache.DoMaintenance(); - cache.Metrics.Value.Evicted.Should().Be(5); + cache.Metrics.Value.Evicted.ShouldBe(5); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.GetOrAdd(1, k => k); cache.GetOrAdd(2, k => k); - cache.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + cache.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] public void WhenItemsAddedGenericEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.GetOrAdd(1, k => k + 1); cache.GetOrAdd(2, k => k + 1); var enumerator = cache.GetEnumerator(); - enumerator.MoveNext().Should().BeTrue(); - enumerator.Current.Should().Be(new KeyValuePair(1, 2)); - enumerator.MoveNext().Should().BeTrue(); - enumerator.Current.Should().Be(new KeyValuePair(2, 3)); + enumerator.MoveNext().ShouldBeTrue(); + enumerator.Current.ShouldBe(new KeyValuePair(1, 2)); + enumerator.MoveNext().ShouldBeTrue(); + enumerator.Current.ShouldBe(new KeyValuePair(2, 3)); } [Fact] public void WhenItemsAddedEnumerateContainsKvps() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.GetOrAdd(1, k => k + 1); cache.GetOrAdd(2, k => k + 1); var enumerable = (IEnumerable)cache; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, 2), new KeyValuePair(2, 3) }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, 2), new KeyValuePair(2, 3) }); } [Fact] @@ -596,8 +594,8 @@ public void WhenItemIsUpdatedItIsUpdated() cache.GetOrAdd(1, k => k); cache.AddOrUpdate(1, 2); - cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } [Fact] @@ -609,8 +607,8 @@ public void WhenKeyExistsAddOrUpdateGuidUpdatesExistingItem() lfu2.AddOrUpdate(1, new Guid(1, 0, 0, b)); lfu2.AddOrUpdate(1, new Guid(2, 0, 0, b)); - lfu2.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(new Guid(2, 0, 0, b)); + lfu2.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(new Guid(2, 0, 0, b)); } [Fact] @@ -618,8 +616,8 @@ public void WhenItemDoesNotExistUpdatedAddsItem() { cache.AddOrUpdate(1, 2); - cache.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(2); + cache.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(2); } [Fact] @@ -627,8 +625,8 @@ public void WhenKeyExistsTryRemoveRemovesItem() { cache.GetOrAdd(1, k => k); - cache.TryRemove(1).Should().BeTrue(); - cache.TryGet(1, out _).Should().BeFalse(); + cache.TryRemove(1).ShouldBeTrue(); + cache.TryGet(1, out _).ShouldBeFalse(); } [Fact] @@ -636,8 +634,8 @@ public void WhenKeyExistsTryRemoveReturnsValue() { cache.GetOrAdd(1, valueFactory.Create); - cache.TryRemove(1, out var value).Should().BeTrue(); - value.Should().Be(1); + cache.TryRemove(1, out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -645,8 +643,8 @@ public void WhenItemExistsTryRemoveRemovesItem() { cache.GetOrAdd(1, k => k); - cache.TryRemove(new KeyValuePair(1, 1)).Should().BeTrue(); - cache.TryGet(1, out _).Should().BeFalse(); + cache.TryRemove(new KeyValuePair(1, 1)).ShouldBeTrue(); + cache.TryGet(1, out _).ShouldBeFalse(); } [Fact] @@ -654,8 +652,8 @@ public void WhenItemDoesntMatchTryRemoveDoesNotRemove() { cache.GetOrAdd(1, k => k); - cache.TryRemove(new KeyValuePair(1, 2)).Should().BeFalse(); - cache.TryGet(1, out var value).Should().BeTrue(); + cache.TryRemove(new KeyValuePair(1, 2)).ShouldBeFalse(); + cache.TryGet(1, out var value).ShouldBeTrue(); } [Fact] @@ -666,10 +664,10 @@ public void WhenItemIsRemovedItIsDisposed() dcache.GetOrAdd(1, k => disposable); - dcache.TryRemove(1).Should().BeTrue(); + dcache.TryRemove(1).ShouldBeTrue(); dcache.DoMaintenance(); - disposable.IsDisposed.Should().BeTrue(); + disposable.IsDisposed.ShouldBeTrue(); } [Fact] @@ -677,16 +675,16 @@ public void WhenItemIsRemovedEvictionCountIsIncremented() { cache.GetOrAdd(1, k => k); - cache.TryRemove(1).Should().BeTrue(); + cache.TryRemove(1).ShouldBeTrue(); cache.DoMaintenance(); - cache.Metrics.Value.Evicted.Should().Be(1); + cache.Metrics.Value.Evicted.ShouldBe(1); } [Fact] public void WhenItemDoesNotExistTryRemoveIsFalse() { - cache.TryRemove(1).Should().BeFalse(); + cache.TryRemove(1).ShouldBeFalse(); } // OnWrite handles the case where a node is removed while the write buffer contains the node @@ -702,17 +700,17 @@ public void WhenRemovedInWriteBuffer() cache.TryUpdate(1, 2); // immediately remove - cache.TryRemove(1).Should().BeTrue(); + cache.TryRemove(1).ShouldBeTrue(); cache.DoMaintenance(); - cache.TryGet(1, out var _).Should().BeFalse(); + cache.TryGet(1, out var _).ShouldBeFalse(); } [Fact] public void WhenItemDoesNotExistTryUpdateIsFalse() { - cache.TryUpdate(1, 2).Should().BeFalse(); + cache.TryUpdate(1, 2).ShouldBeFalse(); } [Fact] @@ -720,9 +718,9 @@ public void WhenAddingNullValueCanBeAddedAndRemoved() { // use foreground so that any null ref exceptions will surface var lfu = new ConcurrentLfu(1, 20, new ForegroundScheduler(), EqualityComparer.Default); - lfu.GetOrAdd(1, _ => null).Should().BeNull(); + lfu.GetOrAdd(1, _ => null).ShouldBeNull(); lfu.AddOrUpdate(1, null); - lfu.TryRemove(1).Should().BeTrue(); + lfu.TryRemove(1).ShouldBeTrue(); } [Fact] @@ -733,8 +731,8 @@ public void WhenClearedCacheIsEmpty() cache.Clear(); - cache.Count.Should().Be(0); - cache.TryGet(1, out var _).Should().BeFalse(); + cache.Count.ShouldBe(0); + cache.TryGet(1, out var _).ShouldBeFalse(); } [Fact] @@ -755,7 +753,7 @@ public void WhenBackgroundMaintenanceRepeatedReadThenClearResultsInEmpty() } // there should be no iteration of the loop where count != 0 - overflow.Should().Be(0); + overflow.ShouldBe(0); } [Fact] @@ -767,12 +765,12 @@ public void TrimRemovesNItems() } cache.DoMaintenance(); - cache.Count.Should().Be(20); + cache.Count.ShouldBe(20); cache.Trim(5); cache.DoMaintenance(); - cache.Count.Should().Be(15); + cache.Count.ShouldBe(15); } [Fact] @@ -792,8 +790,8 @@ public void TrimWhileItemsInWriteBufferRemovesNItems() cache.DoMaintenance(); // The trim takes effect before all the writes are replayed by the maintenance thread. - cache.Metrics.Value.Evicted.Should().Be(10); - cache.Count.Should().Be(15); + cache.Metrics.Value.Evicted.ShouldBe(10); + cache.Count.ShouldBe(15); this.output.WriteLine($"Count {cache.Count}"); this.output.WriteLine($"Keys {string.Join(",", cache.Keys.Select(k => k.ToString()))}"); diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuSoakTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuSoakTests.cs index 1fa61d3b..f8194523 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuSoakTests.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using BitFaster.Caching.Lfu; using Xunit; diff --git a/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuTests.cs b/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuTests.cs index 59b0ad3f..4f1ddace 100644 --- a/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuTests.cs @@ -1,10 +1,9 @@ using System; using System.Runtime.InteropServices; -using System.Threading; using BitFaster.Caching.Lfu; using BitFaster.Caching.Scheduler; using BitFaster.Caching.UnitTests.Retry; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -50,8 +49,8 @@ public void WhenItemIsAccessedTimeToExpireIsUpdated() TimeSpan.FromSeconds(2), cache => { - cache.TryGet(1, out var value).Should().BeTrue(); - cache.TryGet(1, out value).Should().BeTrue(); + cache.TryGet(1, out var value).ShouldBeTrue(); + cache.TryGet(1, out value).ShouldBeTrue(); } ); } @@ -63,35 +62,35 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue() lfu.GetOrAdd("foo", k => 1); - lfu.TryGet("FOO", out var value).Should().BeTrue(); - value.Should().Be(1); + lfu.TryGet("FOO", out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] public void MetricsHasValueIsTrue() { var x = new ConcurrentTLfu(3, new TestExpiryCalculator()); - x.Metrics.HasValue.Should().BeTrue(); + x.Metrics.HasValue.ShouldBeTrue(); } [Fact] public void EventsHasValueIsFalse() { var x = new ConcurrentTLfu(3, new TestExpiryCalculator()); - x.Events.HasValue.Should().BeFalse(); + x.Events.HasValue.ShouldBeFalse(); } [Fact] public void DefaultSchedulerIsThreadPool() { - lfu.Scheduler.Should().BeOfType(); + lfu.Scheduler.ShouldBeOfType(); } [Fact] public void WhenCalculatorIsAfterWritePolicyIsAfterWrite() { - lfu.Policy.ExpireAfterWrite.HasValue.Should().BeTrue(); - lfu.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(timeToLive); + lfu.Policy.ExpireAfterWrite.HasValue.ShouldBeTrue(); + lfu.Policy.ExpireAfterWrite.Value.TimeToLive.ShouldBe(timeToLive); } [Fact] @@ -99,8 +98,8 @@ public void WhenCalculatorIsAfterAccessPolicyIsAfterAccess() { lfu = new ConcurrentTLfu(capacity, new ExpireAfterAccess(timeToLive)); - lfu.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - lfu.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(timeToLive); + lfu.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + lfu.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(timeToLive); } [Fact] @@ -108,8 +107,8 @@ public void WhenCalculatorIsCustomPolicyIsAfter() { lfu = new ConcurrentTLfu(capacity, new TestExpiryCalculator()); - lfu.Policy.ExpireAfter.HasValue.Should().BeTrue(); - (lfu as ITimePolicy).TimeToLive.Should().Be(TimeSpan.Zero); + lfu.Policy.ExpireAfter.HasValue.ShouldBeTrue(); + (lfu as ITimePolicy).TimeToLive.ShouldBe(TimeSpan.Zero); } [Fact] @@ -121,8 +120,8 @@ public void WhenKeyExistsTryGetTimeToExpireReturnsExpiry() lfu.GetOrAdd(1, k => "1"); - lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out var timeToExpire).Should().BeTrue(); - timeToExpire.Should().BeCloseTo(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(50)); + lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out var timeToExpire).ShouldBeTrue(); + timeToExpire.ShouldBe(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(50)); } [Fact] @@ -130,7 +129,7 @@ public void WhenKeyDoesNotExistTryGetTimeToExpireReturnsFalse() { lfu = new ConcurrentTLfu(capacity, new TestExpiryCalculator()); - lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out _).Should().BeFalse(); + lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out _).ShouldBeFalse(); } [Fact] @@ -138,7 +137,7 @@ public void WhenKeyTypeMismatchTryGetTimeToExpireReturnsFalse() { lfu = new ConcurrentTLfu(capacity, new TestExpiryCalculator()); - lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire("string", out _).Should().BeFalse(); + lfu.Policy.ExpireAfter.Value.TryGetTimeToExpire("string", out _).ShouldBeFalse(); } // policy can expire after write @@ -148,7 +147,7 @@ public void WhenItemIsNotExpiredItIsNotRemoved() { lfu.GetOrAdd(1, valueFactory.Create); - lfu.TryGet(1, out var value).Should().BeTrue(); + lfu.TryGet(1, out var value).ShouldBeTrue(); } [RetryFact] @@ -164,7 +163,7 @@ public void WhenItemIsExpiredItIsRemoved() timeToLive.MultiplyBy(ttlWaitMlutiplier), lfu => { - lfu.TryGet(1, out var value).Should().BeFalse(); + lfu.TryGet(1, out var value).ShouldBeFalse(); } ); } @@ -185,7 +184,7 @@ public void WhenItemIsExpiredItIsRemoved2() // This is a bit flaky below 2 secs pause - seems like it doesnt always // remove the item lfu.Policy.ExpireAfterWrite.Value.TrimExpired(); - lfu.Count.Should().Be(0); + lfu.Count.ShouldBe(0); } ); } @@ -208,7 +207,7 @@ public void WhenItemIsUpdatedTtlIsExtended() // If we defer computing time to the maintenance loop, we // need to call maintenance here for the timestamp to be updated lfu.DoMaintenance(); - lfu.TryGet(1, out var value).Should().BeTrue(); + lfu.TryGet(1, out var value).ShouldBeTrue(); } ); } diff --git a/BitFaster.Caching.UnitTests/Lfu/LfuCapacityPartitionTests.cs b/BitFaster.Caching.UnitTests/Lfu/LfuCapacityPartitionTests.cs index 08e1ba62..6a3f8e19 100644 --- a/BitFaster.Caching.UnitTests/Lfu/LfuCapacityPartitionTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/LfuCapacityPartitionTests.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lfu; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -24,14 +22,14 @@ public void WhenCapacityIsLessThan3CtorThrows() { Action constructor = () => { var partition = new LfuCapacityPartition(2); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void CapacityReturnsCapacity() { var partition = new LfuCapacityPartition(123); - partition.Capacity.Should().Be(123); + partition.Capacity.ShouldBe(123); } [Theory] @@ -41,9 +39,9 @@ public void CtorSetsExpectedCapacity(int capacity, int expectedWindow, int expec { var partition = new LfuCapacityPartition(capacity); - partition.Window.Should().Be(expectedWindow); - partition.Protected.Should().Be(expectedProtected); - partition.Probation.Should().Be(expectedProbation); + partition.Window.ShouldBe(expectedWindow); + partition.Protected.ShouldBe(expectedProtected); + partition.Probation.ShouldBe(expectedProbation); } [Fact] @@ -60,8 +58,8 @@ public void WhenHitRateKeepsDecreasingWindowIsCappedAt80Percent() SetHitRate(partition, metrics, max, 0.1); } - partition.Window.Should().Be(80); - partition.Protected.Should().Be(16); + partition.Window.ShouldBe(80); + partition.Protected.ShouldBe(16); } [Fact] @@ -102,7 +100,7 @@ public void WhenHitRateIsStableWindowConverges() var minWindow = last50.Min(); var maxWindow = last50.Max(); - (maxWindow - minWindow).Should().BeLessThanOrEqualTo(1); + (maxWindow - minWindow).ShouldBeLessThanOrEqualTo(1); } [Fact] @@ -166,13 +164,13 @@ public void Capture(LfuCapacityPartition p) public void AssertWindowIncreased(LfuCapacityPartition p) { - p.Window.Should().BeGreaterThan(prev); + p.Window.ShouldBeGreaterThan(prev); prev = p.Window; } public void AssertWindowDecreased(LfuCapacityPartition p) { - p.Window.Should().BeLessThan(prev); + p.Window.ShouldBeLessThan(prev); prev = p.Window; } } diff --git a/BitFaster.Caching.UnitTests/Lfu/LfuInfoTests.cs b/BitFaster.Caching.UnitTests/Lfu/LfuInfoTests.cs index e25a48b9..3b36cd1e 100644 --- a/BitFaster.Caching.UnitTests/Lfu/LfuInfoTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/LfuInfoTests.cs @@ -1,6 +1,6 @@ using System; using BitFaster.Caching.Lfu.Builder; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -12,7 +12,7 @@ public void WhenExpiryNullGetExpiryReturnsNull() { var info = new LfuInfo(); - info.GetExpiry().Should().BeNull(); + info.GetExpiry().ShouldBeNull(); } [Fact] @@ -23,7 +23,7 @@ public void WhenExpiryCalcValueTypeDoesNotMatchThrows() info.SetExpiry(new TestExpiryCalculator()); Action act = () => info.GetExpiry(); - act.Should().Throw(); + act.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/LfuNodeListTests.cs b/BitFaster.Caching.UnitTests/Lfu/LfuNodeListTests.cs index e045a9ad..79a8af2b 100644 --- a/BitFaster.Caching.UnitTests/Lfu/LfuNodeListTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/LfuNodeListTests.cs @@ -1,6 +1,6 @@ using System; using BitFaster.Caching.Lfu; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lfu @@ -13,7 +13,7 @@ public void WhenEmptyRemoveFirstThrows() { var list = new LfuNodeList(); Action remove = () => { list.RemoveFirst(); }; - remove.Should().Throw(); + remove.ShouldThrow(); } #endif @@ -22,7 +22,7 @@ public void WhenPreviousNullLastReturnsNull() { var list = new LfuNodeList(); - list.Last.Should().BeNull(); + list.Last.ShouldBeNull(); } [Fact] @@ -35,7 +35,7 @@ public void WhenPreviousExistsLastReturnsPrevious() list.AddLast(node1); list.AddLast(node2); - list.Last.Should().BeSameAs(node2); + list.Last.ShouldBeSameAs(node2); } [Fact] @@ -48,7 +48,7 @@ public void WhenPreviousExistsNodePreviousReturnsPrevious() list.AddLast(node1); list.AddLast(node2); - node2.Previous.Should().BeSameAs(node1); + node2.Previous.ShouldBeSameAs(node1); } [Fact] @@ -61,7 +61,7 @@ public void WhenHeadNodePreviousReturnsNull() list.AddLast(node1); list.AddLast(node2); - node1.Previous.Should().BeNull(); + node1.Previous.ShouldBeNull(); } } } diff --git a/BitFaster.Caching.UnitTests/Lfu/TestScheduler.cs b/BitFaster.Caching.UnitTests/Lfu/TestScheduler.cs index 9beefabd..037586b7 100644 --- a/BitFaster.Caching.UnitTests/Lfu/TestScheduler.cs +++ b/BitFaster.Caching.UnitTests/Lfu/TestScheduler.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Scheduler; namespace BitFaster.Caching.UnitTests.Lfu diff --git a/BitFaster.Caching.UnitTests/Lfu/TimerWheelTests.cs b/BitFaster.Caching.UnitTests/Lfu/TimerWheelTests.cs index 25bac835..0cf467c0 100644 --- a/BitFaster.Caching.UnitTests/Lfu/TimerWheelTests.cs +++ b/BitFaster.Caching.UnitTests/Lfu/TimerWheelTests.cs @@ -2,12 +2,10 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Xml.Linq; using BitFaster.Caching.Lfu; using BitFaster.Caching.Lru; using BitFaster.Caching.Scheduler; -using FluentAssertions; -using FluentAssertions.Common; +using Shouldly; using Xunit; using Xunit.Abstractions; @@ -51,11 +49,11 @@ public void WhenAdvanceExpiredNodesExpire(long clock, Duration duration, int exp timerWheel.Advance(ref cache, new Duration(clock) + duration); var expired = items.Where(n => ((DisposeTracker)n.Value).Expired); - expired.Count().Should().Be(expiredCount); + expired.Count().ShouldBe(expiredCount); foreach (var node in expired) { - node.GetTimestamp().Should().BeLessThanOrEqualTo(clock + duration.raw); + node.GetTimestamp().ShouldBeLessThanOrEqualTo(clock + duration.raw); } } @@ -70,7 +68,7 @@ public void WhenAdvancedPastItemExpiryItemIsEvicted(long clock2) timerWheel.Advance(ref cache, new Duration(clock2 + 13 * TimerWheel.Spans[0])); - item.Expired.Should().BeTrue(); + item.Expired.ShouldBeTrue(); } [Theory] @@ -86,28 +84,28 @@ public void WhenAdvanceDifferentWheelsNodeIsRescheduled(long clock) timerWheel.Schedule(AddNode(15, new DisposeTracker(), t15)); // wheel 0 timerWheel.Schedule(AddNode(120, new DisposeTracker(), t120)); // wheel 1 - wheelEnumerator.Count().Should().Be(2); + wheelEnumerator.Count().ShouldBe(2); var initialPosition = wheelEnumerator.PositionOf(120); Duration t45 = clockD + Duration.FromSeconds(45); // discard T15, T120 in wheel[1] timerWheel.Advance(ref cache, t45); - lfuNodeList.Count.Should().Be(1); // verify discarded T15 - wheelEnumerator.PositionOf(15).Should().Be(WheelPosition.None); + lfuNodeList.Count.ShouldBe(1); // verify discarded T15 + wheelEnumerator.PositionOf(15).ShouldBe(WheelPosition.None); Duration t110 = clockD + Duration.FromSeconds(110); timerWheel.Advance(ref cache, t110); - lfuNodeList.Count.Should().Be(1); // verify not discarded, T120 in wheel[0] + lfuNodeList.Count.ShouldBe(1); // verify not discarded, T120 in wheel[0] var rescheduledPosition = wheelEnumerator.PositionOf(120); - rescheduledPosition.Should().BeLessThan(initialPosition); + rescheduledPosition.ShouldBeLessThan(initialPosition); Duration t130 = clockD + Duration.FromSeconds(130); timerWheel.Advance(ref cache, t130); - lfuNodeList.Count.Should().Be(0); // verify discarded T120 - wheelEnumerator.PositionOf(120).Should().Be(WheelPosition.None); + lfuNodeList.Count.ShouldBe(0); // verify discarded T120 + wheelEnumerator.PositionOf(120).ShouldBe(WheelPosition.None); } [Fact] @@ -119,7 +117,7 @@ public void WhenAdvanceOverflowsAndItemIsExpiredItemIsEvicted() timerWheel.Advance(ref cache, new Duration(timerWheel.time + (TimerWheel.Spans[3] * 365))); - this.lfuNodeList.Count.Should().Be(0); + this.lfuNodeList.Count.ShouldBe(0); } #if NET6_0_OR_GREATER @@ -142,7 +140,7 @@ public void WhenAdvanceBackwardsNothingIsEvicted(long clock) timerWheel.Advance(ref cache, new Duration(clock - 3 * TimerWheel.Spans[i])); } - this.lfuNodeList.Count.Should().Be(1_000); + this.lfuNodeList.Count.ShouldBe(1_000); } #endif @@ -156,9 +154,9 @@ public void WhenAdvanceThrowsCurrentTimeIsNotAdvanced() // This should expire the node, call evict, then throw via DisposeThrows.Dispose() Action advance = () => timerWheel.Advance(ref cache, new Duration(clock.raw + (2 * TimerWheel.Spans[1]))); - advance.Should().Throw(); + advance.ShouldThrow(); - timerWheel.time.Should().Be(clock.raw); + timerWheel.time.ShouldBe(clock.raw); } [Theory] @@ -166,7 +164,7 @@ public void WhenAdvanceThrowsCurrentTimeIsNotAdvanced() public void WhenEmptyGetExpirationDelayIsMax(long clock) { timerWheel.time = clock; - timerWheel.GetExpirationDelay().raw.Should().Be(long.MaxValue); + timerWheel.GetExpirationDelay().raw.ShouldBe(long.MaxValue); } [Theory] @@ -181,7 +179,7 @@ public void WhenScheduledMaxNodeIsInOuterWheel(long clock) timerWheel.Schedule(AddNode(1, new DisposeTracker(), tMax)); var initialPosition = wheelEnumerator.PositionOf(1); - initialPosition.wheel.Should().Be(4); + initialPosition.wheel.ShouldBe(4); } [Theory] @@ -194,7 +192,7 @@ public void WhenScheduledInFirstWheelDelayIsUpdated(long clock) timerWheel.Schedule(new TimeOrderNode(1, new DisposeTracker()) { TimeToExpire = new Duration(clock) + delay }); - timerWheel.GetExpirationDelay().raw.Should().BeLessThanOrEqualTo(TimerWheel.Spans[0]); + timerWheel.GetExpirationDelay().raw.ShouldBeLessThanOrEqualTo(TimerWheel.Spans[0]); } [Theory] @@ -207,7 +205,7 @@ public void WhenScheduledInLastWheelDelayIsUpdated(long clock) timerWheel.Schedule(new TimeOrderNode(1, new DisposeTracker()) { TimeToExpire = new Duration(clock) + delay }); - timerWheel.GetExpirationDelay().raw.Should().BeLessThanOrEqualTo(delay.raw); + timerWheel.GetExpirationDelay().raw.ShouldBeLessThanOrEqualTo(delay.raw); } [Theory] @@ -226,14 +224,14 @@ public void WhenScheduledInDifferentWheelsDelayIsCorrect(long clock) Duration t45 = clockD + Duration.FromSeconds(45); // discard T15, T80 in wheel[1] timerWheel.Advance(ref cache, t45); - lfuNodeList.Count.Should().Be(1); // verify discarded + lfuNodeList.Count.ShouldBe(1); // verify discarded Duration t95 = clockD + Duration.FromSeconds(95); timerWheel.Schedule(AddNode(3, new DisposeTracker(), t95)); // wheel 0 Duration expectedDelay = (t80 - t45); var delay = timerWheel.GetExpirationDelay(); - delay.raw.Should().BeLessThan(expectedDelay.raw + TimerWheel.Spans[0]); + delay.raw.ShouldBeLessThan(expectedDelay.raw + TimerWheel.Spans[0]); } [Fact] @@ -242,12 +240,12 @@ public void WhenScheduledThenDescheduledNodeIsRemoved() var node = AddNode(1, new DisposeTracker(), Duration.SinceEpoch()); timerWheel.Schedule(node); - wheelEnumerator.PositionOf(1).Should().NotBe(WheelPosition.None); + wheelEnumerator.PositionOf(1).ShouldNotBe(WheelPosition.None); TimerWheel.Deschedule(node); - wheelEnumerator.PositionOf(1).Should().Be(WheelPosition.None); - node.GetNextInTimeOrder().Should().BeNull(); - node.GetPreviousInTimeOrder().Should().BeNull(); + wheelEnumerator.PositionOf(1).ShouldBe(WheelPosition.None); + node.GetNextInTimeOrder().ShouldBeNull(); + node.GetPreviousInTimeOrder().ShouldBeNull(); } [Fact] @@ -261,7 +259,7 @@ public void WhenRescheduledLaterNodeIsMoved() node.TimeToExpire = time + Duration.FromMinutes(60 * 30); timerWheel.Reschedule(node); - wheelEnumerator.PositionOf(1).Should().BeGreaterThan(initial); + wheelEnumerator.PositionOf(1).ShouldBeGreaterThan(initial); } [Fact] @@ -271,7 +269,7 @@ public void WhenDetachedRescheduleIsNoOp() var node = AddNode(1, new DisposeTracker(), time); timerWheel.Reschedule(node); - wheelEnumerator.PositionOf(1).Should().Be(WheelPosition.None); + wheelEnumerator.PositionOf(1).ShouldBe(WheelPosition.None); } private TimeOrderNode AddNode(int key, IDisposable value, Duration timeToExpire) diff --git a/BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs index e311f6d7..26d9afff 100644 --- a/BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Threading.Tasks; @@ -15,7 +15,7 @@ public void WhenTtlIsTimeSpanMaxThrow() { Action constructor = () => { new AfterAccessPolicy(TimeSpan.MaxValue); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -23,20 +23,20 @@ public void WhenTtlIsZeroThrow() { Action constructor = () => { new AfterAccessPolicy(TimeSpan.Zero); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void WhenTtlIsMaxSetAsMax() { var policy = new AfterAccessPolicy(Duration.MaxRepresentable); - policy.TimeToLive.Should().BeCloseTo(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20)); + policy.TimeToLive.ShouldBe(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20)); } [Fact] public void TimeToLiveShouldBeTenSecs() { - this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10)); + this.policy.TimeToLive.ShouldBe(TimeSpan.FromSeconds(10)); } [Fact] @@ -44,8 +44,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -53,7 +53,7 @@ public void CreateItemInitializesTimestampToNow() { var item = this.policy.CreateItem(1, 2); - item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon); + item.TickCount.ShouldBe(Duration.SinceEpoch().raw); } [Fact] @@ -64,7 +64,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [Fact] @@ -77,7 +77,7 @@ public async Task TouchUpdatesTicksCount() this.policy.ShouldDiscard(item); // set the time in the policy this.policy.Touch(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -90,7 +90,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -100,7 +100,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(11).raw; - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -110,13 +110,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(9).raw; - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } [Theory] @@ -128,7 +128,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -140,7 +140,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -152,7 +152,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private LongTickCountLruItem CreateItem(bool wasAccessed, bool isExpired) diff --git a/BitFaster.Caching.UnitTests/Lru/CacheExtTests.cs b/BitFaster.Caching.UnitTests/Lru/CacheExtTests.cs index 4c5ede93..f4e7aa4b 100644 --- a/BitFaster.Caching.UnitTests/Lru/CacheExtTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/CacheExtTests.cs @@ -2,7 +2,7 @@ using System.Linq; using BitFaster.Caching.Lfu; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -14,10 +14,10 @@ public void CanUseICache2FromClassicLru() { var cache = new ClassicLru(5); var cache2 = (ICacheExt)cache; - cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).Should().Be("43"); - cache2.TryRemove(43, out _).Should().BeFalse(); + cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).ShouldBe("43"); + cache2.TryRemove(43, out _).ShouldBeFalse(); var first = cache2.First(); - cache2.TryRemove(first).Should().BeTrue(); + cache2.TryRemove(first).ShouldBeTrue(); } [Fact] @@ -28,10 +28,10 @@ public void CanUseICache2FromConcurrentLfuBuilder() .Build(); var cache2 = (ICacheExt)cache; - cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).Should().Be("43"); - cache2.TryRemove(43, out _).Should().BeFalse(); + cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).ShouldBe("43"); + cache2.TryRemove(43, out _).ShouldBeFalse(); var first = cache2.First(); - cache2.TryRemove(first).Should().BeTrue(); + cache2.TryRemove(first).ShouldBeTrue(); } [Fact] @@ -43,10 +43,10 @@ public void CanUseICache2FromConcurrentLruBuilder() .Build(); var cache2 = (ICacheExt)cache; - cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).Should().Be("43"); - cache2.TryRemove(43, out _).Should().BeFalse(); + cache2.GetOrAdd(42, static (k, i) => (k + i).ToString(), 1).ShouldBe("43"); + cache2.TryRemove(43, out _).ShouldBeFalse(); var first = cache2.First(); - cache2.TryRemove(first).Should().BeTrue(); + cache2.TryRemove(first).ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/CapacityPartitionExtensionsTests.cs b/BitFaster.Caching.UnitTests/Lru/CapacityPartitionExtensionsTests.cs index 9b2e755d..35f0b322 100644 --- a/BitFaster.Caching.UnitTests/Lru/CapacityPartitionExtensionsTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/CapacityPartitionExtensionsTests.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -18,7 +14,7 @@ public void WhenCapacityIsValidDoesNotThrow() Action validate = () => { p.Validate(); }; - validate.Should().NotThrow(); + validate.ShouldNotThrow(); } [Fact] @@ -28,7 +24,7 @@ public void WhenColdIsZeroThrows() Action validate = () => { p.Validate(); }; - validate.Should().Throw(); + validate.ShouldThrow(); } [Fact] @@ -38,7 +34,7 @@ public void WhenWarmIsZeroThrows() Action validate = () => { p.Validate(); }; - validate.Should().Throw(); + validate.ShouldThrow(); } [Fact] @@ -48,7 +44,7 @@ public void WhenHotIsZeroThrows() Action validate = () => { p.Validate(); }; - validate.Should().Throw(); + validate.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs index 4b0b790f..dbae0c2f 100644 --- a/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs @@ -1,9 +1,8 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Xunit; using System.Collections; @@ -22,7 +21,7 @@ public void WhenConcurrencyIsLessThan1CtorThrows() { Action constructor = () => { var x = new ClassicLru(0, 3, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -30,7 +29,7 @@ public void WhenCapacityIsLessThan3CtorThrows() { Action constructor = () => { var x = new ClassicLru(1, 2, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -38,7 +37,7 @@ public void WhenComparerIsNullCtorThrows() { Action constructor = () => { var x = new ClassicLru(1, 3, null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -46,50 +45,50 @@ public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue() { var x = new ClassicLru(3); - x.GetOrAdd(1, k => k).Should().Be(1); + x.GetOrAdd(1, k => k).ShouldBe(1); } [Fact] public void WhenCtorCapacityArgIs3CapacityIs3() { - new ClassicLru(3).Capacity.Should().Be(3); + new ClassicLru(3).Capacity.ShouldBe(3); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); - lru.Count.Should().Be(1); + lru.Count.ShouldBe(1); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + lru.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] public void WhenItemsAddedGenericEnumerateContainsKvps() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.Should().BeEquivalentTo(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + lru.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); } [Fact] public void WhenItemsAddedEnumerateContainsKvps() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); var enumerable = (IEnumerable)lru; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); } [Fact] @@ -98,8 +97,8 @@ public void WhenItemExistsTryGetReturnsValueAndTrue() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - result.Should().Be(true); - value.Should().Be("1"); + result.ShouldBe(true); + value.ShouldBe("1"); } [Fact] @@ -108,14 +107,14 @@ public void WhenItemDoesNotExistTryGetReturnsNullAndFalse() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(2, out var value); - result.Should().Be(false); - value.Should().BeNull(); + result.ShouldBe(false); + value.ShouldBeNull(); } [Fact] public void MetricsAreEnabled() { - lru.Metrics.HasValue.Should().BeTrue(); + lru.Metrics.HasValue.ShouldBeTrue(); } [Fact] @@ -124,7 +123,7 @@ public void WhenItemIsAddedThenRetrievedMetricHitRatioIsHalf() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.HitRatio.Should().Be(0.5); + lru.Metrics.Value.HitRatio.ShouldBe(0.5); } [Fact] @@ -133,7 +132,7 @@ public void WhenItemIsAddedThenRetrievedMetricHitsIs1() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.Hits.Should().Be(1); + lru.Metrics.Value.Hits.ShouldBe(1); } [Fact] @@ -142,7 +141,7 @@ public void WhenItemIsAddedThenRetrievedMetricTotalIs2() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.Total.Should().Be(2); + lru.Metrics.Value.Total.ShouldBe(2); } [Fact] @@ -151,13 +150,13 @@ public void WhenItemDoesNotExistTryGetIncrementsMiss() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.Misses.Should().Be(1); + lru.Metrics.Value.Misses.ShouldBe(1); } [Fact] public void EventsAreEnabled() { - lru.Events.HasValue.Should().BeFalse(); + lru.Events.HasValue.ShouldBeFalse(); } private void OnItemRemoved(object sender, ItemRemovedEventArgs e) @@ -168,7 +167,7 @@ private void OnItemRemoved(object sender, ItemRemovedEventArgs e) [Fact] public void ExpireAfterWriteIsDisabled() { - lru.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + lru.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -177,8 +176,8 @@ public void WhenKeyIsRequestedItIsCreatedAndCached() var result1 = lru.GetOrAdd(1, valueFactory.Create); var result2 = lru.GetOrAdd(1, valueFactory.Create); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } @@ -188,8 +187,8 @@ public void WhenKeyIsRequestedWithArgItIsCreatedAndCached() var result1 = lru.GetOrAdd(1, valueFactory.Create, "x"); var result2 = lru.GetOrAdd(1, valueFactory.Create, "y"); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -198,8 +197,8 @@ public async Task WhenKeyIsRequesteItIsCreatedAndCachedAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -208,8 +207,8 @@ public async Task WhenKeyIsRequestedWithArgItIsCreatedAndCachedAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync, "x"); var result2 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync, "y"); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -218,10 +217,10 @@ public void WhenDifferentKeysAreRequestedValueIsCreatedForEach() var result1 = lru.GetOrAdd(1, valueFactory.Create); var result2 = lru.GetOrAdd(2, valueFactory.Create); - valueFactory.timesCalled.Should().Be(2); + valueFactory.timesCalled.ShouldBe(2); - result1.Should().Be("1"); - result2.Should().Be("2"); + result1.ShouldBe("1"); + result2.ShouldBe("2"); } [Fact] @@ -230,10 +229,10 @@ public async Task WhenDifferentKeysAreRequesteValueIsCreatedForEachAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await lru.GetOrAddAsync(2, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(2); + valueFactory.timesCalled.ShouldBe(2); - result1.Should().Be("1"); - result2.Should().Be("2"); + result1.ShouldBe("1"); + result2.ShouldBe("2"); } [Fact] @@ -244,8 +243,8 @@ public void WhenMoreKeysRequestedThanCapacityCountDoesNotIncrease() lru.GetOrAdd(i, valueFactory.Create); } - lru.Count.Should().Be(capacity); - valueFactory.timesCalled.Should().Be(capacity + 1); + lru.Count.ShouldBe(capacity); + valueFactory.timesCalled.ShouldBe(capacity + 1); } [Fact] @@ -256,8 +255,8 @@ public async Task WhenMoreKeysRequestedThanCapacityCountDoesNotIncreaseAsync() await lru.GetOrAddAsync(i, valueFactory.CreateAsync); } - lru.Count.Should().Be(capacity); - valueFactory.timesCalled.Should().Be(capacity + 1); + lru.Count.ShouldBe(capacity); + valueFactory.timesCalled.ShouldBe(capacity + 1); } [Fact] @@ -269,23 +268,23 @@ public void WhenMoreKeysRequestedThanCapacityOldestItemIsEvicted() lru.GetOrAdd(i, valueFactory.Create); } - valueFactory.timesCalled.Should().Be(capacity); + valueFactory.timesCalled.ShouldBe(capacity); // request 0, now item 1 is to be evicted lru.GetOrAdd(0, valueFactory.Create); - valueFactory.timesCalled.Should().Be(capacity); + valueFactory.timesCalled.ShouldBe(capacity); // request next item after last, verify value factory was called lru.GetOrAdd(capacity, valueFactory.Create); - valueFactory.timesCalled.Should().Be(capacity + 1); + valueFactory.timesCalled.ShouldBe(capacity + 1); // request 0, verify value factory not called lru.GetOrAdd(0, valueFactory.Create); - valueFactory.timesCalled.Should().Be(capacity + 1); + valueFactory.timesCalled.ShouldBe(capacity + 1); // request 1, verify value factory is called (and it was therefore not cached) lru.GetOrAdd(1, valueFactory.Create); - valueFactory.timesCalled.Should().Be(capacity + 2); + valueFactory.timesCalled.ShouldBe(capacity + 2); } [Fact] @@ -297,12 +296,12 @@ public void WhenMoreKeysRequestedThanCapacityEvictedMetricRecordsNumberEvicted() lru.GetOrAdd(i, valueFactory.Create); } - lru.Metrics.Value.Evicted.Should().Be(0); + lru.Metrics.Value.Evicted.ShouldBe(0); // request 0, now item 1 is to be evicted lru.GetOrAdd(4, valueFactory.Create); - lru.Metrics.Value.Evicted.Should().Be(1); + lru.Metrics.Value.Evicted.ShouldBe(1); } [Fact] @@ -316,8 +315,8 @@ public void WhenValueExpiresItIsDisposed() lruOfDisposable.GetOrAdd(i, disposableValueFactory.Create); } - disposableValueFactory.Items[0].IsDisposed.Should().BeTrue(); - disposableValueFactory.Items[1].IsDisposed.Should().BeFalse(); + disposableValueFactory.Items[0].IsDisposed.ShouldBeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeFalse(); } [Fact] @@ -331,8 +330,8 @@ public async Task WhenValueExpiresAsyncItIsDisposed() await lruOfDisposable.GetOrAddAsync(i, disposableValueFactory.CreateAsync); } - disposableValueFactory.Items[0].IsDisposed.Should().BeTrue(); - disposableValueFactory.Items[1].IsDisposed.Should().BeFalse(); + disposableValueFactory.Items[0].IsDisposed.ShouldBeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeFalse(); } [Fact] @@ -340,7 +339,7 @@ public void WhenKeyDoesNotExistTryGetReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryGet(2, out var result).Should().Be(false); + lru.TryGet(2, out var result).ShouldBe(false); } [Fact] @@ -349,8 +348,8 @@ public void WhenKeyExistsTryGetReturnsTrueAndOutValueIsCorrect() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - result.Should().Be(true); - value.Should().Be("1"); + result.ShouldBe(true); + value.ShouldBe("1"); } [Fact] @@ -358,8 +357,8 @@ public void WhenKeyExistsTryRemoveRemovesItemAndReturnsTrue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(1).Should().BeTrue(); - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryRemove(1).ShouldBeTrue(); + lru.TryGet(1, out var value).ShouldBeFalse(); } [Fact] @@ -367,8 +366,8 @@ public void WhenKeyExistsTryRemoveReturnsValue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(1, out var value).Should().BeTrue(); - value.Should().Be("1"); + lru.TryRemove(1, out var value).ShouldBeTrue(); + value.ShouldBe("1"); } [Fact] @@ -376,8 +375,8 @@ public void WhenItemExistsTryRemovesItemAndReturnsTrue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(new KeyValuePair(1, "1")).Should().BeTrue(); - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryRemove(new KeyValuePair(1, "1")).ShouldBeTrue(); + lru.TryGet(1, out var value).ShouldBeFalse(); } [Fact] @@ -385,8 +384,8 @@ public void WhenTryRemoveKvpDoesntMatchItemNotRemovedAndReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(new KeyValuePair(1, "2")).Should().BeFalse(); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryRemove(new KeyValuePair(1, "2")).ShouldBeFalse(); + lru.TryGet(1, out var value).ShouldBeTrue(); } [Fact] @@ -398,7 +397,7 @@ public void WhenItemIsRemovedItIsDisposed() lruOfDisposable.GetOrAdd(1, disposableValueFactory.Create); lruOfDisposable.TryRemove(1); - disposableValueFactory.Items[1].IsDisposed.Should().BeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeTrue(); } [Fact] @@ -406,7 +405,7 @@ public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(2).Should().BeFalse(); + lru.TryRemove(2).ShouldBeFalse(); } [Fact] @@ -414,10 +413,10 @@ public void WhenKeyExistsTryUpdateUpdatesValueAndReturnsTrue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(1, "2").Should().BeTrue(); + lru.TryUpdate(1, "2").ShouldBeTrue(); lru.TryGet(1, out var value); - value.Should().Be("2"); + value.ShouldBe("2"); } [Fact] @@ -425,7 +424,7 @@ public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(2, "3").Should().BeFalse(); + lru.TryUpdate(2, "3").ShouldBeFalse(); } // backcompat: remove conditional compile @@ -435,9 +434,9 @@ public void WhenKeyExistsTryUpdateIncrementsUpdateCount() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(1, "2").Should().BeTrue(); + lru.TryUpdate(1, "2").ShouldBeTrue(); - lru.Metrics.Value.Updated.Should().Be(1); + lru.Metrics.Value.Updated.ShouldBe(1); } [Fact] @@ -445,9 +444,9 @@ public void WhenKeyDoesNotExistTryUpdateDoesNotIncrementCounter() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(2, "3").Should().BeFalse(); + lru.TryUpdate(2, "3").ShouldBeFalse(); - lru.Metrics.Value.Updated.Should().Be(0); + lru.Metrics.Value.Updated.ShouldBe(0); } #endif @@ -456,8 +455,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() { lru.AddOrUpdate(1, "1"); - lru.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be("1"); + lru.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe("1"); } [Fact] @@ -466,8 +465,8 @@ public void WhenKeyExistsAddOrUpdatUpdatesExistingItem() lru.AddOrUpdate(1, "1"); lru.AddOrUpdate(1, "2"); - lru.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be("2"); + lru.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe("2"); } [Fact] @@ -479,8 +478,8 @@ public void WhenKeyDoesNotExistAddOrUpdateMaintainsLruOrder() lru.AddOrUpdate(4, "4"); // verify first item added is removed - lru.Count.Should().Be(3); - lru.TryGet(1, out var value).Should().BeFalse(); + lru.Count.ShouldBe(3); + lru.TryGet(1, out _).ShouldBeFalse(); } [Fact] @@ -496,17 +495,17 @@ public void WhenAddOrUpdateExpiresItemsTheyAreDisposed() } // first item is evicted and disposed - items[0].IsDisposed.Should().BeTrue(); + items[0].IsDisposed.ShouldBeTrue(); // all other items are not disposed - items.Skip(1).All(i => i.IsDisposed == false).Should().BeTrue(); + items.Skip(1).All(i => i.IsDisposed == false).ShouldBeTrue(); } [Fact] public void WhenCacheIsEmptyClearIsNoOp() { lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } [Fact] @@ -515,7 +514,7 @@ public void WhenItemsExistClearRemovesAllItems() lru.AddOrUpdate(1, "1"); lru.AddOrUpdate(2, "2"); lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } [Fact] @@ -532,19 +531,19 @@ public void WhenItemsAreDisposableClearDisposesItemsOnRemove() lruOfDisposable.Clear(); - items.All(i => i.IsDisposed == true).Should().BeTrue(); + items.All(i => i.IsDisposed == true).ShouldBeTrue(); } [Fact] public void WhenTrimCountIsZeroThrows() - { - lru.Invoking(l => lru.Trim(0)).Should().Throw(); + { + Should.Throw(() => lru.Trim(0)); } [Fact] public void WhenTrimCountIsMoreThanCapacityThrows() { - lru.Invoking(l => lru.Trim(capacity + 1)).Should().Throw(); + Should.Throw(() => lru.Trim(capacity + 1)); } [Theory] @@ -563,7 +562,7 @@ public void WhenItemsExistTrimRemovesExpectedItemCount(int trimCount, int[] expe lru.Trim(trimCount); - lru.Keys.Should().BeEquivalentTo(expected); + lru.Keys.ShouldBe(expected); } [Fact] @@ -586,10 +585,10 @@ public void WhenItemsAreDisposableTrimDisposesItems() lruOfDisposable.Trim(2); - items[0].IsDisposed.Should().BeTrue(); - items[1].IsDisposed.Should().BeTrue(); - items[2].IsDisposed.Should().BeFalse(); - items[3].IsDisposed.Should().BeFalse(); + items[0].IsDisposed.ShouldBeTrue(); + items[1].IsDisposed.ShouldBeTrue(); + items[2].IsDisposed.ShouldBeFalse(); + items[3].IsDisposed.ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterAccessTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterAccessTests.cs index fbdb8a05..bb6ef60d 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterAccessTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterAccessTests.cs @@ -3,7 +3,7 @@ using System.Runtime.InteropServices; using BitFaster.Caching.Lru; using BitFaster.Caching.UnitTests.Retry; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -37,13 +37,13 @@ public ConcurrentLruAfterAccessTests() [Fact] public void CanExpireIsTrue() { - this.lru.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); + this.lru.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); } [Fact] public void TimeToLiveIsCtorArg() { - this.lru.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(timeToLive); + this.lru.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(timeToLive); } [RetryFact] @@ -51,7 +51,7 @@ public void WhenItemIsNotExpiredItIsNotRemoved() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); } [RetryFact] @@ -67,7 +67,7 @@ public void WhenItemIsExpiredItIsRemoved() timeToLive.MultiplyBy(ttlWaitMlutiplier), lru => { - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryGet(1, out var value).ShouldBeFalse(); } ); } @@ -86,7 +86,7 @@ public void WhenItemIsUpdatedTtlIsExtended() lru => { lru.TryUpdate(1, "3"); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); } ); } @@ -120,12 +120,12 @@ public void WhenItemIsReadTtlIsExtended() TimeSpan.FromMilliseconds(50), lru => { - lru.TryGet(1, out _).Should().BeTrue($"First"); + lru.TryGet(1, out _).ShouldBeTrue($"First"); }, TimeSpan.FromMilliseconds(75), lru => { - lru.TryGet(1, out var value).Should().BeTrue($"Second"); + lru.TryGet(1, out var value).ShouldBeTrue($"Second"); } ); } @@ -150,15 +150,15 @@ public void WhenValueEvictedItemRemovedEventIsFired() lruEvents.GetOrAdd(i + 1, i => i + 1); } - removedItems.Count.Should().Be(2); + removedItems.Count.ShouldBe(2); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be(2); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe(2); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Evicted); - removedItems[1].Key.Should().Be(4); - removedItems[1].Value.Should().Be(5); - removedItems[1].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[1].Key.ShouldBe(4); + removedItems[1].Value.ShouldBe(5); + removedItems[1].Reason.ShouldBe(ItemRemovedReason.Evicted); } [RetryFact] @@ -190,7 +190,7 @@ public void WhenItemsAreExpiredExpireRemovesExpiredItems() { lru.Policy.ExpireAfterAccess.Value.TrimExpired(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } ); } @@ -221,7 +221,7 @@ public void WhenCacheHasExpiredAndFreshItemsExpireRemovesOnlyExpiredItems() lru.Policy.ExpireAfterAccess.Value.TrimExpired(); - lru.Count.Should().Be(3); + lru.Count.ShouldBe(3); } ); } @@ -244,7 +244,7 @@ public void WhenItemsAreExpiredTrimRemovesExpiredItems() { lru.Policy.Eviction.Value.Trim(1); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } ); } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterDiscreteTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterDiscreteTests.cs index c0ddf336..e9897613 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterDiscreteTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterDiscreteTests.cs @@ -3,7 +3,7 @@ using System.Runtime.InteropServices; using BitFaster.Caching.Lru; using BitFaster.Caching.UnitTests.Retry; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -39,21 +39,21 @@ public ConcurrentLruAfterDiscreteTests() [Fact] public void WhenKeyIsWrongTypeTryGetTimeToExpireIsFalse() { - lru.Policy.ExpireAfter.Value.TryGetTimeToExpire("foo", out _).Should().BeFalse(); + lru.Policy.ExpireAfter.Value.TryGetTimeToExpire("foo", out _).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryGetTimeToExpireIsFalse() { - lru.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out _).Should().BeFalse(); + lru.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out _).ShouldBeFalse(); } [RetryFact] public void WhenKeyExistsTryGetTimeToExpireReturnsExpiryTime() { lru.GetOrAdd(1, k => "1"); - lru.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out var expiry).Should().BeTrue(); - expiry.Should().BeCloseTo(TestExpiryCalculator.DefaultTimeToExpire.ToTimeSpan(), delta); + lru.Policy.ExpireAfter.Value.TryGetTimeToExpire(1, out var expiry).ShouldBeTrue(); + expiry.ShouldBe(TestExpiryCalculator.DefaultTimeToExpire.ToTimeSpan(), delta); } [RetryFact] @@ -69,7 +69,7 @@ public void WhenItemIsExpiredItIsRemoved() TestExpiryCalculator.DefaultTimeToExpire.MultiplyBy(ttlWaitMlutiplier), lru => { - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryGet(1, out var value).ShouldBeFalse(); } ); } @@ -88,7 +88,7 @@ public void WhenItemIsUpdatedTtlIsExtended() lru => { lru.TryUpdate(1, "3"); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); } ); } @@ -118,12 +118,12 @@ public void WhenItemIsReadTtlIsExtended() TimeSpan.FromMilliseconds(50), lru => { - lru.TryGet(1, out _).Should().BeTrue($"First"); + lru.TryGet(1, out _).ShouldBeTrue($"First"); }, TimeSpan.FromMilliseconds(75), lru => { - lru.TryGet(1, out var value).Should().BeTrue($"Second"); + lru.TryGet(1, out var value).ShouldBeTrue($"Second"); } ); } @@ -150,15 +150,15 @@ public void WhenValueEvictedItemRemovedEventIsFired() lruEvents.GetOrAdd(i + 1, i => $"{i + 1}"); } - removedItems.Count.Should().Be(2); + removedItems.Count.ShouldBe(2); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be("2"); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe("2"); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Evicted); - removedItems[1].Key.Should().Be(4); - removedItems[1].Value.Should().Be("5"); - removedItems[1].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[1].Key.ShouldBe(4); + removedItems[1].Value.ShouldBe("5"); + removedItems[1].Reason.ShouldBe(ItemRemovedReason.Evicted); } [RetryFact] @@ -190,7 +190,7 @@ public void WhenItemsAreExpiredExpireRemovesExpiredItems() { lru.Policy.ExpireAfter.Value.TrimExpired(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } ); } @@ -221,7 +221,7 @@ public void WhenCacheHasExpiredAndFreshItemsExpireRemovesOnlyExpiredItems() lru.Policy.ExpireAfter.Value.TrimExpired(); - lru.Count.Should().Be(3); + lru.Count.ShouldBe(3); } ); } @@ -244,7 +244,7 @@ public void WhenItemsAreExpiredTrimRemovesExpiredItems() { lru.Policy.Eviction.Value.Trim(1); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } ); } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs index 851640c3..68ecafa1 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs @@ -1,557 +1,557 @@ -using System; -using BitFaster.Caching.Lru; -using BitFaster.Caching.Atomic; -using FluentAssertions; -using Xunit; - -namespace BitFaster.Caching.UnitTests.Lru -{ - public class ConcurrentLruBuilderTests - { - [Fact] - public void TestFastLru() - { - ICache lru = new ConcurrentLruBuilder() - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void TestMetricsLru() - { - ICache lru = new ConcurrentLruBuilder() - .WithMetrics() - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void TestFastTLru() - { - ICache lru = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void TestMetricsTLru() - { - ICache lru = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .WithMetrics() - .Build(); - - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(128); - } - - [Fact] - public void AsAsyncTestFastLru() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void AsAsyncTestMetricsLru() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .WithMetrics() - .AsAsyncCache() - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void AsAsyncTestFastTLru() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .AsAsyncCache() - .Build(); - - lru.Should().BeOfType>(); - } - - [Fact] - public void AsAsyncTestMetricsTLru() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .WithMetrics() - .AsAsyncCache() - .Build(); - - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(128); - } - - [Fact] - public void TestComparer() - { - ICache fastLru = new ConcurrentLruBuilder() - .WithKeyComparer(StringComparer.OrdinalIgnoreCase) - .Build(); - - fastLru.GetOrAdd("a", k => 1); - fastLru.TryGet("A", out var value).Should().BeTrue(); - } - - [Fact] - public void TestConcurrencyLevel() - { - var b = new ConcurrentLruBuilder() - .WithConcurrencyLevel(-1); - - Action constructor = () => { var x = b.Build(); }; - - constructor.Should().Throw(); - } - - [Fact] - public void TestIntCapacity() - { - ICache lru = new ConcurrentLruBuilder() - .WithCapacity(3) - .Build(); - - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - [Fact] - public void TestPartitionCapacity() - { - ICache lru = new ConcurrentLruBuilder() - .WithCapacity(new FavorWarmPartition(6)) - .Build(); - - lru.Policy.Eviction.Value.Capacity.Should().Be(6); - } - - [Fact] - public void TestExpireAfterAccess() - { - ICache expireAfterAccess = new ConcurrentLruBuilder() - .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) - .Build(); - - expireAfterAccess.Metrics.HasValue.Should().BeFalse(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); - } - - [Fact] - public void TestExpireAfterAccessWithMetrics() - { - ICache expireAfterAccess = new ConcurrentLruBuilder() - .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) - .WithMetrics() - .Build(); - - expireAfterAccess.Metrics.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.HasValue.Should().BeTrue(); - expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(TimeSpan.FromSeconds(1)); - expireAfterAccess.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); - } - - [Fact] - public void TestExpireAfterReadAndExpireAfterWriteThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) - .WithExpireAfterWrite(TimeSpan.FromSeconds(2)); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestExpireAfter() - { - ICache expireAfter = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .Build(); - - expireAfter.Metrics.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfter.HasValue.Should().BeTrue(); - - expireAfter.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); - } - - [Fact] - public void TestAsyncExpireAfter() - { - IAsyncCache expireAfter = new ConcurrentLruBuilder() - .AsAsyncCache() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .Build(); - - expireAfter.Metrics.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfter.HasValue.Should().BeTrue(); - - expireAfter.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); - } - - [Fact] - public void TestExpireAfterWithMetrics() - { - ICache expireAfter = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .WithMetrics() - .Build(); - - expireAfter.Metrics.HasValue.Should().BeTrue(); - expireAfter.Policy.ExpireAfter.HasValue.Should().BeTrue(); - - expireAfter.Policy.ExpireAfterAccess.HasValue.Should().BeFalse(); - expireAfter.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); - } - - [Fact] - public void TestExpireAfterWriteAndExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestExpireAfterAccessAndExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestExpireAfterAccessAndWriteAndExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) - .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestScopedWithExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .AsScopedCache(); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestScopedAtomicWithExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .AsScopedCache() - .WithAtomicGetOrAdd(); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestAsyncScopedWithExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .AsAsyncCache() - .AsScopedCache(); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - [Fact] - public void TestAsyncScopedAtomicWithExpireAfterThrows() - { - var builder = new ConcurrentLruBuilder() - .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) - .AsAsyncCache() - .AsScopedCache() - .WithAtomicGetOrAdd(); - - Action act = () => builder.Build(); - act.Should().Throw(); - } - - // There are 15 combinations to test: - // ----------------------------- - //1 WithAtomic - //2 WithScoped - //3 AsAsync - // - // ----------------------------- - //4 WithAtomic - // WithScoped - // - //5 WithScoped - // WithAtomic - // - //6 AsAsync - // WithScoped - // - //7 WithScoped - // AsAsync - // - //8 WithAtomic - // AsAsync - // - //9 AsAsync - // WithAtomic - // - // ----------------------------- - //10 WithAtomic - // WithScoped - // AsAsync - // - //11 WithAtomic - // AsAsync - // WithScoped - // - //12 WithScoped - // WithAtomic - // AsAsync - // - //13 WithScoped - // AsAsync - // WithAtomic - // - //14 AsAsync - // WithScoped - // WithAtomic - // - //15 AsAsync - // WithAtomic - // WithScoped - - // 1 - [Fact] - public void WithScopedValues() - { - IScopedCache lru = new ConcurrentLruBuilder() - .AsScopedCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - // 2 - [Fact] - public void WithAtomicFactory() - { - ICache lru = new ConcurrentLruBuilder() - .WithAtomicGetOrAdd() - .WithCapacity(3) - .Build(); - - lru.Should().BeOfType>(); - } - - // 3 - [Fact] - public void AsAsync() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 4 - [Fact] - public void WithAtomicWithScope() - { - IScopedCache lru = new ConcurrentLruBuilder() - .WithAtomicGetOrAdd() - .AsScopedCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - // 5 - [Fact] - public void WithScopedWithAtomic() - { - IScopedCache lru = new ConcurrentLruBuilder() - .AsScopedCache() - .WithAtomicGetOrAdd() - .WithCapacity(3) - .Build(); - - lru.Should().BeOfType>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - // 6 - [Fact] - public void AsAsyncWithScoped() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .AsScopedCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - // 7 - [Fact] - public void WithScopedAsAsync() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsScopedCache() - .AsAsyncCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - lru.Policy.Eviction.Value.Capacity.Should().Be(3); - } - - // 8 - [Fact] - public void WithAtomicAsAsync() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .WithAtomicGetOrAdd() - .AsAsyncCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 9 - [Fact] - public void AsAsyncWithAtomic() - { - IAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .WithAtomicGetOrAdd() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 10 - [Fact] - public void WithAtomicWithScopedAsAsync() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .WithAtomicGetOrAdd() - .AsScopedCache() - .AsAsyncCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 11 - [Fact] - public void WithAtomicAsAsyncWithScoped() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .WithAtomicGetOrAdd() - .AsAsyncCache() - .AsScopedCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 12 - [Fact] - public void WithScopedWithAtomicAsAsync() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsScopedCache() - .WithAtomicGetOrAdd() - .AsAsyncCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 13 - [Fact] - public void WithScopedAsAsyncWithAtomic() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsScopedCache() - .AsAsyncCache() - .WithAtomicGetOrAdd() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 14 - [Fact] - public void AsAsyncWithScopedWithAtomic() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .AsScopedCache() - .WithAtomicGetOrAdd() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - - // 15 - [Fact] - public void AsAsyncWithAtomicWithScoped() - { - IScopedAsyncCache lru = new ConcurrentLruBuilder() - .AsAsyncCache() - .WithAtomicGetOrAdd() - .AsScopedCache() - .WithCapacity(3) - .Build(); - - lru.Should().BeAssignableTo>(); - } - } -} +using System; +using BitFaster.Caching.Lru; +using BitFaster.Caching.Atomic; +using Shouldly; +using Xunit; + +namespace BitFaster.Caching.UnitTests.Lru +{ + public class ConcurrentLruBuilderTests + { + [Fact] + public void TestFastLru() + { + ICache lru = new ConcurrentLruBuilder() + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void TestMetricsLru() + { + ICache lru = new ConcurrentLruBuilder() + .WithMetrics() + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void TestFastTLru() + { + ICache lru = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void TestMetricsTLru() + { + ICache lru = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .WithMetrics() + .Build(); + + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(128); + } + + [Fact] + public void AsAsyncTestFastLru() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void AsAsyncTestMetricsLru() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .WithMetrics() + .AsAsyncCache() + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void AsAsyncTestFastTLru() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .AsAsyncCache() + .Build(); + + lru.ShouldBeOfType>(); + } + + [Fact] + public void AsAsyncTestMetricsTLru() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .WithMetrics() + .AsAsyncCache() + .Build(); + + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(128); + } + + [Fact] + public void TestComparer() + { + ICache fastLru = new ConcurrentLruBuilder() + .WithKeyComparer(StringComparer.OrdinalIgnoreCase) + .Build(); + + fastLru.GetOrAdd("a", k => 1); + fastLru.TryGet("A", out var value).ShouldBeTrue(); + } + + [Fact] + public void TestConcurrencyLevel() + { + var b = new ConcurrentLruBuilder() + .WithConcurrencyLevel(-1); + + Action constructor = () => { var x = b.Build(); }; + + constructor.ShouldThrow(); + } + + [Fact] + public void TestIntCapacity() + { + ICache lru = new ConcurrentLruBuilder() + .WithCapacity(3) + .Build(); + + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + [Fact] + public void TestPartitionCapacity() + { + ICache lru = new ConcurrentLruBuilder() + .WithCapacity(new FavorWarmPartition(6)) + .Build(); + + lru.Policy.Eviction.Value.Capacity.ShouldBe(6); + } + + [Fact] + public void TestExpireAfterAccess() + { + ICache expireAfterAccess = new ConcurrentLruBuilder() + .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) + .Build(); + + expireAfterAccess.Metrics.HasValue.ShouldBeFalse(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); + } + + [Fact] + public void TestExpireAfterAccessWithMetrics() + { + ICache expireAfterAccess = new ConcurrentLruBuilder() + .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) + .WithMetrics() + .Build(); + + expireAfterAccess.Metrics.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.HasValue.ShouldBeTrue(); + expireAfterAccess.Policy.ExpireAfterAccess.Value.TimeToLive.ShouldBe(TimeSpan.FromSeconds(1)); + expireAfterAccess.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); + } + + [Fact] + public void TestExpireAfterReadAndExpireAfterWriteThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) + .WithExpireAfterWrite(TimeSpan.FromSeconds(2)); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestExpireAfter() + { + ICache expireAfter = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .Build(); + + expireAfter.Metrics.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfter.HasValue.ShouldBeTrue(); + + expireAfter.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); + } + + [Fact] + public void TestAsyncExpireAfter() + { + IAsyncCache expireAfter = new ConcurrentLruBuilder() + .AsAsyncCache() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .Build(); + + expireAfter.Metrics.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfter.HasValue.ShouldBeTrue(); + + expireAfter.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); + } + + [Fact] + public void TestExpireAfterWithMetrics() + { + ICache expireAfter = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .WithMetrics() + .Build(); + + expireAfter.Metrics.HasValue.ShouldBeTrue(); + expireAfter.Policy.ExpireAfter.HasValue.ShouldBeTrue(); + + expireAfter.Policy.ExpireAfterAccess.HasValue.ShouldBeFalse(); + expireAfter.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); + } + + [Fact] + public void TestExpireAfterWriteAndExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestExpireAfterAccessAndExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestExpireAfterAccessAndWriteAndExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfterWrite(TimeSpan.FromSeconds(1)) + .WithExpireAfterAccess(TimeSpan.FromSeconds(1)) + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestScopedWithExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .AsScopedCache(); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestScopedAtomicWithExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .AsScopedCache() + .WithAtomicGetOrAdd(); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestAsyncScopedWithExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .AsAsyncCache() + .AsScopedCache(); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + [Fact] + public void TestAsyncScopedAtomicWithExpireAfterThrows() + { + var builder = new ConcurrentLruBuilder() + .WithExpireAfter(new TestExpiryCalculator((k, v) => Duration.FromMinutes(5))) + .AsAsyncCache() + .AsScopedCache() + .WithAtomicGetOrAdd(); + + Action act = () => builder.Build(); + act.ShouldThrow(); + } + + // There are 15 combinations to test: + // ----------------------------- + //1 WithAtomic + //2 WithScoped + //3 AsAsync + // + // ----------------------------- + //4 WithAtomic + // WithScoped + // + //5 WithScoped + // WithAtomic + // + //6 AsAsync + // WithScoped + // + //7 WithScoped + // AsAsync + // + //8 WithAtomic + // AsAsync + // + //9 AsAsync + // WithAtomic + // + // ----------------------------- + //10 WithAtomic + // WithScoped + // AsAsync + // + //11 WithAtomic + // AsAsync + // WithScoped + // + //12 WithScoped + // WithAtomic + // AsAsync + // + //13 WithScoped + // AsAsync + // WithAtomic + // + //14 AsAsync + // WithScoped + // WithAtomic + // + //15 AsAsync + // WithAtomic + // WithScoped + + // 1 + [Fact] + public void WithScopedValues() + { + IScopedCache lru = new ConcurrentLruBuilder() + .AsScopedCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + // 2 + [Fact] + public void WithAtomicFactory() + { + ICache lru = new ConcurrentLruBuilder() + .WithAtomicGetOrAdd() + .WithCapacity(3) + .Build(); + + lru.ShouldBeOfType>(); + } + + // 3 + [Fact] + public void AsAsync() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 4 + [Fact] + public void WithAtomicWithScope() + { + IScopedCache lru = new ConcurrentLruBuilder() + .WithAtomicGetOrAdd() + .AsScopedCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + // 5 + [Fact] + public void WithScopedWithAtomic() + { + IScopedCache lru = new ConcurrentLruBuilder() + .AsScopedCache() + .WithAtomicGetOrAdd() + .WithCapacity(3) + .Build(); + + lru.ShouldBeOfType>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + // 6 + [Fact] + public void AsAsyncWithScoped() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .AsScopedCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + // 7 + [Fact] + public void WithScopedAsAsync() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsScopedCache() + .AsAsyncCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + lru.Policy.Eviction.Value.Capacity.ShouldBe(3); + } + + // 8 + [Fact] + public void WithAtomicAsAsync() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .WithAtomicGetOrAdd() + .AsAsyncCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 9 + [Fact] + public void AsAsyncWithAtomic() + { + IAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .WithAtomicGetOrAdd() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 10 + [Fact] + public void WithAtomicWithScopedAsAsync() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .WithAtomicGetOrAdd() + .AsScopedCache() + .AsAsyncCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 11 + [Fact] + public void WithAtomicAsAsyncWithScoped() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .WithAtomicGetOrAdd() + .AsAsyncCache() + .AsScopedCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 12 + [Fact] + public void WithScopedWithAtomicAsAsync() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsScopedCache() + .WithAtomicGetOrAdd() + .AsAsyncCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 13 + [Fact] + public void WithScopedAsAsyncWithAtomic() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsScopedCache() + .AsAsyncCache() + .WithAtomicGetOrAdd() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 14 + [Fact] + public void AsAsyncWithScopedWithAtomic() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .AsScopedCache() + .WithAtomicGetOrAdd() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + + // 15 + [Fact] + public void AsAsyncWithAtomicWithScoped() + { + IScopedAsyncCache lru = new ConcurrentLruBuilder() + .AsAsyncCache() + .WithAtomicGetOrAdd() + .AsScopedCache() + .WithCapacity(3) + .Build(); + + lru.ShouldBeAssignableTo>(); + } + } +} diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruSoakTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruSoakTests.cs index def2f841..fc41360c 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruSoakTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruSoakTests.cs @@ -1,13 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; using Xunit.Abstractions; -using static BitFaster.Caching.UnitTests.Lru.LruItemSoakTests; namespace BitFaster.Caching.UnitTests.Lru { @@ -43,7 +41,7 @@ await Threaded.Run(4, () => { this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys)); // allow +/- 1 variance for capacity - lru.Count.Should().BeInRange(7, 10); + lru.Count.ShouldBeInRange(7, 10); RunIntegrityCheck(); } } @@ -64,7 +62,7 @@ await Threaded.RunAsync(4, async () => { this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys)); // allow +/- 1 variance for capacity - lru.Count.Should().BeInRange(7, 10); + lru.Count.ShouldBeInRange(7, 10); RunIntegrityCheck(); } } @@ -86,7 +84,7 @@ await Threaded.Run(4, () => { this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys)); // allow +/- 1 variance for capacity - lru.Count.Should().BeInRange(7, 10); + lru.Count.ShouldBeInRange(7, 10); RunIntegrityCheck(); } } @@ -108,7 +106,7 @@ await Threaded.RunAsync(4, async () => { this.testOutputHelper.WriteLine(string.Join(" ", lru.Keys)); // allow +/- 1 variance for capacity - lru.Count.Should().BeInRange(7, 10); + lru.Count.ShouldBeInRange(7, 10); RunIntegrityCheck(); } } @@ -234,7 +232,7 @@ await Threaded.Run(4, () => } }); - cache.Metrics.Value.Evicted.Should().Be(0); + cache.Metrics.Value.Evicted.ShouldBe(0); } [Fact] @@ -253,7 +251,7 @@ public async Task WhenConcurrentUpdateAndRemoveKvp() for (var i = 0; i < 100_000; i++) { lru.AddOrUpdate(5, "a"); - lru.TryGet(5, out _).Should().BeTrue("key 'a' should not be deleted"); + lru.TryGet(5, out _).ShouldBeTrue("key 'a' should not be deleted"); lru.AddOrUpdate(5, "x"); } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs index e52015d3..ba0b912b 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections; @@ -46,7 +46,7 @@ public void WhenConcurrencyIsLessThan1CtorThrows() { Action constructor = () => { var x = new ConcurrentLru(0, 3, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -54,7 +54,7 @@ public void WhenCapacityIsLessThan3CtorThrows() { Action constructor = () => { var x = new ConcurrentLru(1, 2, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -63,7 +63,7 @@ public void WhenPartitionIsNullCtorThrows() ICapacityPartition partition = null; Action constructor = () => { var x = new ConcurrentLru(1, partition, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -72,7 +72,7 @@ public void WhenPartitionIsInvalidThrows() var p = new TestCapacityPartition { Cold = 2, Warm = 0, Hot = 2 }; Action constructor = () => { var x = new ConcurrentLru(1, p, EqualityComparer.Default); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -80,7 +80,7 @@ public void WhenComparerIsNullCtorThrows() { Action constructor = () => { var x = new ConcurrentLru(1, 3, null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -93,9 +93,9 @@ public void WhenCapacityIs4HotHasCapacity1AndColdHasCapacity1() lru.GetOrAdd(i, x => x); } - lru.HotCount.Should().Be(1); - lru.ColdCount.Should().Be(1); - lru.Capacity.Should().Be(4); + lru.HotCount.ShouldBe(1); + lru.ColdCount.ShouldBe(1); + lru.Capacity.ShouldBe(4); } [Fact] @@ -108,9 +108,9 @@ public void WhenCapacityIs5HotHasCapacity1AndColdHasCapacity2() lru.GetOrAdd(i, x => x); } - lru.HotCount.Should().Be(1); - lru.ColdCount.Should().Be(2); - lru.Capacity.Should().Be(5); + lru.HotCount.ShouldBe(1); + lru.ColdCount.ShouldBe(2); + lru.Capacity.ShouldBe(5); } [Fact] @@ -118,52 +118,52 @@ public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue() { var x = new ConcurrentLru(3); - x.GetOrAdd(1, k => k).Should().Be(1); + x.GetOrAdd(1, k => k).ShouldBe(1); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); - lru.Count.Should().Be(1); + lru.Count.ShouldBe(1); } [Fact] public async Task WhenItemIsAddedCountIsCorrectAsync() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); await lru.GetOrAddAsync(0, valueFactory.CreateAsync); - lru.Count.Should().Be(1); + lru.Count.ShouldBe(1); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + lru.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] public void WhenItemsAddedGenericEnumerateContainsKvps() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.Should().BeEquivalentTo(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + lru.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); } [Fact] public void WhenItemsAddedEnumerateContainsKvps() { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); var enumerable = (IEnumerable)lru; - enumerable.Should().BeEquivalentTo(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + enumerable.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); } [Fact] @@ -171,7 +171,7 @@ public void FromColdWarmupFillsWarmQueue() { this.Warmup(); - this.lru.Count.Should().Be(9); + this.lru.Count.ShouldBe(9); } [Fact] @@ -180,8 +180,8 @@ public void WhenItemExistsTryGetReturnsValueAndTrue() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - result.Should().Be(true); - value.Should().Be("1"); + result.ShouldBe(true); + value.ShouldBe("1"); } [Fact] @@ -190,14 +190,14 @@ public void WhenItemDoesNotExistTryGetReturnsNullAndFalse() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(2, out var value); - result.Should().Be(false); - value.Should().BeNull(); + result.ShouldBe(false); + value.ShouldBeNull(); } [Fact] public void MetricsAreEnabled() { - lru.Metrics.HasValue.Should().BeTrue(); + lru.Metrics.HasValue.ShouldBeTrue(); } [Fact] @@ -206,7 +206,7 @@ public void WhenItemIsAddedThenRetrievedMetricHitRatioIsHalf() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.HitRatio.Should().Be(0.5); + lru.Metrics.Value.HitRatio.ShouldBe(0.5); } [Fact] @@ -215,7 +215,7 @@ public void WhenItemIsAddedThenRetrievedTotalIs2() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - lru.Metrics.Value.Total.Should().Be(2); + lru.Metrics.Value.Total.ShouldBe(2); } [Fact] @@ -228,13 +228,13 @@ public void WhenRefToMetricsIsCapturedResultIsCorrect() lru.GetOrAdd(1, valueFactory.Create); bool result = lru.TryGet(1, out var value); - m.Value.HitRatio.Should().Be(0.5); + m.Value.HitRatio.ShouldBe(0.5); } [Fact] public void ExpireAfterWriteHasValueIsFalse() { - this.lru.Policy.ExpireAfterWrite.HasValue.Should().BeFalse(); + this.lru.Policy.ExpireAfterWrite.HasValue.ShouldBeFalse(); } [Fact] @@ -243,8 +243,8 @@ public void WhenKeyIsRequestedItIsCreatedAndCached() var result1 = lru.GetOrAdd(1, valueFactory.Create); var result2 = lru.GetOrAdd(1, valueFactory.Create); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -253,8 +253,8 @@ public void WhenKeyIsRequestedWithArgItIsCreatedAndCached() var result1 = lru.GetOrAdd(1, valueFactory.Create, "x"); var result2 = lru.GetOrAdd(1, valueFactory.Create, "y"); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -263,8 +263,8 @@ public async Task WhenKeyIsRequestedItIsCreatedAndCachedAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -273,8 +273,8 @@ public async Task WhenKeyIsRequestedWithArgItIsCreatedAndCachedAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync, "x"); var result2 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync, "y"); - valueFactory.timesCalled.Should().Be(1); - result1.Should().Be(result2); + valueFactory.timesCalled.ShouldBe(1); + result1.ShouldBe(result2); } [Fact] @@ -283,10 +283,10 @@ public void WhenDifferentKeysAreRequestedValueIsCreatedForEach() var result1 = lru.GetOrAdd(1, valueFactory.Create); var result2 = lru.GetOrAdd(2, valueFactory.Create); - valueFactory.timesCalled.Should().Be(2); + valueFactory.timesCalled.ShouldBe(2); - result1.Should().Be("1"); - result2.Should().Be("2"); + result1.ShouldBe("1"); + result2.ShouldBe("2"); } [Fact] @@ -295,10 +295,10 @@ public async Task WhenDifferentKeysAreRequesteValueIsCreatedForEachAsync() var result1 = await lru.GetOrAddAsync(1, valueFactory.CreateAsync); var result2 = await lru.GetOrAddAsync(2, valueFactory.CreateAsync); - valueFactory.timesCalled.Should().Be(2); + valueFactory.timesCalled.ShouldBe(2); - result1.Should().Be("1"); - result2.Should().Be("2"); + result1.ShouldBe("1"); + result2.ShouldBe("2"); } [Fact] @@ -308,8 +308,8 @@ public void WhenValuesAreNotReadAndMoreKeysRequestedThanCapacityCountDoesNotIncr var result = lru.GetOrAdd(1, valueFactory.Create); - lru.Count.Should().Be(9); - valueFactory.timesCalled.Should().Be(10); + lru.Count.ShouldBe(9); + valueFactory.timesCalled.ShouldBe(10); } [Fact] @@ -327,8 +327,8 @@ public void WhenValuesAreReadAndMoreKeysRequestedThanCapacityCountIsBounded() } } - lru.Count.Should().Be(capacity); - valueFactory.timesCalled.Should().Be(capacity + 1); + lru.Count.ShouldBe(capacity); + valueFactory.timesCalled.ShouldBe(capacity + 1); } [Fact] @@ -348,7 +348,7 @@ public void WhenKeysAreContinuouslyRequestedInTheOrderTheyAreAddedCountIsBounded } testOutputHelper.WriteLine($"Total: {lru.Count} Hot: {lru.HotCount} Warm: {lru.WarmCount} Cold: {lru.ColdCount}"); - lru.Count.Should().BeLessOrEqualTo(capacity + 1); + lru.Count.ShouldBeLessThanOrEqualTo(capacity + 1); } } @@ -394,7 +394,7 @@ public void WhenKeysAreContinuouslyRequestedInTheOrderTheyAreAddedCountIsBounded lru.GetOrAdd(j, valueFactory.Create); } - lru.Count.Should().BeLessOrEqualTo(capacity + 1, $"Total: {lru.Count} Hot: {lru.HotCount} Warm: {lru.WarmCount} Cold: {lru.ColdCount}"); + lru.Count.ShouldBeLessThanOrEqualTo(capacity + 1, $"Total: {lru.Count} Hot: {lru.HotCount} Warm: {lru.WarmCount} Cold: {lru.ColdCount}"); } } @@ -415,7 +415,7 @@ public void WhenValueIsNotTouchedAndExpiresFromHotValueIsBumpedToCold() lru.GetOrAdd(8, valueFactory.Create); lru.GetOrAdd(9, valueFactory.Create); - lru.TryGet(0, out var value).Should().BeFalse(); + lru.TryGet(0, out var value).ShouldBeFalse(); } [Fact] @@ -436,7 +436,7 @@ public void WhenValueIsTouchedAndExpiresFromHotValueIsBumpedToWarm() lru.GetOrAdd(8, valueFactory.Create); lru.GetOrAdd(9, valueFactory.Create); - lru.TryGet(0, out var value).Should().BeTrue(); + lru.TryGet(0, out var value).ShouldBeTrue(); } [Fact] @@ -459,7 +459,7 @@ public void WhenValueIsTouchedAndExpiresFromColdItIsBumpedToWarm() lru.GetOrAdd(8, valueFactory.Create); lru.GetOrAdd(9, valueFactory.Create); - lru.TryGet(0, out var value).Should().BeTrue(); + lru.TryGet(0, out var value).ShouldBeTrue(); } [Fact] @@ -482,7 +482,7 @@ public void WhenValueIsNotTouchedAndExpiresFromColdItIsRemoved() lru.GetOrAdd(8, valueFactory.Create); lru.GetOrAdd(9, valueFactory.Create); - lru.TryGet(0, out var value).Should().BeFalse(); + lru.TryGet(0, out var value).ShouldBeFalse(); } [Fact] @@ -508,14 +508,14 @@ public void WhenValueIsNotTouchedAndExpiresFromWarmValueIsBumpedToCold() lru.GetOrAdd(9, valueFactory.Create); // verify 0 is present, but don't touch it - lru.Keys.Should().Contain(0); + lru.Keys.ShouldContain(0); // push 7,8,9 to cold, evict 0 lru.GetOrAdd(10, valueFactory.Create); lru.GetOrAdd(11, valueFactory.Create); lru.GetOrAdd(12, valueFactory.Create); - lru.TryGet(0, out var value).Should().BeFalse(); + lru.TryGet(0, out var value).ShouldBeFalse(); } [Fact] @@ -541,14 +541,14 @@ public void WhenValueIsTouchedAndExpiresFromWarmValueIsBumpedBackIntoWarm() lru.GetOrAdd(9, valueFactory.Create); // Touch 0 - lru.TryGet(0, out var value).Should().BeTrue(); + lru.TryGet(0, out var value).ShouldBeTrue(); // push 7,8,9 to cold, cycle 0 back to warm lru.GetOrAdd(10, valueFactory.Create); lru.GetOrAdd(11, valueFactory.Create); lru.GetOrAdd(12, valueFactory.Create); - lru.TryGet(0, out value).Should().BeTrue(); + lru.TryGet(0, out value).ShouldBeTrue(); } [Fact] @@ -562,22 +562,22 @@ public void WhenValueExpiresItIsDisposed() lruOfDisposable.GetOrAdd(i, disposableValueFactory.Create); } - disposableValueFactory.Items[0].IsDisposed.Should().BeTrue(); + disposableValueFactory.Items[0].IsDisposed.ShouldBeTrue(); - disposableValueFactory.Items[1].IsDisposed.Should().BeFalse(); - disposableValueFactory.Items[2].IsDisposed.Should().BeFalse(); - disposableValueFactory.Items[3].IsDisposed.Should().BeFalse(); - disposableValueFactory.Items[4].IsDisposed.Should().BeFalse(); - disposableValueFactory.Items[5].IsDisposed.Should().BeFalse(); - disposableValueFactory.Items[6].IsDisposed.Should().BeFalse(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeFalse(); + disposableValueFactory.Items[2].IsDisposed.ShouldBeFalse(); + disposableValueFactory.Items[3].IsDisposed.ShouldBeFalse(); + disposableValueFactory.Items[4].IsDisposed.ShouldBeFalse(); + disposableValueFactory.Items[5].IsDisposed.ShouldBeFalse(); + disposableValueFactory.Items[6].IsDisposed.ShouldBeFalse(); } [Fact] public void WhenAddingNullValueCanBeAddedAndRemoved() { - lru.GetOrAdd(1, _ => null).Should().BeNull(); + lru.GetOrAdd(1, _ => null).ShouldBeNull(); lru.AddOrUpdate(1, null); - lru.TryRemove(1).Should().BeTrue(); + lru.TryRemove(1).ShouldBeTrue(); } [Fact] @@ -595,15 +595,15 @@ public void WhenValueEvictedItemRemovedEventIsFired() lruEvents.GetOrAdd(i + 1, i => i + 1); } - removedItems.Count.Should().Be(2); + removedItems.Count.ShouldBe(2); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be(2); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe(2); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Evicted); - removedItems[1].Key.Should().Be(4); - removedItems[1].Value.Should().Be(5); - removedItems[1].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[1].Key.ShouldBe(4); + removedItems[1].Value.ShouldBe(5); + removedItems[1].Reason.ShouldBe(ItemRemovedReason.Evicted); } [Fact] @@ -613,7 +613,7 @@ public void WhenValuesAreEvictedEvictionMetricCountsEvicted() this.lru.GetOrAdd(1, valueFactory.Create); - this.lru.Metrics.Value.Evicted.Should().Be(1); + this.lru.Metrics.Value.Evicted.ShouldBe(1); } [Fact] @@ -629,7 +629,7 @@ public void WhenItemRemovedEventIsUnregisteredEventIsNotFired() lruEvents.GetOrAdd(i + 1, i => i + 1); } - removedItems.Count.Should().Be(0); + removedItems.Count.ShouldBe(0); } [Fact] @@ -637,8 +637,8 @@ public void WhenKeyExistsTryRemoveRemovesItemAndReturnsTrue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(1).Should().BeTrue(); - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryRemove(1).ShouldBeTrue(); + lru.TryGet(1, out var value).ShouldBeFalse(); } [Fact] @@ -646,8 +646,8 @@ public void WhenKeyExistsTryRemoveReturnsValue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(1, out var value).Should().BeTrue(); - value.Should().Be("1"); + lru.TryRemove(1, out var value).ShouldBeTrue(); + value.ShouldBe("1"); } [Fact] @@ -659,7 +659,7 @@ public void WhenItemIsRemovedItIsDisposed() lruOfDisposable.GetOrAdd(1, disposableValueFactory.Create); lruOfDisposable.TryRemove(1); - disposableValueFactory.Items[1].IsDisposed.Should().BeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeTrue(); } [Fact] @@ -670,12 +670,12 @@ public void WhenItemIsRemovedRemovedEventIsFired() lruEvents.GetOrAdd(1, i => i + 2); - lruEvents.TryRemove(1).Should().BeTrue(); + lruEvents.TryRemove(1).ShouldBeTrue(); - removedItems.Count().Should().Be(1); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be(3); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Removed); + removedItems.Count().ShouldBe(1); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe(3); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Removed); } [Fact] @@ -683,7 +683,7 @@ public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryRemove(2).Should().BeFalse(); + lru.TryRemove(2).ShouldBeFalse(); } [Fact] @@ -695,7 +695,7 @@ public void WhenRepeatedlyAddingAndRemovingSameValueLruRemainsInConsistentState( // Because TryRemove leaves the item in the queue, when it is eventually removed // from the cold queue, it should not remove the newly created value. lru.GetOrAdd(1, valueFactory.Create); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); lru.TryRemove(1); } } @@ -705,10 +705,10 @@ public void WhenKeyExistsTryUpdateUpdatesValueAndReturnsTrue() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(1, "2").Should().BeTrue(); + lru.TryUpdate(1, "2").ShouldBeTrue(); lru.TryGet(1, out var value); - value.Should().Be("2"); + value.ShouldBe("2"); } [Fact] @@ -721,7 +721,7 @@ public void WhenKeyExistsTryUpdateDisposesOldValue() lruOfDisposable.GetOrAdd(1, disposableValueFactory.Create); lruOfDisposable.TryUpdate(1, newValue); - disposableValueFactory.Items[1].IsDisposed.Should().BeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeTrue(); } [Fact] @@ -729,7 +729,7 @@ public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(2, "3").Should().BeFalse(); + lru.TryUpdate(2, "3").ShouldBeFalse(); } // backcompat: remove conditional compile @@ -739,9 +739,9 @@ public void WhenKeyExistsTryUpdateIncrementsUpdateCount() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(1, "2").Should().BeTrue(); + lru.TryUpdate(1, "2").ShouldBeTrue(); - lru.Metrics.Value.Updated.Should().Be(1); + lru.Metrics.Value.Updated.ShouldBe(1); } [Fact] @@ -749,9 +749,9 @@ public void WhenKeyDoesNotExistTryUpdateDoesNotIncrementCounter() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryUpdate(2, "3").Should().BeFalse(); + lru.TryUpdate(2, "3").ShouldBeFalse(); - lru.Metrics.Value.Updated.Should().Be(0); + lru.Metrics.Value.Updated.ShouldBe(0); } #endif [Fact] @@ -759,8 +759,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() { lru.AddOrUpdate(1, "1"); - lru.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be("1"); + lru.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe("1"); } [Fact] @@ -769,8 +769,8 @@ public void WhenKeyExistsAddOrUpdateUpdatesExistingItem() lru.AddOrUpdate(1, "1"); lru.AddOrUpdate(1, "2"); - lru.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be("2"); + lru.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe("2"); } [Fact] @@ -782,8 +782,8 @@ public void WhenKeyExistsAddOrUpdateGuidUpdatesExistingItem() lru2.AddOrUpdate(1, new Guid(1, 0, 0, b)); lru2.AddOrUpdate(1, new Guid(2, 0, 0, b)); - lru2.TryGet(1, out var value).Should().BeTrue(); - value.Should().Be(new Guid(2, 0, 0, b)); + lru2.TryGet(1, out var value).ShouldBeTrue(); + value.ShouldBe(new Guid(2, 0, 0, b)); } [Fact] @@ -796,7 +796,7 @@ public void WhenKeyExistsAddOrUpdateDisposesOldValue() lruOfDisposable.GetOrAdd(1, disposableValueFactory.Create); lruOfDisposable.AddOrUpdate(1, newValue); - disposableValueFactory.Items[1].IsDisposed.Should().BeTrue(); + disposableValueFactory.Items[1].IsDisposed.ShouldBeTrue(); } [Fact] @@ -807,8 +807,8 @@ public void WhenKeyDoesNotExistAddOrUpdateMaintainsLruOrder() lru.AddOrUpdate(3, "3"); lru.AddOrUpdate(4, "4"); - lru.HotCount.Should().Be(3); - lru.WarmCount.Should().Be(1); // items must have been enqueued and cycled for one of them to reach the warm queue + lru.HotCount.ShouldBe(3); + lru.WarmCount.ShouldBe(1); // items must have been enqueued and cycled for one of them to reach the warm queue } // backcompat: remove conditional compile @@ -824,10 +824,10 @@ public void WhenItemExistsAddOrUpdateFiresUpdateEvent() lruEvents.AddOrUpdate(1, 3); - this.updatedItems.Count.Should().Be(1); - this.updatedItems[0].Key.Should().Be(1); - this.updatedItems[0].OldValue.Should().Be(2); - this.updatedItems[0].NewValue.Should().Be(3); + this.updatedItems.Count.ShouldBe(1); + this.updatedItems[0].Key.ShouldBe(1); + this.updatedItems[0].OldValue.ShouldBe(2); + this.updatedItems[0].NewValue.ShouldBe(3); } [Fact] @@ -841,10 +841,10 @@ public void WhenItemExistsTryUpdateFiresUpdateEvent() lruEvents.TryUpdate(1, 3); - this.updatedItems.Count.Should().Be(1); - this.updatedItems[0].Key.Should().Be(1); - this.updatedItems[0].OldValue.Should().Be(2); - this.updatedItems[0].NewValue.Should().Be(3); + this.updatedItems.Count.ShouldBe(1); + this.updatedItems[0].Key.ShouldBe(1); + this.updatedItems[0].OldValue.ShouldBe(2); + this.updatedItems[0].NewValue.ShouldBe(3); } [Fact] @@ -859,7 +859,7 @@ public void WhenItemUpdatedEventIsUnregisteredEventIsNotFired() lruEvents.AddOrUpdate(1, 2); lruEvents.AddOrUpdate(1, 2); - updatedItems.Count.Should().Be(0); + updatedItems.Count.ShouldBe(0); } #endif @@ -867,7 +867,7 @@ public void WhenItemUpdatedEventIsUnregisteredEventIsNotFired() public void WhenCacheIsEmptyClearIsNoOp() { lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } [Fact] @@ -878,12 +878,12 @@ public void WhenItemsExistClearRemovesAllItems() lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); // verify queues are purged - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } // This is a special case: @@ -905,7 +905,7 @@ public void WhenCacheIsSize3ItemsExistAndItemsAccessedClearRemovesAllItems() lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } [Theory] @@ -939,12 +939,12 @@ public void WhenItemsExistAndItemsAccessedClearRemovesAllItems(int itemCount) this.testOutputHelper.WriteLine("LRU " + string.Join(" ", lru.Keys)); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); // verify queues are purged - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } [Fact] @@ -956,14 +956,14 @@ public void WhenWarmThenClearedIsWarmIsReset() } lru.Clear(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); for (int i = 0; i < 20; i++) { lru.GetOrAdd(i, k => k.ToString()); } - lru.Count.Should().Be(capacity.Hot + capacity.Warm + capacity.Cold); + lru.Count.ShouldBe(capacity.Hot + capacity.Warm + capacity.Cold); } [Fact] @@ -975,14 +975,14 @@ public void WhenWarmThenTrimIsWarmIsReset() } lru.Trim(6); - lru.Count.Should().Be(3); + lru.Count.ShouldBe(3); for (int i = 0; i < 20; i++) { lru.GetOrAdd(i, k => k.ToString()); } - lru.Count.Should().Be(capacity.Hot + capacity.Warm + capacity.Cold); + lru.Count.ShouldBe(capacity.Hot + capacity.Warm + capacity.Cold); } [Fact] @@ -999,7 +999,7 @@ public void WhenItemsAreDisposableClearDisposesItemsOnRemove() lruOfDisposable.Clear(); - items.All(i => i.IsDisposed == true).Should().BeTrue(); + items.All(i => i.IsDisposed == true).ShouldBeTrue(); } [Fact] @@ -1015,24 +1015,24 @@ public void WhenItemsArClearedAnEventIsFired() lruEvents.Clear(); - removedItems.Count.Should().Be(6); + removedItems.Count.ShouldBe(6); for (int i = 0; i < 6; i++) { - removedItems[i].Reason.Should().Be(ItemRemovedReason.Cleared); + removedItems[i].Reason.ShouldBe(ItemRemovedReason.Cleared); } } [Fact] public void WhenTrimCountIsZeroThrows() { - lru.Invoking(l => lru.Trim(0)).Should().Throw(); + Should.Throw(() => lru.Trim(0)); } [Fact] public void WhenTrimCountIsMoreThanCapacityThrows() { - lru.Invoking(l => lru.Trim(hotCap + warmCap + coldCap + 1)).Should().Throw(); + Should.Throw(() => lru.Trim(hotCap + warmCap + coldCap + 1)); } [Theory] @@ -1070,7 +1070,7 @@ public void WhenColdItemsExistTrimRemovesExpectedItemCount(int trimCount, int[] lru.Trim(trimCount); - lru.Keys.Should().BeEquivalentTo(expected); + lru.Keys.ShouldBe(expected); } [Theory] @@ -1102,7 +1102,7 @@ public void WhenHotAndWarmItemsExistTrimRemovesExpectedItemCount(int itemCount, lru.Trim(itemCount); - lru.Keys.Should().BeEquivalentTo(expected); + lru.Keys.ShouldBe(expected); } [Theory] @@ -1127,7 +1127,7 @@ public void WhenHotItemsExistTrimRemovesExpectedItemCount(int itemCount, int[] e lru.Trim(itemCount); - lru.Keys.Should().BeEquivalentTo(expected); + lru.Keys.ShouldBe(expected); } [Theory] @@ -1173,7 +1173,7 @@ public void WhenColdItemsAreTouchedTrimRemovesExpectedItemCount(int trimCount, i this.testOutputHelper.WriteLine("LRU " + string.Join(" ", lru.Keys)); this.testOutputHelper.WriteLine("exp " + string.Join(" ", expected)); - lru.Keys.Should().BeEquivalentTo(expected); + lru.Keys.ShouldBe(expected); } [Theory] @@ -1207,12 +1207,12 @@ public void WhenItemsExistAndItemsAccessedTrimRemovesAllItems(int itemCount) this.testOutputHelper.WriteLine("LRU " + string.Join(" ", lru.Keys)); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); // verify queues are purged - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } [Theory] @@ -1240,12 +1240,12 @@ public void WhenItemsRemovedClearRemovesAllItems(int itemCount) this.testOutputHelper.WriteLine("LRU " + string.Join(" ", lru.Keys)); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); // verify queues are purged - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } [Fact] @@ -1262,10 +1262,10 @@ public void WhenItemsAreDisposableTrimDisposesItems() lruOfDisposable.Trim(2); - items[0].IsDisposed.Should().BeTrue(); - items[1].IsDisposed.Should().BeTrue(); - items[2].IsDisposed.Should().BeFalse(); - items[3].IsDisposed.Should().BeFalse(); + items[0].IsDisposed.ShouldBeTrue(); + items[1].IsDisposed.ShouldBeTrue(); + items[2].IsDisposed.ShouldBeFalse(); + items[3].IsDisposed.ShouldBeFalse(); } [Fact] @@ -1281,15 +1281,15 @@ public void WhenItemsAreTrimmedAnEventIsFired() lruEvents.Trim(2); - removedItems.Count.Should().Be(2); + removedItems.Count.ShouldBe(2); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be(2); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Trimmed); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe(2); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Trimmed); - removedItems[1].Key.Should().Be(2); - removedItems[1].Value.Should().Be(3); - removedItems[1].Reason.Should().Be(ItemRemovedReason.Trimmed); + removedItems[1].Key.ShouldBe(2); + removedItems[1].Value.ShouldBe(3); + removedItems[1].Reason.ShouldBe(ItemRemovedReason.Trimmed); } @@ -1339,9 +1339,9 @@ public ConcurrentLruIntegrityChecker(ConcurrentLruCore cache) public void Validate() { // queue counters must be consistent with queues - this.hotQueue.Count.Should().Be(cache.HotCount, "hot queue has a corrupted count"); - this.warmQueue.Count.Should().Be(cache.WarmCount, "warm queue has a corrupted count"); - this.coldQueue.Count.Should().Be(cache.ColdCount, "cold queue has a corrupted count"); + this.hotQueue.Count.ShouldBe(cache.HotCount, "hot queue has a corrupted count"); + this.warmQueue.Count.ShouldBe(cache.WarmCount, "warm queue has a corrupted count"); + this.coldQueue.Count.ShouldBe(cache.ColdCount, "cold queue has a corrupted count"); // cache contents must be consistent with queued items ValidateQueue(cache, this.hotQueue, "hot"); @@ -1349,7 +1349,7 @@ public void Validate() ValidateQueue(cache, this.coldQueue, "cold"); // cache must be within capacity - cache.Count.Should().BeLessThanOrEqualTo(cache.Capacity + 1, "capacity out of valid range"); + cache.Count.ShouldBeLessThanOrEqualTo(cache.Capacity + 1, "capacity out of valid range"); } private void ValidateQueue(ConcurrentLruCore cache, ConcurrentQueue queue, string queueName) @@ -1365,12 +1365,12 @@ private void ValidateQueue(ConcurrentLruCore cache, ConcurrentQue { hotQueue.Union(warmQueue).Union(coldQueue) .Any(i => i.Key.Equals(item.Key) && !i.WasRemoved) - .Should().BeTrue($"{queueName} removed item {item.Key} was not removed"); + .ShouldBeTrue($"{queueName} removed item {item.Key} was not removed"); } } else { - dictionary.TryGetValue(item.Key, out var value).Should().BeTrue($"{queueName} item {item.Key} was not present"); + dictionary.TryGetValue(item.Key, out var value).ShouldBeTrue($"{queueName} item {item.Key} was not present"); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs index 69004bef..4632b71d 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections.Generic; @@ -39,13 +39,13 @@ public ConcurrentTLruTests() [Fact] public void CanExpireIsTrue() { - this.lru.Policy.ExpireAfterWrite.HasValue.Should().BeTrue(); + this.lru.Policy.ExpireAfterWrite.HasValue.ShouldBeTrue(); } [Fact] public void TimeToLiveIsCtorArg() { - this.lru.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(timeToLive); + this.lru.Policy.ExpireAfterWrite.Value.TimeToLive.ShouldBe(timeToLive); } [RetryFact] @@ -53,7 +53,7 @@ public void WhenItemIsNotExpiredItIsNotRemoved() { lru.GetOrAdd(1, valueFactory.Create); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); } [RetryFact] @@ -69,7 +69,7 @@ public void WhenItemIsExpiredItIsRemoved() timeToLive.MultiplyBy(ttlWaitMlutiplier), lru => { - lru.TryGet(1, out var value).Should().BeFalse(); + lru.TryGet(1, out var value).ShouldBeFalse(); } ); } @@ -88,7 +88,7 @@ public void WhenItemIsUpdatedTtlIsExtended() lru => { lru.TryUpdate(1, "3"); - lru.TryGet(1, out var value).Should().BeTrue(); + lru.TryGet(1, out var value).ShouldBeTrue(); } ); } @@ -108,15 +108,15 @@ public void WhenValueEvictedItemRemovedEventIsFired() lruEvents.GetOrAdd(i + 1, i => i + 1); } - removedItems.Count.Should().Be(2); + removedItems.Count.ShouldBe(2); - removedItems[0].Key.Should().Be(1); - removedItems[0].Value.Should().Be(2); - removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[0].Key.ShouldBe(1); + removedItems[0].Value.ShouldBe(2); + removedItems[0].Reason.ShouldBe(ItemRemovedReason.Evicted); - removedItems[1].Key.Should().Be(4); - removedItems[1].Value.Should().Be(5); - removedItems[1].Reason.Should().Be(ItemRemovedReason.Evicted); + removedItems[1].Key.ShouldBe(4); + removedItems[1].Value.ShouldBe(5); + removedItems[1].Reason.ShouldBe(ItemRemovedReason.Evicted); } [Fact] @@ -131,7 +131,7 @@ public void WhenItemRemovedEventIsUnregisteredEventIsNotFired() lruEvents.GetOrAdd(i + 1, i => i + 1); } - removedItems.Count.Should().Be(0); + removedItems.Count.ShouldBe(0); } [RetryFact] @@ -163,9 +163,9 @@ public void WhenItemsAreExpiredExpireRemovesExpiredItems() { lru.Policy.ExpireAfterWrite.Value.TrimExpired(); - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } ); } @@ -204,10 +204,10 @@ public void WhenExpiredItemsAreTrimmedCacheMarkedCold() lru.GetOrAdd(i, k => k.ToString()); } - lru.Count.Should().Be(lru.Policy.Eviction.Value.Capacity); + lru.Count.ShouldBe(lru.Policy.Eviction.Value.Capacity); var total = lru.HotCount + lru.WarmCount + lru.ColdCount; - total.Should().Be(lru.Policy.Eviction.Value.Capacity); + total.ShouldBe(lru.Policy.Eviction.Value.Capacity); } ); } @@ -238,10 +238,10 @@ public void WhenCacheHasExpiredAndFreshItemsExpireRemovesOnlyExpiredItems() lru.Policy.ExpireAfterWrite.Value.TrimExpired(); - lru.Count.Should().Be(3); + lru.Count.ShouldBe(3); var total = lru.HotCount + lru.WarmCount + lru.ColdCount; - total.Should().Be(3); + total.ShouldBe(3); } ); } @@ -264,11 +264,11 @@ public void WhenItemsAreExpiredTrimRemovesExpiredItems() { lru.Policy.Eviction.Value.Trim(1); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); - lru.HotCount.Should().Be(0); - lru.WarmCount.Should().Be(0); - lru.ColdCount.Should().Be(0); + lru.HotCount.ShouldBe(0); + lru.WarmCount.ShouldBe(0); + lru.ColdCount.ShouldBe(0); } ); } @@ -289,7 +289,7 @@ public void WhenItemsAreExpiredCountFiltersExpiredItems() timeToLive.MultiplyBy(ttlWaitMlutiplier), lru => { - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } ); } @@ -310,7 +310,7 @@ public void WhenItemsAreExpiredEnumerateFiltersExpiredItems() timeToLive.MultiplyBy(ttlWaitMlutiplier), lru => { - lru.Should().BeEquivalentTo(Array.Empty>()); + lru.ShouldBe(Array.Empty>()); } ); } @@ -320,7 +320,7 @@ public void ConstructWithDefaultCtorReturnsCapacity() { var x = new ConcurrentTLru(3, TimeSpan.FromSeconds(1)); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -328,7 +328,7 @@ public void ConstructCapacityCtorReturnsCapacity() { var x = new ConcurrentTLru(1, 3, EqualityComparer.Default, TimeSpan.FromSeconds(1)); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -336,7 +336,7 @@ public void ConstructPartitionCtorReturnsCapacity() { var x = new ConcurrentTLru(1, new EqualCapacityPartition(3), EqualityComparer.Default, TimeSpan.FromSeconds(1)); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/DiscretePolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/DiscretePolicyTests.cs index 84b58dbb..7579888d 100644 --- a/BitFaster.Caching.UnitTests/Lru/DiscretePolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/DiscretePolicyTests.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -22,13 +22,13 @@ public void WhenCalculatorNullThrows() { Action act = () => new DiscretePolicy(null); - act.Should().Throw(); + act.ShouldThrow(); } [Fact] public void TimeToLiveShouldBeZero() { - this.policy.TimeToLive.Should().Be(TimeSpan.Zero); + this.policy.TimeToLive.ShouldBe(TimeSpan.Zero); } [Fact] @@ -38,17 +38,17 @@ public void CreateItemInitializesKeyValueAndTicks() expiryCalculator.ExpireAfterCreate = (k, v) => { - k.Should().Be(1); - v.Should().Be(2); + k.ShouldBe(1); + v.ShouldBe(2); return timeToExpire; }; var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); - item.TickCount.Should().BeCloseTo(timeToExpire.raw + Duration.SinceEpoch().raw, DurationTests.epsilon); + item.TickCount.ShouldBe(timeToExpire.raw + Duration.SinceEpoch().raw); } [Fact] @@ -59,7 +59,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [Fact] @@ -72,7 +72,7 @@ public async Task TouchUpdatesTicksCount() this.policy.ShouldDiscard(item); // set the time in the policy this.policy.Touch(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -85,7 +85,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -95,7 +95,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() item.TickCount = item.TickCount - Duration.FromMilliseconds(11).raw; - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -105,13 +105,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() item.TickCount = item.TickCount - Duration.FromMilliseconds(9).raw; - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } @@ -124,7 +124,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -136,7 +136,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -148,7 +148,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private LongTickCountLruItem CreateItem(bool wasAccessed, bool isExpired) diff --git a/BitFaster.Caching.UnitTests/Lru/DisposableItem.cs b/BitFaster.Caching.UnitTests/Lru/DisposableItem.cs index 81e9950d..43405aff 100644 --- a/BitFaster.Caching.UnitTests/Lru/DisposableItem.cs +++ b/BitFaster.Caching.UnitTests/Lru/DisposableItem.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace BitFaster.Caching.UnitTests.Lru { diff --git a/BitFaster.Caching.UnitTests/Lru/DisposableValueFactory.cs b/BitFaster.Caching.UnitTests/Lru/DisposableValueFactory.cs index 41c95d50..70d9eb94 100644 --- a/BitFaster.Caching.UnitTests/Lru/DisposableValueFactory.cs +++ b/BitFaster.Caching.UnitTests/Lru/DisposableValueFactory.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using System.Threading.Tasks; namespace BitFaster.Caching.UnitTests.Lru diff --git a/BitFaster.Caching.UnitTests/Lru/EqualCapacityPartitionTests.cs b/BitFaster.Caching.UnitTests/Lru/EqualCapacityPartitionTests.cs index cea6adee..d12f43d9 100644 --- a/BitFaster.Caching.UnitTests/Lru/EqualCapacityPartitionTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/EqualCapacityPartitionTests.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -16,7 +12,7 @@ public void WhenCapacityBelow3Throws() { Action constructor = () => { var x = new EqualCapacityPartition(2); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Theory] @@ -28,9 +24,9 @@ public void EqualPartitioningCreatesEqualQueues(int totalCapacity, int expectedH { var p = new EqualCapacityPartition(totalCapacity); - p.Hot.Should().Be(expectedHot); - p.Warm.Should().Be(expectedWarm); - p.Cold.Should().Be(expectedCold); + p.Hot.ShouldBe(expectedHot); + p.Warm.ShouldBe(expectedWarm); + p.Cold.ShouldBe(expectedCold); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs b/BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs index 9c009f7c..41bbd018 100644 --- a/BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections.Generic; @@ -15,8 +15,8 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue() lru.GetOrAdd("foo", k => 1); - lru.TryGet("FOO", out var value).Should().BeTrue(); - value.Should().Be(1); + lru.TryGet("FOO", out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -24,7 +24,7 @@ public void ConstructWithDefaultCtorReturnsCapacity() { var x = new FastConcurrentLru(3); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -32,7 +32,7 @@ public void ConstructPartitionCtorReturnsCapacity() { var x = new FastConcurrentLru(1, new EqualCapacityPartition(3), EqualityComparer.Default); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -40,7 +40,7 @@ public void MetricsHasValueIsFalse() { var x = new FastConcurrentLru(3); - x.Metrics.HasValue.Should().BeFalse(); + x.Metrics.HasValue.ShouldBeFalse(); } [Fact] @@ -48,7 +48,7 @@ public void EventsHasValueIsFalse() { var x = new FastConcurrentLru(3); - x.Events.HasValue.Should().BeFalse(); + x.Events.HasValue.ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs b/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs index 39b92577..7f35c649 100644 --- a/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs @@ -1,8 +1,7 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections.Generic; -using System.Text; using Xunit; using System.Threading.Tasks; @@ -17,8 +16,8 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue() lru.GetOrAdd("foo", k => 1); - lru.TryGet("FOO", out var value).Should().BeTrue(); - value.Should().Be(1); + lru.TryGet("FOO", out var value).ShouldBeTrue(); + value.ShouldBe(1); } [Fact] @@ -26,7 +25,7 @@ public void ConstructWithDefaultCtorReturnsCapacity() { var x = new FastConcurrentTLru(3, TimeSpan.FromSeconds(1)); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -34,7 +33,7 @@ public void ConstructPartitionCtorReturnsCapacity() { var x = new FastConcurrentTLru(1, new EqualCapacityPartition(3), EqualityComparer.Default, TimeSpan.FromSeconds(1)); - x.Capacity.Should().Be(3); + x.Capacity.ShouldBe(3); } [Fact] @@ -51,7 +50,7 @@ public async Task WhenItemsAreExpiredExpireRemovesExpiredItems() lru.Policy.ExpireAfterWrite.Value.TrimExpired(); - lru.Count.Should().Be(0); + lru.Count.ShouldBe(0); } [Fact] @@ -59,7 +58,7 @@ public void MetricsHasValueIsFalse() { var x = new FastConcurrentTLru(3, TimeSpan.FromSeconds(1)); - x.Metrics.HasValue.Should().BeFalse(); + x.Metrics.HasValue.ShouldBeFalse(); } [Fact] @@ -67,7 +66,7 @@ public void EventsHasValueIsFalse() { var x = new FastConcurrentTLru(3, TimeSpan.FromSeconds(1)); - x.Events.HasValue.Should().BeFalse(); + x.Events.HasValue.ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/FavorWarmPartitionTests.cs b/BitFaster.Caching.UnitTests/Lru/FavorWarmPartitionTests.cs index 7e2c06d0..bee27c1b 100644 --- a/BitFaster.Caching.UnitTests/Lru/FavorWarmPartitionTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/FavorWarmPartitionTests.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -16,7 +12,7 @@ public void WhenCapacityBelow3Throws() { Action constructor = () => { var x = new FavorWarmPartition(2); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -24,7 +20,7 @@ public void WhenRatioBelow0Throws() { Action constructor = () => { var x = new FavorWarmPartition(5, 0.0); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -32,7 +28,7 @@ public void WhenRatioAbove1Throws() { Action constructor = () => { var x = new FavorWarmPartition(5, 1.0); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Theory] @@ -52,9 +48,9 @@ public void EqualPartitioningCreatesEqualQueues(int totalCapacity, int expectedH { var p = new FavorWarmPartition(totalCapacity); - p.Hot.Should().Be(expectedHot); - p.Warm.Should().Be(expectedWarm); - p.Cold.Should().Be(expectedCold); + p.Hot.ShouldBe(expectedHot); + p.Warm.ShouldBe(expectedWarm); + p.Cold.ShouldBe(expectedCold); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/LruInfoTests.cs b/BitFaster.Caching.UnitTests/Lru/LruInfoTests.cs index 133cb469..f25b0e30 100644 --- a/BitFaster.Caching.UnitTests/Lru/LruInfoTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/LruInfoTests.cs @@ -1,6 +1,6 @@ using System; using BitFaster.Caching.Lru.Builder; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -12,7 +12,7 @@ public void WhenExpiryNullGetExpiryReturnsNull() { var info = new LruInfo(); - info.GetExpiry().Should().BeNull(); + info.GetExpiry().ShouldBeNull(); } [Fact] @@ -23,7 +23,7 @@ public void WhenExpiryCalcValueTypeDoesNotMatchThrows() info.SetExpiry(new TestExpiryCalculator()); Action act = () => info.GetExpiry(); - act.Should().Throw(); + act.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/LruItemMemoryLayoutDumps.cs b/BitFaster.Caching.UnitTests/Lru/LruItemMemoryLayoutDumps.cs index a2ea5a43..bfa9c77b 100644 --- a/BitFaster.Caching.UnitTests/Lru/LruItemMemoryLayoutDumps.cs +++ b/BitFaster.Caching.UnitTests/Lru/LruItemMemoryLayoutDumps.cs @@ -1,5 +1,4 @@ -using System; -using BitFaster.Caching.Lru; +using BitFaster.Caching.Lru; using ObjectLayoutInspector; using Xunit; using Xunit.Abstractions; diff --git a/BitFaster.Caching.UnitTests/Lru/LruPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/LruPolicyTests.cs index 89cdc4ba..c421a853 100644 --- a/BitFaster.Caching.UnitTests/Lru/LruPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/LruPolicyTests.cs @@ -1,8 +1,6 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; -using System.Collections.Generic; -using System.Text; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -14,7 +12,7 @@ public class LruPolicyTests [Fact] public void TimeToLiveIsInfinite() { - this.policy.TimeToLive.Should().Be(new TimeSpan(0, 0, 0, 0, -1)); + this.policy.TimeToLive.ShouldBe(new TimeSpan(0, 0, 0, 0, -1)); } [Fact] @@ -22,8 +20,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -31,7 +29,7 @@ public void CreateItemInitializesWasAccessedFalse() { var item = this.policy.CreateItem(1, 2); - item.WasAccessed.Should().BeFalse(); + item.WasAccessed.ShouldBeFalse(); } [Fact] @@ -42,7 +40,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [Fact] @@ -50,7 +48,7 @@ public void WhenItemIsNotAccessedShouldDiscardIsFalse() { var item = this.policy.CreateItem(1, 2); - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] @@ -59,13 +57,13 @@ public void WhenItemIsAccessedShouldDiscardIsFalse() var item = this.policy.CreateItem(1, 2); item.WasAccessed = true; - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsFalse() { - this.policy.CanDiscard().Should().BeFalse(); + this.policy.CanDiscard().ShouldBeFalse(); } [Theory] @@ -75,7 +73,7 @@ public void RouteHot(bool wasAccessed, ItemDestination expectedDestination) { var item = CreateItem(wasAccessed); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -85,7 +83,7 @@ public void RouteWarm(bool wasAccessed, ItemDestination expectedDestination) { var item = CreateItem(wasAccessed); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -95,7 +93,7 @@ public void RouteCold(bool wasAccessed, ItemDestination expectedDestination) { var item = CreateItem(wasAccessed); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private LruItem CreateItem(bool wasAccessed) diff --git a/BitFaster.Caching.UnitTests/Lru/NoTelemetryPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/NoTelemetryPolicyTests.cs index 9b75347c..dcc3c162 100644 --- a/BitFaster.Caching.UnitTests/Lru/NoTelemetryPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/NoTelemetryPolicyTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using Xunit; @@ -11,61 +11,61 @@ public class NoTelemetryPolicyTests [Fact] public void HitRatioIsZero() { - counter.HitRatio.Should().Be(0); + counter.HitRatio.ShouldBe(0); } [Fact] public void TotalIsZero() { - counter.Total.Should().Be(0); + counter.Total.ShouldBe(0); } [Fact] public void HitsIsZero() { - counter.Hits.Should().Be(0); + counter.Hits.ShouldBe(0); } [Fact] public void MissesIsZero() { - counter.Misses.Should().Be(0); + counter.Misses.ShouldBe(0); } [Fact] public void UpdatedIsZero() { - counter.Updated.Should().Be(0); + counter.Updated.ShouldBe(0); } [Fact] public void EvictedIsZero() { - counter.Evicted.Should().Be(0); + counter.Evicted.ShouldBe(0); } [Fact] public void IncrementHitCountIsNoOp() { - counter.Invoking(c => c.IncrementHit()).Should().NotThrow(); + Should.NotThrow(() => counter.IncrementHit()); } [Fact] public void IncrementTotalCountIsNoOp() { - counter.Invoking(c => c.IncrementMiss()).Should().NotThrow(); + Should.NotThrow(() => counter.IncrementMiss()); } [Fact] public void OnItemUpdatedIsNoOp() { - counter.Invoking(c => c.OnItemUpdated(1, 2, 3)).Should().NotThrow(); + Should.NotThrow(() => counter.OnItemUpdated(1, 2, 3)); } [Fact] public void OnItemRemovedIsNoOp() { - counter.Invoking(c => c.OnItemRemoved(1, 2, ItemRemovedReason.Evicted)).Should().NotThrow(); + Should.NotThrow(() => counter.OnItemRemoved(1, 2, ItemRemovedReason.Evicted)); } [Fact] diff --git a/BitFaster.Caching.UnitTests/Lru/StopwatchTickConverterTests.cs b/BitFaster.Caching.UnitTests/Lru/StopwatchTickConverterTests.cs index 86450e8a..c7853a12 100644 --- a/BitFaster.Caching.UnitTests/Lru/StopwatchTickConverterTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/StopwatchTickConverterTests.cs @@ -1,6 +1,6 @@ using System; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Lru @@ -12,7 +12,7 @@ public void WhenConvertingToTicksIsReversable() { var timespan = TimeSpan.FromSeconds(1); - StopwatchTickConverter.FromTicks(StopwatchTickConverter.ToTicks(timespan)).Should().BeCloseTo(timespan, TimeSpan.FromMilliseconds(20)); + StopwatchTickConverter.FromTicks(StopwatchTickConverter.ToTicks(timespan)).ShouldBe(timespan, TimeSpan.FromMilliseconds(20)); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs b/BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs index 1acb2aaf..03db9d7f 100644 --- a/BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs +++ b/BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs @@ -1,6 +1,6 @@ #if NETCOREAPP3_1_OR_GREATER -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Threading.Tasks; @@ -18,7 +18,7 @@ public void WhenTtlIsTimeSpanMaxThrow() { Action constructor = () => { new TLruLongTicksPolicy(TimeSpan.MaxValue); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -26,20 +26,20 @@ public void WhenTtlIsZeroThrow() { Action constructor = () => { new TLruLongTicksPolicy(TimeSpan.Zero); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] public void WhenTtlIsMaxSetAsMax() { var policy = new TLruLongTicksPolicy(Duration.MaxRepresentable); - policy.TimeToLive.Should().BeCloseTo(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20)); + policy.TimeToLive.ShouldBe(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20)); } [Fact] public void TimeToLiveShouldBeTenSecs() { - this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10)); + this.policy.TimeToLive.ShouldBe(TimeSpan.FromSeconds(10)); } [Fact] @@ -47,8 +47,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -56,7 +56,7 @@ public void CreateItemInitializesTimestampToNow() { var item = this.policy.CreateItem(1, 2); - item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon); + item.TickCount.ShouldBe(Duration.SinceEpoch().raw); } [Fact] @@ -67,7 +67,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [Fact] @@ -80,7 +80,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -89,7 +89,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() var item = this.policy.CreateItem(1, 2); item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(11).raw; - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -98,13 +98,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() var item = this.policy.CreateItem(1, 2); item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(9).raw; - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } [Theory] @@ -116,7 +116,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -128,7 +128,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -140,7 +140,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private LongTickCountLruItem CreateItem(bool wasAccessed, bool isExpired) @@ -163,7 +163,7 @@ public void CanConvertToAndFromTicks() { var time = TimeSpan.FromSeconds(10); var ticks = TLruLongTicksPolicy.ToTicks(time); - TLruLongTicksPolicy.FromTicks(ticks).Should().Be(time); + TLruLongTicksPolicy.FromTicks(ticks).ShouldBe(time); } // backcompat: remove (methods only added for TLruLongTicksPolicy) @@ -172,7 +172,7 @@ public void WhenTimeLessThanEqualZeroToTicksThrows() { Action toTicks = () => { TLruLongTicksPolicy.ToTicks(TimeSpan.Zero); }; - toTicks.Should().Throw(); + toTicks.ShouldThrow(); } // backcompat: remove (methods only added for TLruLongTicksPolicy) @@ -181,7 +181,7 @@ public void WhenTimeGreaterThanMaxToTicksThrows() { Action toTicks = () => { TLruLongTicksPolicy.ToTicks(TimeSpan.MaxValue); }; - toTicks.Should().Throw(); + toTicks.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs index f9e0d9f8..4ebcaa4e 100644 --- a/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TelemetryPolicyTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System.Collections.Generic; using Xunit; @@ -19,25 +19,25 @@ public TelemetryPolicyTests() [Fact] public void WhenHitTotalIs1() { - telemetryPolicy.Total.Should().Be(0); + telemetryPolicy.Total.ShouldBe(0); telemetryPolicy.IncrementHit(); - telemetryPolicy.Total.Should().Be(1); + telemetryPolicy.Total.ShouldBe(1); } [Fact] public void WhenHitHitsIs1() { - telemetryPolicy.Hits.Should().Be(0); + telemetryPolicy.Hits.ShouldBe(0); telemetryPolicy.IncrementHit(); - telemetryPolicy.Hits.Should().Be(1); + telemetryPolicy.Hits.ShouldBe(1); } [Fact] public void WhenMissMissesIs1() { - telemetryPolicy.Misses.Should().Be(0); + telemetryPolicy.Misses.ShouldBe(0); telemetryPolicy.IncrementMiss(); - telemetryPolicy.Misses.Should().Be(1); + telemetryPolicy.Misses.ShouldBe(1); } [Fact] @@ -45,7 +45,7 @@ public void WhenHitCountAndTotalCountAreEqualRatioIs1() { telemetryPolicy.IncrementHit(); - telemetryPolicy.HitRatio.Should().Be(1.0); + telemetryPolicy.HitRatio.ShouldBe(1.0); } [Fact] @@ -54,13 +54,13 @@ public void WhenHitCountIsEqualToMissCountRatioIsHalf() telemetryPolicy.IncrementMiss(); telemetryPolicy.IncrementHit(); - telemetryPolicy.HitRatio.Should().Be(0.5); + telemetryPolicy.HitRatio.ShouldBe(0.5); } [Fact] public void WhenTotalCountIsZeroRatioReturnsZero() { - telemetryPolicy.HitRatio.Should().Be(0.0); + telemetryPolicy.HitRatio.ShouldBe(0.0); } [Fact] @@ -68,7 +68,7 @@ public void WhenItemUpdatedIncrementUpdatedCount() { telemetryPolicy.OnItemUpdated(1, 2, 3); - telemetryPolicy.Updated.Should().Be(1); + telemetryPolicy.Updated.ShouldBe(1); } [Fact] @@ -76,7 +76,7 @@ public void WhenItemRemovedIncrementEvictedCount() { telemetryPolicy.OnItemRemoved(1, 2, ItemRemovedReason.Evicted); - telemetryPolicy.Evicted.Should().Be(1); + telemetryPolicy.Evicted.ShouldBe(1); } [Fact] @@ -84,7 +84,7 @@ public void WhenItemRemovedDontIncrementEvictedCount() { telemetryPolicy.OnItemRemoved(1, 2, ItemRemovedReason.Removed); - telemetryPolicy.Evicted.Should().Be(0); + telemetryPolicy.Evicted.ShouldBe(0); } [Fact] @@ -96,10 +96,10 @@ public void WhenOnItemRemovedInvokedEventIsFired() telemetryPolicy.OnItemRemoved(1, 2, ItemRemovedReason.Evicted); - eventList.Should().HaveCount(1); - eventList[0].Key.Should().Be(1); - eventList[0].Value.Should().Be(2); - eventList[0].Reason.Should().Be(ItemRemovedReason.Evicted); + eventList.Count.ShouldBe(1); + eventList[0].Key.ShouldBe(1); + eventList[0].Value.ShouldBe(2); + eventList[0].Reason.ShouldBe(ItemRemovedReason.Evicted); } [Fact] @@ -113,8 +113,8 @@ public void WhenEventSourceIsSetItemRemovedEventUsesSource() telemetryPolicy.OnItemRemoved(1, 2, ItemRemovedReason.Evicted); - eventSourceList.Should().HaveCount(1); - eventSourceList[0].Should().Be(this); + eventSourceList.Count.ShouldBe(1); + eventSourceList[0].ShouldBe(this); } [Fact] @@ -126,10 +126,10 @@ public void WhenOnItemUpdatedInvokedEventIsFired() telemetryPolicy.OnItemUpdated(1, 2, 3); - eventList.Should().HaveCount(1); - eventList[0].Key.Should().Be(1); - eventList[0].OldValue.Should().Be(2); - eventList[0].NewValue.Should().Be(3); + eventList.Count.ShouldBe(1); + eventList[0].Key.ShouldBe(1); + eventList[0].OldValue.ShouldBe(2); + eventList[0].NewValue.ShouldBe(3); } [Fact] @@ -143,8 +143,8 @@ public void WhenEventSourceIsSetItemUpdatedEventUsesSource() telemetryPolicy.OnItemUpdated(1, 2, 3); - eventSourceList.Should().HaveCount(1); - eventSourceList[0].Should().Be(this); + eventSourceList.Count.ShouldBe(1); + eventSourceList[0].ShouldBe(this); } // backcompat: remove @@ -157,7 +157,7 @@ public void WhenInterfaceDefaultItemUpdatedRegisteredNoOp() Action act = () => policy.Object.OnItemUpdated(1, 2, 3); - act.Should().NotThrow(); + act.ShouldNotThrow(); } #endif } diff --git a/BitFaster.Caching.UnitTests/Lru/TestCapacityPartition.cs b/BitFaster.Caching.UnitTests/Lru/TestCapacityPartition.cs index 1036eaea..cabe68ec 100644 --- a/BitFaster.Caching.UnitTests/Lru/TestCapacityPartition.cs +++ b/BitFaster.Caching.UnitTests/Lru/TestCapacityPartition.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using BitFaster.Caching.Lru; +using BitFaster.Caching.Lru; namespace BitFaster.Caching.UnitTests.Lru { diff --git a/BitFaster.Caching.UnitTests/Lru/TlruDateTimePolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TlruDateTimePolicyTests.cs index 4cdb7b78..ba249e2f 100644 --- a/BitFaster.Caching.UnitTests/Lru/TlruDateTimePolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TlruDateTimePolicyTests.cs @@ -1,10 +1,6 @@ -using FluentAssertions; -using FluentAssertions.Extensions; +using Shouldly; using BitFaster.Caching.Lru; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Xunit; @@ -17,7 +13,7 @@ public class TLruDateTimePolicyTests [Fact] public void TimeToLiveShouldBeTenSecs() { - this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10)); + this.policy.TimeToLive.ShouldBe(TimeSpan.FromSeconds(10)); } [Fact] @@ -25,8 +21,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -34,7 +30,7 @@ public void CreateItemInitializesTimestampToNow() { var item = this.policy.CreateItem(1, 2); - item.TimeStamp.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromMilliseconds(100)); + item.TimeStamp.ShouldBe(DateTime.UtcNow, TimeSpan.FromMilliseconds(100)); } [Fact] @@ -45,7 +41,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [Fact] @@ -58,7 +54,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TimeStamp.Should().BeAfter(ts); + item.TimeStamp.ShouldBeGreaterThan(ts); } [Fact] @@ -67,7 +63,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() var item = this.policy.CreateItem(1, 2); item.TimeStamp = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(11)); - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -76,13 +72,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() var item = this.policy.CreateItem(1, 2); item.TimeStamp = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(9)); - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } [Theory] @@ -94,7 +90,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -106,7 +102,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -118,7 +114,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private TimeStampedLruItem CreateItem(bool wasAccessed, bool isExpired) diff --git a/BitFaster.Caching.UnitTests/Lru/TlruStopwatchPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TlruStopwatchPolicyTests.cs index 1b228af4..94bcf8ce 100644 --- a/BitFaster.Caching.UnitTests/Lru/TlruStopwatchPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TlruStopwatchPolicyTests.cs @@ -1,10 +1,10 @@ -using FluentAssertions; -using BitFaster.Caching.Lru; -using System; +using System; using System.Threading.Tasks; -using Xunit; using System.Diagnostics; +using Xunit; +using BitFaster.Caching.Lru; using BitFaster.Caching.UnitTests.Retry; +using Shouldly; namespace BitFaster.Caching.UnitTests.Lru { @@ -19,7 +19,7 @@ public void WhenTtlIsZeroThrow() { Action constructor = () => { new TLruLongTicksPolicy(TimeSpan.Zero); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -27,7 +27,7 @@ public void WhenTtlIsTimeSpanMaxThrow() { Action constructor = () => { new TLruLongTicksPolicy(TimeSpan.MaxValue); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -35,13 +35,13 @@ public void WhenTtlIsCloseToMaxAllow() { var ttl = Duration.MaxRepresentable; - new TLruLongTicksPolicy(ttl).TimeToLive.Should().BeCloseTo(ttl, TimeSpan.FromSeconds(1)); + new TLruLongTicksPolicy(ttl).TimeToLive.ShouldBe(ttl, TimeSpan.FromSeconds(1)); } [Fact] public void TimeToLiveShouldBeTenSecs() { - this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10)); + this.policy.TimeToLive.ShouldBe(TimeSpan.FromSeconds(10)); } [Fact] @@ -49,8 +49,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -58,7 +58,7 @@ public void CreateItemInitializesTimestampToNow() { var item = this.policy.CreateItem(1, 2); - item.TickCount.Should().BeCloseTo(Duration.SinceEpoch().raw, DurationTests.epsilon); + item.TickCount.ShouldBe(Duration.SinceEpoch().raw); } [Fact] @@ -69,7 +69,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [RetryFact] @@ -82,7 +82,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -91,7 +91,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() var item = this.policy.CreateItem(1, 2); item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(11).raw; - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -100,13 +100,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() var item = this.policy.CreateItem(1, 2); item.TickCount = Duration.SinceEpoch().raw - Duration.FromSeconds(9).raw; - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } [Theory] @@ -118,7 +118,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -130,7 +130,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -142,7 +142,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private LongTickCountLruItem CreateItem(bool wasAccessed, bool isExpired) diff --git a/BitFaster.Caching.UnitTests/Lru/TlruTicksPolicyTests.cs b/BitFaster.Caching.UnitTests/Lru/TlruTicksPolicyTests.cs index db1e5fef..af200b39 100644 --- a/BitFaster.Caching.UnitTests/Lru/TlruTicksPolicyTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/TlruTicksPolicyTests.cs @@ -1,13 +1,9 @@ -using FluentAssertions; -using FluentAssertions.Extensions; -using BitFaster.Caching.Lru; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; using System.Threading.Tasks; using Xunit; +using BitFaster.Caching.Lru; using BitFaster.Caching.UnitTests.Retry; +using Shouldly; namespace BitFaster.Caching.UnitTests.Lru { @@ -18,7 +14,7 @@ public class TLruTicksPolicyTests [Fact] public void TimeToLiveShouldBeTenSecs() { - this.policy.TimeToLive.Should().Be(TimeSpan.FromSeconds(10)); + this.policy.TimeToLive.ShouldBe(TimeSpan.FromSeconds(10)); } [Fact] @@ -26,8 +22,8 @@ public void CreateItemInitializesKeyAndValue() { var item = this.policy.CreateItem(1, 2); - item.Key.Should().Be(1); - item.Value.Should().Be(2); + item.Key.ShouldBe(1); + item.Value.ShouldBe(2); } [Fact] @@ -35,7 +31,7 @@ public void CreateItemInitializesTimestampToNow() { var item = this.policy.CreateItem(1, 2); - item.TickCount.Should().BeCloseTo(Environment.TickCount, 20); + item.TickCount.ShouldBe(Environment.TickCount); } [Fact] @@ -46,7 +42,7 @@ public void TouchUpdatesItemWasAccessed() this.policy.Touch(item); - item.WasAccessed.Should().BeTrue(); + item.WasAccessed.ShouldBeTrue(); } [RetryFact] @@ -59,7 +55,7 @@ public async Task UpdateUpdatesTickCount() this.policy.Update(item); - item.TickCount.Should().BeGreaterThan(tc); + item.TickCount.ShouldBeGreaterThan(tc); } [Fact] @@ -68,7 +64,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue() var item = this.policy.CreateItem(1, 2); item.TickCount = Environment.TickCount - (int)TimeSpan.FromSeconds(11).ToEnvTicks(); - this.policy.ShouldDiscard(item).Should().BeTrue(); + this.policy.ShouldDiscard(item).ShouldBeTrue(); } [Fact] @@ -77,13 +73,13 @@ public void WhenItemIsNotExpiredShouldDiscardIsFalse() var item = this.policy.CreateItem(1, 2); item.TickCount = Environment.TickCount - (int)TimeSpan.FromSeconds(9).ToEnvTicks(); - this.policy.ShouldDiscard(item).Should().BeFalse(); + this.policy.ShouldDiscard(item).ShouldBeFalse(); } [Fact] public void CanDiscardIsTrue() { - this.policy.CanDiscard().Should().BeTrue(); + this.policy.CanDiscard().ShouldBeTrue(); } [Theory] @@ -95,7 +91,7 @@ public void RouteHot(bool wasAccessed, bool isExpired, ItemDestination expectedD { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteHot(item).Should().Be(expectedDestination); + this.policy.RouteHot(item).ShouldBe(expectedDestination); } [Theory] @@ -107,7 +103,7 @@ public void RouteWarm(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteWarm(item).Should().Be(expectedDestination); + this.policy.RouteWarm(item).ShouldBe(expectedDestination); } [Theory] @@ -119,7 +115,7 @@ public void RouteCold(bool wasAccessed, bool isExpired, ItemDestination expected { var item = CreateItem(wasAccessed, isExpired); - this.policy.RouteCold(item).Should().Be(expectedDestination); + this.policy.RouteCold(item).ShouldBe(expectedDestination); } private TickCountLruItem CreateItem(bool wasAccessed, bool isExpired) diff --git a/BitFaster.Caching.UnitTests/Lru/ValueFactory.cs b/BitFaster.Caching.UnitTests/Lru/ValueFactory.cs index 18156cfd..875dbb3d 100644 --- a/BitFaster.Caching.UnitTests/Lru/ValueFactory.cs +++ b/BitFaster.Caching.UnitTests/Lru/ValueFactory.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace BitFaster.Caching.UnitTests.Lru { diff --git a/BitFaster.Caching.UnitTests/ReferenceCountTests.cs b/BitFaster.Caching.UnitTests/ReferenceCountTests.cs index 429d2001..85b0faee 100644 --- a/BitFaster.Caching.UnitTests/ReferenceCountTests.cs +++ b/BitFaster.Caching.UnitTests/ReferenceCountTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -11,7 +11,7 @@ public void WhenOtherIsEqualEqualsReturnsTrue() var a = new ReferenceCount(new object()); var b = a.IncrementCopy().DecrementCopy(); - a.Should().Be(b); + a.ShouldBe(b); } [Fact] @@ -20,7 +20,7 @@ public void WhenOtherIsEqualReferenceEqualsReturnsFalse() var a = new ReferenceCount(new object()); var b = a.IncrementCopy().DecrementCopy(); - a.Should().NotBeSameAs(b); + a.ShouldNotBeSameAs(b); } [Fact] @@ -29,7 +29,7 @@ public void WhenOtherIsNotEqualEqualsReturnsFalse() var a = new ReferenceCount(new object()); var b = new ReferenceCount(new object()); - a.Should().NotBe(b); + a.ShouldNotBe(b); } [Fact] @@ -38,7 +38,7 @@ public void WhenOtherRefCountIsNotEqualEqualsReturnsFalse() var a = new ReferenceCount(0); var b = a.IncrementCopy(); - a.Should().NotBe(b); + a.ShouldNotBe(b); } [Fact] @@ -47,7 +47,7 @@ public void WhenObjectsAreEqualHashcodesAreEqual() var a = new ReferenceCount(new object()); var b = a.IncrementCopy().DecrementCopy(); - a.GetHashCode().Should().Be(b.GetHashCode()); + a.GetHashCode().ShouldBe(b.GetHashCode()); } [Fact] @@ -56,7 +56,7 @@ public void WhenObjectsAreDifferentHashcodesAreDifferent() var a = new ReferenceCount(new object()); var b = new ReferenceCount(new object()); - a.GetHashCode().Should().NotBe(b.GetHashCode()); + a.GetHashCode().ShouldNotBe(b.GetHashCode()); } [Fact] diff --git a/BitFaster.Caching.UnitTests/Retry/DelayedMessageBus.cs b/BitFaster.Caching.UnitTests/Retry/DelayedMessageBus.cs index 02081d87..9b9a7aed 100644 --- a/BitFaster.Caching.UnitTests/Retry/DelayedMessageBus.cs +++ b/BitFaster.Caching.UnitTests/Retry/DelayedMessageBus.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using Xunit.Abstractions; using Xunit.Sdk; diff --git a/BitFaster.Caching.UnitTests/Retry/RetryFact.cs b/BitFaster.Caching.UnitTests/Retry/RetryFact.cs index efd42e94..2a796193 100644 --- a/BitFaster.Caching.UnitTests/Retry/RetryFact.cs +++ b/BitFaster.Caching.UnitTests/Retry/RetryFact.cs @@ -1,5 +1,5 @@ -using Xunit.Sdk; -using Xunit; +using Xunit; +using Xunit.Sdk; namespace BitFaster.Caching.UnitTests.Retry { diff --git a/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs index 75fc9d66..b3d07f02 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/BackgroundSchedulerTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Scheduler; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Scheduler @@ -14,18 +14,18 @@ public class BackgroundSchedulerTests : IDisposable [Fact] public void IsBackground() { - scheduler.IsBackground.Should().BeTrue(); + scheduler.IsBackground.ShouldBeTrue(); } [Fact] public async Task WhenWorkIsScheduledCountIsIncremented() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); await Task.Yield(); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); } [Fact] @@ -37,19 +37,19 @@ public async Task WhenWorkIsScheduledItIsRun() scheduler.Run(() => { Volatile.Write(ref run, true); tcs.SetResult(true); }); await tcs.Task; - Volatile.Read(ref run).Should().BeTrue(); + Volatile.Read(ref run).ShouldBeTrue(); } [Fact] public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); await Task.Yield(); - scheduler.LastException.HasValue.Should().BeFalse(); + scheduler.LastException.HasValue.ShouldBeFalse(); } [Fact] @@ -61,8 +61,8 @@ public async Task WhenWorkThrowsLastExceptionIsPopulated() await tcs.Task; await scheduler.WaitForExceptionAsync(); - scheduler.LastException.HasValue.Should().BeTrue(); - scheduler.LastException.Value.Should().BeOfType(); + scheduler.LastException.HasValue.ShouldBeTrue(); + scheduler.LastException.Value.ShouldBeOfType(); } [Fact] @@ -77,7 +77,7 @@ public void WhenBacklogExceededTasksAreDropped() mre.Set(); - scheduler.RunCount.Should().BeCloseTo(BackgroundThreadScheduler.MaxBacklog, 1); + scheduler.RunCount.ShouldBe(BackgroundThreadScheduler.MaxBacklog); } [Fact] diff --git a/BitFaster.Caching.UnitTests/Scheduler/ForegroundSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/ForegroundSchedulerTests.cs index 60260b37..c7f67ce8 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/ForegroundSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/ForegroundSchedulerTests.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Scheduler; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Scheduler @@ -16,17 +12,17 @@ public class ForegroundSchedulerTests [Fact] public void IsNotBackground() { - scheduler.IsBackground.Should().BeFalse(); + scheduler.IsBackground.ShouldBeFalse(); } [Fact] public void WhenWorkIsScheduledCountIsIncremented() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); } [Fact] @@ -36,17 +32,17 @@ public void WhenWorkIsScheduledItIsRun() scheduler.Run(() => { run = true; }); - run.Should().BeTrue(); + run.ShouldBeTrue(); } [Fact] public void WhenWorkDoesNotThrowLastExceptionIsEmpty() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); - scheduler.LastException.HasValue.Should().BeFalse(); + scheduler.LastException.HasValue.ShouldBeFalse(); } [Fact] @@ -54,7 +50,7 @@ public void WhenWorkThrowsExceptionIsSynchronous() { Action work = () => { scheduler.Run(() => { throw new InvalidCastException(); }); }; - work.Should().Throw(); + work.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/Scheduler/NullSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/NullSchedulerTests.cs index a744c17d..5a880c3b 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/NullSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/NullSchedulerTests.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using BitFaster.Caching.Scheduler; -using FluentAssertions; +using BitFaster.Caching.Scheduler; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Scheduler @@ -16,17 +11,17 @@ public class NullSchedulerTests [Fact] public void IsNotBackground() { - scheduler.IsBackground.Should().BeFalse(); + scheduler.IsBackground.ShouldBeFalse(); } [Fact] public void WhenWorkIsScheduledCountIsIncremented() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); } [Fact] @@ -36,13 +31,13 @@ public void WhenWorkIsScheduledItIsNotRun() scheduler.Run(() => { run = true; }); - run.Should().BeFalse(); + run.ShouldBeFalse(); } [Fact] public void LastExceptionIsEmpty() { - scheduler.LastException.HasValue.Should().BeFalse(); + scheduler.LastException.HasValue.ShouldBeFalse(); } } } diff --git a/BitFaster.Caching.UnitTests/Scheduler/SchedulerTestExtensions.cs b/BitFaster.Caching.UnitTests/Scheduler/SchedulerTestExtensions.cs index ddebff2e..8f1237c8 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/SchedulerTestExtensions.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/SchedulerTestExtensions.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using BitFaster.Caching.Scheduler; diff --git a/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs b/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs index 8dfa8cce..72ace83b 100644 --- a/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs +++ b/BitFaster.Caching.UnitTests/Scheduler/ThreadPoolSchedulerTests.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; using BitFaster.Caching.Scheduler; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests.Scheduler @@ -14,12 +14,12 @@ public class ThreadPoolSchedulerTests [Fact] public async Task WhenWorkIsScheduledCountIsIncremented() { - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { }); await Task.Yield(); - scheduler.RunCount.Should().Be(1); + scheduler.RunCount.ShouldBe(1); } [Fact] @@ -32,20 +32,20 @@ public async Task WhenWorkIsScheduledItIsRun() await tcs.Task; - Volatile.Read(ref run).Should().BeTrue(); + Volatile.Read(ref run).ShouldBeTrue(); } [Fact] public async Task WhenWorkDoesNotThrowLastExceptionIsEmpty() { var tcs = new TaskCompletionSource(); - scheduler.RunCount.Should().Be(0); + scheduler.RunCount.ShouldBe(0); scheduler.Run(() => { tcs.SetResult(true); }); await tcs.Task; - scheduler.LastException.HasValue.Should().BeFalse(); + scheduler.LastException.HasValue.ShouldBeFalse(); } [Fact] @@ -58,8 +58,8 @@ public async Task WhenWorkThrowsLastExceptionIsPopulated() await tcs.Task; await scheduler.WaitForExceptionAsync(); - scheduler.LastException.HasValue.Should().BeTrue(); - scheduler.LastException.Value.Should().BeOfType(); + scheduler.LastException.HasValue.ShouldBeTrue(); + scheduler.LastException.Value.ShouldBeOfType(); } } } diff --git a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs index 6bd50a76..953230a6 100644 --- a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs +++ b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTestBase.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -24,17 +24,17 @@ protected ScopedAsyncCacheTestBase(IScopedAsyncCache cache) [Fact] public void WhenCreatedCapacityPropertyWrapsInnerCache() { - this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity); + this.cache.Policy.Eviction.Value.Capacity.ShouldBe(capacity); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); this.cache.AddOrUpdate(1, new Disposable()); - this.cache.Count.Should().Be(1); + this.cache.Count.ShouldBe(1); } [Fact] @@ -43,8 +43,8 @@ public void WhenItemIsAddedThenLookedUpMetricsAreCorrect() this.cache.AddOrUpdate(1, new Disposable()); this.cache.ScopedTryGet(1, out var lifetime); - this.cache.Metrics.Value.Misses.Should().Be(0); - this.cache.Metrics.Value.Hits.Should().Be(1); + this.cache.Metrics.Value.Misses.ShouldBe(0); + this.cache.Metrics.Value.Hits.ShouldBe(1); } [Fact] @@ -55,7 +55,7 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, new Disposable()); this.cache.TryRemove(1); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } // backcompat: remove conditional compile @@ -68,7 +68,7 @@ public void WhenUpdatedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, new Disposable()); this.cache.AddOrUpdate(1, new Disposable()); - this.updatedItems.First().Key.Should().Be(1); + this.updatedItems.First().Key.ShouldBe(1); } #endif @@ -78,8 +78,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() var d = new Disposable(); this.cache.AddOrUpdate(1, d); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(d); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(d); } // backcompat: remove conditional compile @@ -92,8 +92,8 @@ await this.cache.ScopedGetOrAddAsync( (k, a) => Task.FromResult(new Scoped(new Disposable(a))), 2); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.State.Should().Be(2); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.State.ShouldBe(2); } #endif [Fact] @@ -104,8 +104,8 @@ public void WhenKeyExistsAddOrUpdateUpdatesExistingItem() this.cache.AddOrUpdate(1, d1); this.cache.AddOrUpdate(1, d2); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(d2); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(d2); } [Fact] @@ -116,7 +116,7 @@ public void WhenItemUpdatedOldValueIsAliveUntilLifetimeCompletes() // start a lifetime on 1 this.cache.AddOrUpdate(1, d1); - this.cache.ScopedTryGet(1, out var lifetime1).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime1).ShouldBeTrue(); using (lifetime1) { @@ -124,13 +124,13 @@ public void WhenItemUpdatedOldValueIsAliveUntilLifetimeCompletes() this.cache.AddOrUpdate(1, d2); // cache reflects replacement - this.cache.ScopedTryGet(1, out var lifetime2).Should().BeTrue(); - lifetime2.Value.Should().Be(d2); + this.cache.ScopedTryGet(1, out var lifetime2).ShouldBeTrue(); + lifetime2.Value.ShouldBe(d2); - d1.IsDisposed.Should().BeFalse(); + d1.IsDisposed.ShouldBeFalse(); } - d1.IsDisposed.Should().BeTrue(); + d1.IsDisposed.ShouldBeTrue(); } [Fact] @@ -141,22 +141,22 @@ public void WhenClearedItemsAreDisposed() this.cache.Clear(); - d.IsDisposed.Should().BeTrue(); + d.IsDisposed.ShouldBeTrue(); } [Fact] public void WhenItemExistsTryGetReturnsLifetime() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); - lifetime.Should().NotBeNull(); + lifetime.ShouldNotBeNull(); } [Fact] public void WhenItemDoesNotExistTryGetReturnsFalse() { - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -168,26 +168,26 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue() this.cache.Policy.Eviction.Value.Trim(1); - this.cache.ScopedTryGet(0, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(0, out var lifetime).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { - this.cache.TryRemove(1).Should().BeFalse(); + this.cache.TryRemove(1).ShouldBeFalse(); } [Fact] public void WhenKeyExistsTryRemoveReturnsTrue() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.TryRemove(1).Should().BeTrue(); + this.cache.TryRemove(1).ShouldBeTrue(); } [Fact] public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { - this.cache.TryUpdate(1, new Disposable()).Should().BeFalse(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeFalse(); } [Fact] @@ -195,16 +195,16 @@ public void WhenKeyExistsTryUpdateReturnsTrue() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeTrue(); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, new Disposable()); cache.AddOrUpdate(2, new Disposable()); - cache.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + cache.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] @@ -213,12 +213,12 @@ public void WhenItemsAddedGenericEnumerateContainsKvps() var d1 = new Disposable(); var d2 = new Disposable(); - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, d1); cache.AddOrUpdate(2, d2); cache .Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.CreateLifetime().Value)) - .Should().BeEquivalentTo(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); + .ShouldBe(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); } [Fact] @@ -227,7 +227,7 @@ public void WhenItemsAddedEnumerateContainsKvps() var d1 = new Disposable(); var d2 = new Disposable(); - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, d1); cache.AddOrUpdate(2, d2); @@ -241,7 +241,7 @@ public void WhenItemsAddedEnumerateContainsKvps() list.Add(new KeyValuePair(kvp.Key, kvp.Value.CreateLifetime().Value)); } - list.Should().BeEquivalentTo(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); + list.ShouldBe(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); } [Fact] @@ -253,7 +253,7 @@ public async Task WhenFactoryThrowsEmptyValueIsNotCounted() } catch { } - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); } [Fact] @@ -266,7 +266,7 @@ public async Task WhenFactoryThrowsEmptyValueIsNotEnumerable() catch { } // IEnumerable.Count() instead of Count property - cache.Count().Should().Be(0); + cache.Count().ShouldBe(0); } [Fact] @@ -278,7 +278,7 @@ public async Task WhenFactoryThrowsEmptyKeyIsNotEnumerable() } catch { } - cache.Keys.Count().Should().Be(0); + cache.Keys.Count().ShouldBe(0); } protected void OnItemRemoved(object sender, ItemRemovedEventArgs> e) diff --git a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTests.cs b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTests.cs index 864287c6..603c179b 100644 --- a/BitFaster.Caching.UnitTests/ScopedAsyncCacheTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedAsyncCacheTests.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -18,7 +18,7 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new ScopedAsyncCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -30,7 +30,7 @@ public async Task WhenScopeIsDisposedTryGetReturnsFalse() scope.Dispose(); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -38,7 +38,7 @@ public async Task WhenKeyDoesNotExistGetOrAddAsyncAddsValue() { await this.cache.ScopedGetOrAddAsync(1, k => Task.FromResult(new Scoped(new Disposable()))); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); } [Fact] @@ -49,7 +49,7 @@ public async Task GetOrAddAsyncDisposedScopeThrows() Func getOrAdd = async () => { await this.cache.ScopedGetOrAddAsync(1, k => Task.FromResult(scope)); }; - await getOrAdd.Should().ThrowAsync(); + var ex = await getOrAdd.ShouldThrowAsync();; } // backcompat: remove conditional compile @@ -62,7 +62,7 @@ public async Task GetOrAddAsyncArgDisposedScopeThrows() Func getOrAdd = async () => { await this.cache.ScopedGetOrAddAsync(1, (k, a) => Task.FromResult(scope), 2); }; - await getOrAdd.Should().ThrowAsync(); + var ex = await getOrAdd.ShouldThrowAsync();; } #endif } diff --git a/BitFaster.Caching.UnitTests/ScopedCacheSoakTests.cs b/BitFaster.Caching.UnitTests/ScopedCacheSoakTests.cs index 49b15190..41a590fd 100644 --- a/BitFaster.Caching.UnitTests/ScopedCacheSoakTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedCacheSoakTests.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -21,7 +21,7 @@ await Threaded.Run(4, () => { { using (var l = this.cache.ScopedGetOrAdd(j, k => new Scoped(new Disposable(k)))) { - l.Value.IsDisposed.Should().BeFalse($"ref count {l.ReferenceCount}"); + l.Value.IsDisposed.ShouldBeFalse($"ref count {l.ReferenceCount}"); } } }); diff --git a/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs b/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs index dd41bda2..77bbc462 100644 --- a/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs +++ b/BitFaster.Caching.UnitTests/ScopedCacheTestBase.cs @@ -1,8 +1,7 @@ - -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.Linq; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -23,17 +22,17 @@ protected ScopedCacheTestBase(IScopedCache cache) [Fact] public void WhenCreatedCapacityPropertyWrapsInnerCache() { - this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity); + this.cache.Policy.Eviction.Value.Capacity.ShouldBe(capacity); } [Fact] public void WhenItemIsAddedCountIsCorrect() { - this.cache.Count.Should().Be(0); + this.cache.Count.ShouldBe(0); this.cache.AddOrUpdate(1, new Disposable()); - this.cache.Count.Should().Be(1); + this.cache.Count.ShouldBe(1); } [Fact] @@ -42,8 +41,8 @@ public void WhenItemIsAddedThenLookedUpMetricsAreCorrect() this.cache.AddOrUpdate(1, new Disposable()); this.cache.ScopedTryGet(1, out var lifetime); - this.cache.Metrics.Value.Misses.Should().Be(0); - this.cache.Metrics.Value.Hits.Should().Be(1); + this.cache.Metrics.Value.Misses.ShouldBe(0); + this.cache.Metrics.Value.Hits.ShouldBe(1); } [Fact] @@ -54,7 +53,7 @@ public void WhenRemovedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, new Disposable()); this.cache.TryRemove(1); - this.removedItems.First().Key.Should().Be(1); + this.removedItems.First().Key.ShouldBe(1); } // backcompat: remove conditional compile @@ -67,7 +66,7 @@ public void WhenUpdatedEventHandlerIsRegisteredItIsFired() this.cache.AddOrUpdate(1, new Disposable()); this.cache.AddOrUpdate(1, new Disposable()); - this.updatedItems.First().Key.Should().Be(1); + this.updatedItems.First().Key.ShouldBe(1); } #endif @@ -77,8 +76,8 @@ public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem() var d = new Disposable(); this.cache.AddOrUpdate(1, d); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(d); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(d); } // backcompat: remove conditional compile @@ -91,8 +90,8 @@ public void WhenKeyDoesNotExistGetOrAddArgAddsValueWithArg() (k, a) => new Scoped(new Disposable(a)), 2); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.State.Should().Be(2); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.State.ShouldBe(2); } #endif @@ -104,8 +103,8 @@ public void WhenKeyExistsAddOrUpdateUpdatesExistingItem() this.cache.AddOrUpdate(1, d1); this.cache.AddOrUpdate(1, d2); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); - lifetime.Value.Should().Be(d2); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); + lifetime.Value.ShouldBe(d2); } [Fact] @@ -116,7 +115,7 @@ public void WhenItemUpdatedOldValueIsAliveUntilLifetimeCompletes() // start a lifetime on 1 this.cache.AddOrUpdate(1, d1); - this.cache.ScopedTryGet(1, out var lifetime1).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime1).ShouldBeTrue(); using (lifetime1) { @@ -124,13 +123,13 @@ public void WhenItemUpdatedOldValueIsAliveUntilLifetimeCompletes() this.cache.AddOrUpdate(1, d2); // cache reflects replacement - this.cache.ScopedTryGet(1, out var lifetime2).Should().BeTrue(); - lifetime2.Value.Should().Be(d2); + this.cache.ScopedTryGet(1, out var lifetime2).ShouldBeTrue(); + lifetime2.Value.ShouldBe(d2); - d1.IsDisposed.Should().BeFalse(); + d1.IsDisposed.ShouldBeFalse(); } - d1.IsDisposed.Should().BeTrue(); + d1.IsDisposed.ShouldBeTrue(); } [Fact] @@ -141,22 +140,22 @@ public void WhenClearedItemsAreDisposed() this.cache.Clear(); - d.IsDisposed.Should().BeTrue(); + d.IsDisposed.ShouldBeTrue(); } [Fact] public void WhenItemExistsTryGetReturnsLifetime() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); - lifetime.Should().NotBeNull(); + lifetime.ShouldNotBeNull(); } [Fact] public void WhenItemDoesNotExistTryGetReturnsFalse() { - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -168,26 +167,26 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue() this.cache.Policy.Eviction.Value.Trim(1); - this.cache.ScopedTryGet(0, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(0, out var lifetime).ShouldBeFalse(); } [Fact] public void WhenKeyDoesNotExistTryRemoveReturnsFalse() { - this.cache.TryRemove(1).Should().BeFalse(); + this.cache.TryRemove(1).ShouldBeFalse(); } [Fact] public void WhenKeyExistsTryRemoveReturnsTrue() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.TryRemove(1).Should().BeTrue(); + this.cache.TryRemove(1).ShouldBeTrue(); } [Fact] public void WhenKeyDoesNotExistTryUpdateReturnsFalse() { - this.cache.TryUpdate(1, new Disposable()).Should().BeFalse(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeFalse(); } [Fact] @@ -195,16 +194,16 @@ public void WhenKeyExistsTryUpdateReturnsTrue() { this.cache.AddOrUpdate(1, new Disposable()); - this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); + this.cache.TryUpdate(1, new Disposable()).ShouldBeTrue(); } [Fact] public void WhenItemsAddedKeysContainsTheKeys() { - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, new Disposable()); cache.AddOrUpdate(2, new Disposable()); - cache.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + cache.Keys.ShouldBe(new[] { 1, 2 }); } [Fact] @@ -213,12 +212,12 @@ public void WhenItemsAddedGenericEnumerateContainsKvps() var d1 = new Disposable(); var d2 = new Disposable(); - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, d1); cache.AddOrUpdate(2, d2); cache .Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.CreateLifetime().Value)) - .Should().BeEquivalentTo(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); + .ShouldBe(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); } [Fact] @@ -227,7 +226,7 @@ public void WhenItemsAddedEnumerateContainsKvps() var d1 = new Disposable(); var d2 = new Disposable(); - cache.Count.Should().Be(0); + cache.Count.ShouldBe(0); cache.AddOrUpdate(1, d1); cache.AddOrUpdate(2, d2); @@ -241,7 +240,7 @@ public void WhenItemsAddedEnumerateContainsKvps() list.Add(new KeyValuePair(kvp.Key, kvp.Value.CreateLifetime().Value)); } - list.Should().BeEquivalentTo(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); + list.ShouldBe(new[] { new KeyValuePair(1, d1), new KeyValuePair(2, d2) }); } protected void OnItemRemoved(object sender, ItemRemovedEventArgs> e) diff --git a/BitFaster.Caching.UnitTests/ScopedCacheTests.cs b/BitFaster.Caching.UnitTests/ScopedCacheTests.cs index 2345b48f..7312f0ab 100644 --- a/BitFaster.Caching.UnitTests/ScopedCacheTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedCacheTests.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using BitFaster.Caching.Lru; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -21,7 +17,7 @@ public void WhenInnerCacheIsNullCtorThrows() { Action constructor = () => { var x = new ScopedCache(null); }; - constructor.Should().Throw(); + constructor.ShouldThrow(); } [Fact] @@ -33,7 +29,7 @@ public void WhenScopeIsDisposedTryGetReturnsFalse() scope.Dispose(); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeFalse(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeFalse(); } [Fact] @@ -41,7 +37,7 @@ public void WhenKeyDoesNotExistGetOrAddAddsValue() { this.cache.ScopedGetOrAdd(1, k => new Scoped(new Disposable())); - this.cache.ScopedTryGet(1, out var lifetime).Should().BeTrue(); + this.cache.ScopedTryGet(1, out var lifetime).ShouldBeTrue(); } [Fact] @@ -52,7 +48,7 @@ public void GetOrAddDisposedScopeThrows() Action getOrAdd = () => { this.cache.ScopedGetOrAdd(1, k => scope); }; - getOrAdd.Should().Throw(); + getOrAdd.ShouldThrow(); } } } diff --git a/BitFaster.Caching.UnitTests/ScopedSoakTests.cs b/BitFaster.Caching.UnitTests/ScopedSoakTests.cs index 976f4b50..0818d6a8 100644 --- a/BitFaster.Caching.UnitTests/ScopedSoakTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedSoakTests.cs @@ -1,5 +1,5 @@ using System.Threading.Tasks; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -19,15 +19,15 @@ await Threaded.Run(4, () => { { using (var l = scope.CreateLifetime()) { - l.Value.IsDisposed.Should().BeFalse(); + l.Value.IsDisposed.ShouldBeFalse(); } } }); - scope.IsDisposed.Should().BeFalse(); + scope.IsDisposed.ShouldBeFalse(); scope.Dispose(); - scope.TryCreateLifetime(out _).Should().BeFalse(); - scope.IsDisposed.Should().BeTrue(); + scope.TryCreateLifetime(out _).ShouldBeFalse(); + scope.IsDisposed.ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/ScopedTests.cs b/BitFaster.Caching.UnitTests/ScopedTests.cs index 53fc4130..5a409df9 100644 --- a/BitFaster.Caching.UnitTests/ScopedTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using BitFaster.Caching.Lru; using System; using System.Collections.Generic; @@ -15,7 +15,7 @@ public void WhenScopeIsCreatedThenScopeDisposedValueIsDisposed() var scope = new Scoped(disposable); scope.Dispose(); - disposable.IsDisposed.Should().BeTrue(); + disposable.IsDisposed.ShouldBeTrue(); } [Fact] @@ -27,10 +27,10 @@ public void WhenScopeIsCreatedThenScopeDisposedLifetimeDisposesValue() scope.Dispose(); scope.Dispose(); // validate double dispose is still single ref count - disposable.IsDisposed.Should().BeFalse(); + disposable.IsDisposed.ShouldBeFalse(); lifetime.Dispose(); - disposable.IsDisposed.Should().BeTrue(); + disposable.IsDisposed.ShouldBeTrue(); } [Fact] @@ -43,10 +43,10 @@ public void WhenScopeIsCreatedThenLifetimeDisposedScopeDisposesValue() lifetime.Dispose(); lifetime.Dispose(); // validate double dispose is still single ref count - disposable.IsDisposed.Should().BeFalse(); + disposable.IsDisposed.ShouldBeFalse(); scope.Dispose(); - disposable.IsDisposed.Should().BeTrue(); + disposable.IsDisposed.ShouldBeTrue(); } [Fact] @@ -56,7 +56,7 @@ public void WhenScopeIsDisposedCreateScopeThrows() var scope = new Scoped(disposable); scope.Dispose(); - scope.Invoking(s => s.CreateLifetime()).Should().Throw(); + Should.Throw(() => scope.CreateLifetime()); } [Fact] @@ -66,7 +66,7 @@ public void WhenScopeIsDisposedTryCreateScopeReturnsFalse() var scope = new Scoped(disposable); scope.Dispose(); - scope.TryCreateLifetime(out var l).Should().BeFalse(); + scope.TryCreateLifetime(out var l).ShouldBeFalse(); } [Fact] @@ -77,14 +77,14 @@ public void WhenScopedIsCreatedFromCacheItemHasExpectedLifetime() using (var lifetime = lru.GetOrAdd(1, valueFactory.Create).CreateLifetime()) { - lifetime.Value.IsDisposed.Should().BeFalse(); + lifetime.Value.IsDisposed.ShouldBeFalse(); } - valueFactory.Disposable.IsDisposed.Should().BeFalse(); + valueFactory.Disposable.IsDisposed.ShouldBeFalse(); lru.TryRemove(1); - valueFactory.Disposable.IsDisposed.Should().BeTrue(); + valueFactory.Disposable.IsDisposed.ShouldBeTrue(); } } } diff --git a/BitFaster.Caching.UnitTests/SingletonCacheTests.cs b/BitFaster.Caching.UnitTests/SingletonCacheTests.cs index d2871f17..4c0c3eb7 100644 --- a/BitFaster.Caching.UnitTests/SingletonCacheTests.cs +++ b/BitFaster.Caching.UnitTests/SingletonCacheTests.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -16,7 +16,7 @@ public void AcquireWithSameKeyUsingCustomComparerReturnsSameLifetime() var lifetime1 = cache.Acquire("foo"); var lifetime2 = cache.Acquire("FOO"); - lifetime1.Value.Should().BeSameAs(lifetime2.Value); + lifetime1.Value.ShouldBeSameAs(lifetime2.Value); lifetime1.Dispose(); lifetime2.Dispose(); } @@ -28,7 +28,7 @@ public void AcquireWithSameKeyReturnsSameLifetime() var lifetime1 = cache.Acquire("Foo"); var lifetime2 = cache.Acquire("Foo"); - lifetime1.Value.Should().BeSameAs(lifetime2.Value); + lifetime1.Value.ShouldBeSameAs(lifetime2.Value); lifetime1.Dispose(); lifetime2.Dispose(); } @@ -42,19 +42,19 @@ public void AcquireWithSameKeyMaintainsReferenceCount() { using (var lifetime2 = cache.Acquire("Foo")) { - lifetime1.ReferenceCount.Should().Be(1); - lifetime2.ReferenceCount.Should().Be(2); + lifetime1.ReferenceCount.ShouldBe(1); + lifetime2.ReferenceCount.ShouldBe(2); } using (var lifetime3 = cache.Acquire("Foo")) { - lifetime3.ReferenceCount.Should().Be(2); + lifetime3.ReferenceCount.ShouldBe(2); } } using (var lifetime4 = cache.Acquire("Foo")) { - lifetime4.ReferenceCount.Should().Be(1); + lifetime4.ReferenceCount.ShouldBe(1); } } @@ -69,7 +69,7 @@ public void AcquireReleaseAcquireReturnsDifferentValue() var lifetime2 = cache.Acquire("Foo"); lifetime2.Dispose(); - lifetime1.Value.Should().NotBeSameAs(lifetime2.Value); + lifetime1.Value.ShouldNotBeSameAs(lifetime2.Value); } [Fact] @@ -107,7 +107,7 @@ public async Task AcquireWithSameKeyOnTwoDifferentThreadsReturnsSameValue() await Task.WhenAll(task1, task2); - lifetime1.Value.Should().BeSameAs(lifetime2.Value); + lifetime1.Value.ShouldBeSameAs(lifetime2.Value); } [Fact] @@ -130,7 +130,7 @@ public async Task AcquireWithSameKeyOnManyDifferentThreadsReturnsSameValue() lock (lifetime.Value) { int result = Interlocked.Increment(ref count); - result.Should().Be(1); + result.ShouldBe(1); Interlocked.Decrement(ref count); } } @@ -151,10 +151,10 @@ public void WhenValueIsDisposableItIsDisposedWhenReleased() using (var lifetime = cache.Acquire("Foo")) { value = lifetime.Value; - value.IsDisposed.Should().BeFalse(); + value.IsDisposed.ShouldBeFalse(); } - value.IsDisposed.Should().BeTrue(); + value.IsDisposed.ShouldBeTrue(); } [Fact] diff --git a/BitFaster.Caching.UnitTests/Threaded.cs b/BitFaster.Caching.UnitTests/Threaded.cs index a3f575f5..9166e250 100644 --- a/BitFaster.Caching.UnitTests/Threaded.cs +++ b/BitFaster.Caching.UnitTests/Threaded.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/BitFaster.Caching.UnitTests/Timed.cs b/BitFaster.Caching.UnitTests/Timed.cs index 9f04da9e..c9137ca2 100644 --- a/BitFaster.Caching.UnitTests/Timed.cs +++ b/BitFaster.Caching.UnitTests/Timed.cs @@ -1,7 +1,7 @@ using System; using System.Diagnostics; using System.Threading; -using FluentAssertions; +using Shouldly; namespace BitFaster.Caching.UnitTests { @@ -70,7 +70,7 @@ public static void Execute(TArg arg, Func first, Tim } Thread.Sleep(200); - attempts++.Should().BeLessThan(128, "Unable to run test within verification margin"); + attempts++.ShouldBeLessThan(128, "Unable to run test within verification margin"); } } } diff --git a/BitFaster.Caching.UnitTests/TypePropsTests.cs b/BitFaster.Caching.UnitTests/TypePropsTests.cs index a02d9c80..c7d4affc 100644 --- a/BitFaster.Caching.UnitTests/TypePropsTests.cs +++ b/BitFaster.Caching.UnitTests/TypePropsTests.cs @@ -1,6 +1,6 @@ using System; using System.Reflection; -using FluentAssertions; +using Shouldly; using Xunit; namespace BitFaster.Caching.UnitTests @@ -20,7 +20,7 @@ public void Test(Type argType, bool expected) { var isWriteAtomic = method.MakeGenericMethod(argType); - isWriteAtomic.Invoke(null, null).Should().BeOfType().Which.Should().Be(expected); + isWriteAtomic.Invoke(null, null).ShouldBeOfType().ShouldBe(expected); } private static bool IsWriteAtomic() From bbda7256b045eab4b0a17a61e5b8ed4671f85e4d Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:08:19 +0000 Subject: [PATCH 2/3] Fix unordered enumerable tests --- BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs index ba0b912b..9842d51b 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs @@ -143,7 +143,7 @@ public void WhenItemsAddedKeysContainsTheKeys() lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.Keys.ShouldBe(new[] { 1, 2 }); + lru.Keys.ShouldBe(new[] { 1, 2 }, ignoreOrder: true); } [Fact] @@ -152,7 +152,7 @@ public void WhenItemsAddedGenericEnumerateContainsKvps() lru.Count.ShouldBe(0); lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - lru.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + lru.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }, ignoreOrder: true); } [Fact] @@ -162,8 +162,9 @@ public void WhenItemsAddedEnumerateContainsKvps() lru.GetOrAdd(1, valueFactory.Create); lru.GetOrAdd(2, valueFactory.Create); - var enumerable = (IEnumerable)lru; - enumerable.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }); + var untypedEnumerable = (IEnumerable)lru; + var enumerable = untypedEnumerable.Cast>(); + enumerable.ShouldBe(new[] { new KeyValuePair(1, "1"), new KeyValuePair(2, "2") }, ignoreOrder: true); } [Fact] From 81071e4363e641917b73a1156bf988c5ccaa57d5 Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:28:37 +0000 Subject: [PATCH 3/3] Fix epsilon in duration tests --- BitFaster.Caching.UnitTests/DurationTests.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/BitFaster.Caching.UnitTests/DurationTests.cs b/BitFaster.Caching.UnitTests/DurationTests.cs index 22c8e816..31df24ef 100644 --- a/BitFaster.Caching.UnitTests/DurationTests.cs +++ b/BitFaster.Caching.UnitTests/DurationTests.cs @@ -10,8 +10,6 @@ namespace BitFaster.Caching.UnitTests { public class DurationTests { - public static readonly ulong epsilon = (ulong)Duration.FromMilliseconds(20).raw; - private readonly ITestOutputHelper testOutputHelper; public DurationTests(ITestOutputHelper testOutputHelper) @@ -22,25 +20,26 @@ public DurationTests(ITestOutputHelper testOutputHelper) [Fact] public void SinceEpoch() { + // epsilon is 1/200 of a second + long epsilon = Stopwatch.Frequency / 200; + #if NETCOREAPP3_0_OR_GREATER if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp()); + TimeSpan.FromTicks(Duration.SinceEpoch().raw).ShouldBe(TimeSpan.FromTicks(Stopwatch.GetTimestamp()), TimeSpan.FromTicks(epsilon)); } else { - Duration.SinceEpoch().raw.ShouldBe(Environment.TickCount64); + TimeSpan.FromTicks(Duration.SinceEpoch().raw).ShouldBe(TimeSpan.FromTicks(Environment.TickCount64), TimeSpan.FromTicks(epsilon)); } #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Duration.SinceEpoch().raw.ShouldBe(Duration.GetTickCount64()); + TimeSpan.FromTicks(Duration.SinceEpoch().raw).ShouldBe(TimeSpan.FromTicks(Duration.GetTickCount64()), TimeSpan.FromTicks(epsilon)); } else { - // eps is 1/200 of a second - ulong eps = (ulong)(Stopwatch.Frequency / 200); - Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp()); + TimeSpan.FromTicks(Duration.SinceEpoch().raw).ShouldBe(TimeSpan.FromTicks(Stopwatch.GetTimestamp()), TimeSpan.FromTicks(epsilon)); } #endif }