Skip to content

Commit b1b7b7c

Browse files
authored
soak test ScopedAsyncAtomicFactory (#428)
* soak * dispose ---------
1 parent 979ee61 commit b1b7b7c

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Collections.Concurrent;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using BitFaster.Caching.Atomic;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace BitFaster.Caching.UnitTests.Atomic
9+
{
10+
[Collection("Soak")]
11+
public class ScopedAsyncAtomicFactorySoakTests
12+
{
13+
private const int threads = 4;
14+
private const int items = 1024;
15+
16+
[Fact]
17+
public async Task WhenGetOrAddIsConcurrentValuesCreatedAtomically()
18+
{
19+
var dictionary = new ConcurrentDictionary<int, ScopedAsyncAtomicFactory<int, Disposable>>(concurrencyLevel: threads, capacity: items);
20+
var counters = new int[threads];
21+
22+
await Threaded.RunAsync(threads, async (r) =>
23+
{
24+
for (int i = 0; i < items; i++)
25+
{
26+
while (true)
27+
{
28+
var scope = dictionary.GetOrAdd(i, k => new ScopedAsyncAtomicFactory<int, Disposable>());
29+
var (success, lifetime) = await scope.TryCreateLifetimeAsync(i, k => { counters[r]++; return Task.FromResult(new Scoped<Disposable>(new Disposable(k))); });
30+
31+
if (success)
32+
{
33+
using (lifetime)
34+
{
35+
lifetime.Value.IsDisposed.Should().BeFalse();
36+
}
37+
38+
break;
39+
}
40+
}
41+
}
42+
});
43+
44+
counters.Sum(x => x).Should().Be(items);
45+
}
46+
47+
[Fact]
48+
public async Task WhenGetOrAddAndDisposeIsConcurrentLifetimesAreValid()
49+
{
50+
var dictionary = new ConcurrentDictionary<int, ScopedAsyncAtomicFactory<int, Disposable>>(concurrencyLevel: threads, capacity: items);
51+
52+
await Threaded.RunAsync(threads, async (r) =>
53+
{
54+
for (int i = 0; i < items; i++)
55+
{
56+
if (dictionary.TryRemove(i, out var d))
57+
{
58+
d.Dispose();
59+
}
60+
61+
while (true)
62+
{
63+
var scope = dictionary.GetOrAdd(i, k => new ScopedAsyncAtomicFactory<int, Disposable>());
64+
var (success, lifetime) = await scope.TryCreateLifetimeAsync(i, k => { return Task.FromResult(new Scoped<Disposable>(new Disposable(k))); });
65+
66+
if (success)
67+
{
68+
using (lifetime)
69+
{
70+
lifetime.Value.IsDisposed.Should().BeFalse();
71+
}
72+
73+
break;
74+
}
75+
}
76+
}
77+
});
78+
}
79+
}
80+
}

BitFaster.Caching.UnitTests/Atomic/ScopedAtomicFactorySoakTests.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ namespace BitFaster.Caching.UnitTests.Atomic
1010
[Collection("Soak")]
1111
public class ScopedAtomicFactorySoakTests
1212
{
13+
private const int threads = 4;
14+
private const int items = 1024;
15+
1316
[Fact]
1417
public async Task WhenGetOrAddIsConcurrentValuesCreatedAtomically()
1518
{
16-
const int threads = 4;
17-
const int items = 1024;
1819
var dictionary = new ConcurrentDictionary<int, ScopedAtomicFactory<int, Disposable>>(concurrencyLevel: threads, capacity: items);
1920
var counters = new int[threads];
2021

@@ -43,12 +44,9 @@ await Threaded.Run(threads, (r) =>
4344
}
4445

4546
[Fact]
46-
public async Task WhenGetOrAddAndDisposeIsConcurrentValuesCreatedAtomically()
47+
public async Task WhenGetOrAddAndDisposeIsConcurrentLifetimesAreValid()
4748
{
48-
const int threads = 4;
49-
const int items = 1024;
5049
var dictionary = new ConcurrentDictionary<int, ScopedAtomicFactory<int, Disposable>>(concurrencyLevel: threads, capacity: items);
51-
var counters = new int[threads];
5250

5351
await Threaded.Run(threads, (r) =>
5452
{
@@ -63,7 +61,7 @@ await Threaded.Run(threads, (r) =>
6361
{
6462
var scoped = dictionary.GetOrAdd(i, k => new ScopedAtomicFactory<int, Disposable>());
6563

66-
if (scoped.TryCreateLifetime(i, k => { counters[r]++; return new Scoped<Disposable>(new Disposable(k)); }, out var lifetime))
64+
if (scoped.TryCreateLifetime(i, k => { return new Scoped<Disposable>(new Disposable(k)); }, out var lifetime))
6765
{
6866
using (lifetime)
6967
{
@@ -73,7 +71,6 @@ await Threaded.Run(threads, (r) =>
7371
break;
7472
}
7573
}
76-
7774
}
7875
});
7976
}

0 commit comments

Comments
 (0)