Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
328 changes: 314 additions & 14 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;
using BitFaster.Caching.Synchronized;
using FluentAssertions;
using Xunit;

Expand All @@ -14,7 +15,7 @@ public class ConcurrentLruBuilderTests
[Fact]
public void TestFastLru()
{
var lru = new ConcurrentLruBuilder<int, int>()
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.Build();

lru.Should().BeOfType<FastConcurrentLru<int, int>>();
Expand All @@ -23,7 +24,7 @@ public void TestFastLru()
[Fact]
public void TestMetricsLru()
{
var lru = new ConcurrentLruBuilder<int, int>()
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithMetrics()
.Build();

Expand All @@ -33,7 +34,7 @@ public void TestMetricsLru()
[Fact]
public void TestFastTLru()
{
var lru = new ConcurrentLruBuilder<int, int>()
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithExpireAfterWrite(TimeSpan.FromSeconds(1))
.Build();

Expand All @@ -43,7 +44,7 @@ public void TestFastTLru()
[Fact]
public void TestMetricsTLru()
{
var lru = new ConcurrentLruBuilder<int, int>()
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithExpireAfterWrite(TimeSpan.FromSeconds(1))
.WithMetrics()
.Build();
Expand All @@ -53,22 +54,55 @@ public void TestMetricsTLru()
}

[Fact]
public void TestScoped()
public void AsAsyncTestFastLru()
{
var lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.WithCapacity(3)
.WithExpireAfterWrite(TimeSpan.FromMinutes(1))
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.AsAsyncCache()
.Build();

lru.Should().BeOfType<ScopedCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
lru.Should().BeOfType<FastConcurrentLru<int, int>>();
}

[Fact]
public void AsAsyncTestMetricsLru()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithMetrics()
.AsAsyncCache()
.Build();

lru.Should().BeOfType<ConcurrentLru<int, int>>();
}

[Fact]
public void AsAsyncTestFastTLru()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithExpireAfterWrite(TimeSpan.FromSeconds(1))
.AsAsyncCache()
.Build();

lru.Should().BeOfType<FastConcurrentTLru<int, int>>();
}

[Fact]
public void AsAsyncTestMetricsTLru()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithExpireAfterWrite(TimeSpan.FromSeconds(1))
.WithMetrics()
.AsAsyncCache()
.Build();

lru.Should().BeOfType<ConcurrentTLru<int, int>>();
lru.Capacity.Should().Be(128);
}


[Fact]
public void TestComparer()
{
var fastLru = new ConcurrentLruBuilder<string, int>()
ICache<string, int> fastLru = new ConcurrentLruBuilder<string, int>()
.WithKeyComparer(StringComparer.OrdinalIgnoreCase)
.Build();

Expand All @@ -90,7 +124,7 @@ public void TestConcurrencyLevel()
[Fact]
public void TestIntCapacity()
{
var lru = new ConcurrentLruBuilder<int, Disposable>()
ICache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithCapacity(3)
.Build();

Expand All @@ -100,11 +134,277 @@ public void TestIntCapacity()
[Fact]
public void TestPartitionCapacity()
{
var lru = new ConcurrentLruBuilder<int, Disposable>()
ICache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithCapacity(new FavorFrequencyPartition(6))
.Build();

lru.Capacity.Should().Be(6);
}

// 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<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.WithCapacity(3)
.Build();

lru.Should().BeOfType<ScopedCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
}

// 2
[Fact]
public void WithAtomicFactory()
{
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithAtomicCreate()
.WithCapacity(3)
.Build();

lru.Should().BeOfType<AtomicFactoryCache<int, int>>();
}

// 3
[Fact]
public void AsAsync()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.AsAsyncCache()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IAsyncCache<int, int>>();
}

// 4
[Fact]
public void WithAtomicWithScope()
{
IScopedCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithAtomicCreate()
.WithScopedValues()
.WithCapacity(3)
.Build();

lru.Should().BeOfType<AtomicFactoryScopedCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
}

// 5
[Fact]
public void WithScopedWithAtomic()
{
IScopedCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.WithAtomicCreate()
.WithCapacity(3)
.Build();

lru.Should().BeOfType<AtomicFactoryScopedCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
}

// 6
[Fact]
public void AsAsyncWithScoped()
{
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.AsAsyncCache()
.WithScopedValues()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();

lru.Capacity.Should().Be(3);
}

// 7
[Fact]
public void WithScopedAsAsync()
{
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.AsAsyncCache()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
lru.Capacity.Should().Be(3);
}

// 8
[Fact]
public void WithAtomicAsAsync()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.WithAtomicCreate()
.AsAsyncCache()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IAsyncCache<int, int>>();
}

// 9
[Fact]
public void AsAsyncWithAtomic()
{
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
.AsAsyncCache()
.WithAtomicCreate()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IAsyncCache<int, int>>();
}

// 10
[Fact]
public void WithAtomicWithScopedAsAsync()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithAtomicCreate()
.WithScopedValues()
.AsAsyncCache()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}

// 11
[Fact]
public void WithAtomicAsAsyncWithScoped()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithAtomicCreate()
.AsAsyncCache()
.WithScopedValues()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}

// 12
[Fact]
public void WithScopedWithAtomicAsAsync()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.WithAtomicCreate()
.AsAsyncCache()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}

// 13
[Fact]
public void WithScopedAsAsyncWithAtomic()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.WithScopedValues()
.AsAsyncCache()
.WithAtomicCreate()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}

// 14
[Fact]
public void AsAsyncWithScopedWithAtomic()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.AsAsyncCache()
.WithScopedValues()
.WithAtomicCreate()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}

// 15
[Fact]
public void AsAsyncWithAtomicWithScoped()
{
// TODO: this will not resolve a TLru

IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
.AsAsyncCache()
.WithAtomicCreate()
.WithScopedValues()
.WithCapacity(3)
.Build();

lru.Should().BeAssignableTo<IScopedAsyncCache<int, Disposable>>();
}
}
}
Loading