Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BitFaster.Caching.Atomic;
using BitFaster.Caching.Lru;
using FluentAssertions;
using Moq;
using Xunit;

namespace BitFaster.Caching.UnitTests.Atomic
Expand All @@ -26,13 +22,62 @@ public async Task ScopedGetOrAddLifetimeIsAlwaysAlive(int _)
{
var cache = new AtomicFactoryScopedCache<int, Disposable>(new ConcurrentLru<int, ScopedAtomicFactory<int, Disposable>>(1, capacity, EqualityComparer<int>.Default));

var run = Threaded.Run(threadCount, _ =>
{
for (int i = 0; i < loopIterations; i++)
{
using (var lifetime = cache.ScopedGetOrAdd(i, k => { return new Scoped<Disposable>(new Disposable(k)); }))
{
lifetime.Value.IsDisposed.Should().BeFalse($"ref count {lifetime.ReferenceCount}");
}
}
});

await run;
}

#if NET9_0_OR_GREATER
[Theory]
[Repeat(soakIterations)]
public async Task ScopedGetOrAddAlternateLifetimeIsAlwaysAlive(int _)
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();

var run = Threaded.Run(threadCount, _ =>
{
var key = new char[8];

for (int i = 0; i < loopIterations; i++)
{
using (var lifetime = cache.ScopedGetOrAdd(i, k => { return new Scoped<Disposable>(new Disposable(k)); }))
(i + 1).TryFormat(key, out int written);

using (var lifetime = alternate.ScopedGetOrAdd(key.AsSpan().Slice(0, written), k => { return new Scoped<Disposable>(new Disposable(int.Parse(k))); }))
{
lifetime.Value.IsDisposed.Should().BeFalse($"ref count {lifetime.ReferenceCount}");
}
}
});

await run;
}

[Theory]
[Repeat(soakIterations)]
public async Task ScopedGetOrAddAlternateArgLifetimeIsAlwaysAlive(int _)
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();

var run = Threaded.Run(threadCount, _ =>
{
var key = new char[8];

for (int i = 0; i < loopIterations; i++)
{
(i + 1).TryFormat(key, out int written);

using (var lifetime = alternate.ScopedGetOrAdd(key.AsSpan().Slice(0, written), (k, offset) => { return new Scoped<Disposable>(new Disposable(int.Parse(k) + offset)); }, 1))
{
lifetime.Value.IsDisposed.Should().BeFalse($"ref count {lifetime.ReferenceCount}");
}
Expand All @@ -41,5 +86,6 @@ public async Task ScopedGetOrAddLifetimeIsAlwaysAlive(int _)

await run;
}
#endif
}
}
160 changes: 160 additions & 0 deletions BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BitFaster.Caching.Atomic;
using BitFaster.Caching.Lru;
Expand Down Expand Up @@ -131,5 +132,164 @@ public void WhenFactoryThrowsEmptyKeyIsNotEnumerable()

cache.Keys.Count().Should().Be(0);
}

#if NET9_0_OR_GREATER
[Fact]
public void TryGetAlternateLookupReturnsLookupForCompatibleComparer()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
using var lifetime = cache.ScopedGetOrAdd("42", _ => new Scoped<Disposable>(new Disposable(42)));
ReadOnlySpan<char> key = "42";

cache.TryGetAlternateLookup<ReadOnlySpan<char>>(out var alternate).Should().BeTrue();
alternate.ScopedTryGet(key, out var alternateLifetime).Should().BeTrue();
alternateLifetime.Value.State.Should().Be(42);
alternateLifetime.Dispose();
}

[Fact]
public void GetAlternateLookupThrowsForIncompatibleComparer()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));

Action act = () => cache.GetAlternateLookup<int>();

act.Should().Throw<InvalidOperationException>().WithMessage("Incompatible comparer");
cache.TryGetAlternateLookup<int>(out var alternate).Should().BeFalse();
alternate.Should().BeNull();
}

[Fact]
public void AlternateLookupTryRemoveReturnsActualKey()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
using var lifetime = cache.ScopedGetOrAdd("42", _ => new Scoped<Disposable>(new Disposable(42)));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
ReadOnlySpan<char> key = "42";

alternate.TryRemove(key, out var actualKey).Should().BeTrue();

actualKey.Should().Be("42");
cache.ScopedTryGet("42", out _).Should().BeFalse();
}

[Fact]
public void AlternateLookupScopedGetOrAddUsesActualKeyOnMissAndHit()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
var factoryCalls = 0;
ReadOnlySpan<char> key = "42";

using var lifetime = alternate.ScopedGetOrAdd(key, k =>
{
factoryCalls++;
return new Scoped<Disposable>(new Disposable(int.Parse(k)));
});

using var sameLifetime = alternate.ScopedGetOrAdd(key, (k, offset) =>
{
factoryCalls++;
return new Scoped<Disposable>(new Disposable(int.Parse(k) + offset));
}, 1);

lifetime.Value.State.Should().Be(42);
sameLifetime.Value.State.Should().Be(42);
factoryCalls.Should().Be(1);
}

[Fact]
public void WhenScopeIsDisposedTryGetAltReturnsFalse()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
var scope = new Scoped<Disposable>(new Disposable());

cache.ScopedGetOrAdd("a", k => scope);

scope.Dispose();

alternate.ScopedTryGet("a", out var lifetime).Should().BeFalse();
}

[Fact]
public void WhenKeyExistsTryRemoveAltReturnsTrue()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();

cache.AddOrUpdate("a", new Disposable());
alternate.TryRemove("a", out var key).Should().BeTrue();
key.Should().Be("a");
}

