Skip to content

Commit

Permalink
Added unit test to check "GetOrAdd" atomicness
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-jitbit committed Sep 22, 2022
1 parent c0223b4 commit 6e9477f
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task TestGetOrAdd()
}

[TestMethod]
public async Task TestAtomicness()
public async Task TestTryAddAtomicness()
{
int i = 0;

Expand All @@ -90,6 +90,28 @@ await TestHelper.RunConcurrently(20, () => {
Assert.IsTrue(i == 1, i.ToString());
}

[TestMethod]
public async Task TestGetOrAddAtomicNess()
{
int i = 0;

var cache = new FastCache<int, int>();

cache.GetOrAdd(42, k => {
i++;
return 1024;
}, TimeSpan.FromMilliseconds(100));


await TestHelper.RunConcurrently(20, () => {
if (cache.TryAdd(42, 42, TimeSpan.FromSeconds(1)))
i++;
});

//test that add factory was called only once
Assert.IsTrue(i == 1, i.ToString());
}

[TestMethod]
public async Task Enumerator()
{
Expand Down

0 comments on commit 6e9477f

Please sign in to comment.