Skip to content

Commit 602ec5b

Browse files
authored
atomicFactory (#417)
1 parent d43a040 commit 602ec5b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using BitFaster.Caching.Lru;
4+
using FluentAssertions;
5+
using Xunit;
6+
7+
namespace BitFaster.Caching.UnitTests.Atomic
8+
{
9+
[Collection("Soak")]
10+
public class AtomicFactorySoakTests
11+
{
12+
[Fact]
13+
public async Task WhenGetOrAddIsConcurrentValuesCreatedAtomically()
14+
{
15+
var cache = new ConcurrentLruBuilder<int, int>().WithAtomicGetOrAdd().WithCapacity(1024).Build();
16+
17+
var counters = new int[4];
18+
19+
await Threaded.Run(4, (r) =>
20+
{
21+
for (int i = 0; i < 1024; i++)
22+
{
23+
cache.GetOrAdd(i, k => { counters[r]++; return k; });
24+
}
25+
});
26+
27+
counters.Sum(x => x).Should().Be(1024);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)