Skip to content

Commit

Permalink
atomicFactory (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfaster authored Oct 14, 2023
1 parent d43a040 commit 602ec5b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Linq;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;
using FluentAssertions;
using Xunit;

namespace BitFaster.Caching.UnitTests.Atomic
{
[Collection("Soak")]
public class AtomicFactorySoakTests
{
[Fact]
public async Task WhenGetOrAddIsConcurrentValuesCreatedAtomically()
{
var cache = new ConcurrentLruBuilder<int, int>().WithAtomicGetOrAdd().WithCapacity(1024).Build();

var counters = new int[4];

await Threaded.Run(4, (r) =>
{
for (int i = 0; i < 1024; i++)
{
cache.GetOrAdd(i, k => { counters[r]++; return k; });
}
});

counters.Sum(x => x).Should().Be(1024);

Check failure on line 27 in BitFaster.Caching.UnitTests/Atomic/AtomicFactorySoakTests.cs

View workflow job for this annotation

GitHub Actions / test results (win net4.8)

BitFaster.Caching.UnitTests.Atomic.AtomicFactorySoakTests ► WhenGetOrAddIsConcurrentValuesCreatedAtomically

Failed test found in: BitFaster.Caching.UnitTests/TestResults/results4.trx Error: Expected counters.Sum(x => x) to be 1024, but found 1071 (difference of 47).
Raw output
Expected counters.Sum(x => x) to be 1024, but found 1071 (difference of 47).
   at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Numeric.NumericAssertions`2.Be(T expected, String because, Object[] becauseArgs)
   at BitFaster.Caching.UnitTests.Atomic.AtomicFactorySoakTests.<WhenGetOrAddIsConcurrentValuesCreatedAtomically>d__0.MoveNext() in D:\a\BitFaster.Caching\BitFaster.Caching\BitFaster.Caching.UnitTests\Atomic\AtomicFactorySoakTests.cs:line 27
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
}
}
}

0 comments on commit 602ec5b

Please sign in to comment.