[Fact]
public void WhenItemDoesNotExistTryGetAltReturnsFalse()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
alternate.ScopedTryGet("a", out var lifetime).Should().BeFalse();
}

[Fact]
public void WhenKeyDoesNotExistTryRemoveAltReturnsFalse()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
alternate.TryRemove("a", out _).Should().BeFalse();
}

[Fact]
public void GetOrAddAltDisposedScopeThrows()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();

var scope = new Scoped<Disposable>(new Disposable());
scope.Dispose();

Action getOrAdd = () => { alternate.ScopedGetOrAdd("a", k => scope); };

getOrAdd.Should().Throw<InvalidOperationException>();
}

[Fact]
public void AlternateLookupTryUpdateReturnsFalseForMissingKeyAndUpdatesExistingValue()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
ReadOnlySpan<char> key = "42";

alternate.TryUpdate(key, new Disposable(42)).Should().BeFalse();
cache.ScopedTryGet("42", out _).Should().BeFalse();

using var lifetime = cache.ScopedGetOrAdd("42", _ => new Scoped<Disposable>(new Disposable(1)));
lifetime.Dispose();

alternate.TryUpdate(key, new Disposable(2)).Should().BeTrue();

alternate.ScopedTryGet(key, out var updatedLifetime).Should().BeTrue();
updatedLifetime.Value.State.Should().Be(2);
updatedLifetime.Dispose();
}

[Fact]
public void AlternateLookupAddOrUpdateAddsMissingValueAndUpdatesExistingValue()
{
var cache = new AtomicFactoryScopedCache<string, Disposable>(new ConcurrentLru<string, ScopedAtomicFactory<string, Disposable>>(1, capacity, StringComparer.Ordinal));
var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>();
ReadOnlySpan<char> key = "42";

alternate.AddOrUpdate(key, new Disposable(42));
alternate.ScopedTryGet(key, out var lifetime).Should().BeTrue();
lifetime.Value.State.Should().Be(42);
lifetime.Dispose();

alternate.AddOrUpdate(key, new Disposable(43));
alternate.ScopedTryGet(key, out var updatedLifetime).Should().BeTrue();
updatedLifetime.Value.State.Should().Be(43);
updatedLifetime.Dispose();
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task WhileBufferIsFilledBufferCanBeDrained()

[Theory]
[Repeat(SoakIterations)]
public async Task Count_WhileBufferIsConcurrentlyFilled_IsMonotonic(int iteration)
public async Task WhileBufferIsFilledCountCanBeTaken(int iteration)
{
this.testOutputHelper.WriteLine($"Iteration {iteration}");
this.testOutputHelper.WriteLine($"ProcessorCount={Environment.ProcessorCount}.");
Expand Down
34 changes: 28 additions & 6 deletions BitFaster.Caching.UnitTests/CacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,34 @@ public void WhenCacheInterfaceDefaultGetAlternateLookupThrows()
}

[Fact]
public void WhenCacheInterfaceDefaultTryGetAlternateLookupThrows()
{
var cache = new Mock<ICache<int, int>>();
cache.CallBase = true;

Action tryGetAlternateLookup = () => { cache.Object.TryGetAlternateLookup<string>(out var lookup); };
public void WhenCacheInterfaceDefaultTryGetAlternateLookupThrows()
{
var cache = new Mock<ICache<int, int>>();
cache.CallBase = true;

Action tryGetAlternateLookup = () => { cache.Object.TryGetAlternateLookup<string>(out var lookup); };

tryGetAlternateLookup.Should().Throw<NotSupportedException>();
}

[Fact]
public void WhenScopedCacheInterfaceDefaultGetAlternateLookupThrows()
{
var cache = new Mock<IScopedCache<int, Disposable>>();
cache.CallBase = true;

Action getAlternateLookup = () => { cache.Object.GetAlternateLookup<string>(); };

getAlternateLookup.Should().Throw<NotSupportedException>();
}

[Fact]
public void WhenScopedCacheInterfaceDefaultTryGetAlternateLookupThrows()
{
var cache = new Mock<IScopedCache<int, Disposable>>();
cache.CallBase = true;

Action tryGetAlternateLookup = () => { cache.Object.TryGetAlternateLookup<string>(out var lookup); };

tryGetAlternateLookup.Should().Throw<NotSupportedException>();
}
Expand Down
29 changes: 28 additions & 1 deletion BitFaster.Caching.UnitTests/ScopedCacheSoakTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -28,5 +29,31 @@ await Threaded.Run(4, () =>
});
}
}

#if NET9_0_OR_GREATER
[Fact]
public async Task WhenSoakAlternateScopedGetOrAddValueIsAlwaysAlive()
{
const int keyBufferLength = 5;
const int threadCount = 4;
var scopedCache = new ScopedCache<string, Disposable>(new ConcurrentLru<string, Scoped<Disposable>>(1, capacity, StringComparer.Ordinal));
var alternateLookup = scopedCache.GetAlternateLookup<ReadOnlySpan<char>>();

for (int i = 0; i < 10; i++)
{
await Threaded.Run(threadCount, _ =>
{
var key = new char[keyBufferLength];
for (int j = 0; j < 100000; j++)
{
j.TryFormat(key, out int written);
using var lifetime = alternateLookup.ScopedGetOrAdd(key.AsSpan(0, written), static k => new Scoped<Disposable>(new Disposable(int.Parse(k))));

lifetime.Value.IsDisposed.Should().BeFalse($"ref count {lifetime.ReferenceCount}");
}
});
}
}
#endif
}
}
Loading
Loading