Skip to content

Commit 9d14dc6

Browse files
committed
add ConcurrentCacheTest
1 parent 988a07b commit 9d14dc6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using WebApiClient;
5+
using Xunit;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApiClientTest.Internal
10+
{
11+
public class ConcurrentCacheTest
12+
{
13+
private int count = 0;
14+
15+
[Fact]
16+
public void GetOrAddGet()
17+
{
18+
var key = "WebApiClient";
19+
var cache = new ConcurrentCache<string, int>();
20+
21+
Parallel.For(0, 1000, (i) =>
22+
{
23+
var value = cache.GetOrAdd(key, k =>
24+
{
25+
Interlocked.Increment(ref this.count);
26+
return 1;
27+
});
28+
29+
Assert.True(value == 1);
30+
Assert.True(count == 1);
31+
});
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)