Skip to content

Commit f6d925d

Browse files
committed
Enhanced caching tests and updated packages
- Upgraded various packages in `Promact.Caching.Test.csproj` for better compatibility and performance, including `Microsoft.NET.Test.Sdk`, `MSTest.TestAdapter`, `MSTest.TestFramework`, `coverlet.collector`, and `Promact.Core`. - Added new test methods in `RedisCacheTests.cs` to cover successful data retrieval, handling of null values, and behavior for non-existent keys. - Modified `DistributedCachingServices.cs` to improve key retrieval from `MemoryDistributedCache` using reflection and added a `GetAll` method for fetching all cache data. - Updated `Promact.Caching.csproj` with new package references and project metadata to enhance package information. - Introduced `InMemoryCacheTests.cs` and `NotSupportedProviderTest.cs` for testing in-memory caching functionality and behavior with unsupported cache providers, respectively.
1 parent 96e6706 commit f6d925d

File tree

6 files changed

+253
-11
lines changed

6 files changed

+253
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.Extensions.Caching.Distributed;
2+
using Microsoft.Extensions.Caching.Memory;
3+
using Microsoft.Extensions.Caching.StackExchangeRedis;
4+
using Microsoft.Extensions.Options;
5+
using Promact.Core.Caching;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace Promact.Caching.Test
13+
{
14+
[TestClass]
15+
public class InMemoryCacheTests
16+
{
17+
private IDistributedCache _distributedCache;
18+
private ICachingService _cacheService;
19+
private string[] _keys = new string[] { "TestKey1", "TestKey2", "TestKey3" };
20+
[TestInitialize]
21+
public void Setup()
22+
{
23+
IOptions<MemoryDistributedCacheOptions> options = Options.Create(new MemoryDistributedCacheOptions());
24+
_distributedCache = new MemoryDistributedCache(options);
25+
_cacheService = new DistributedCachingServices(_distributedCache);
26+
}
27+
28+
[TestMethod]
29+
public void AddDataSuccessTest()
30+
{
31+
var data = new TestData() { Name = "Test", Age = 20 };
32+
_cacheService.Set("TestKey1", data);
33+
34+
var result = _cacheService.Get<TestData>("TestKey1");
35+
Assert.IsNotNull(result);
36+
Assert.AreEqual(data.Name, result.Name);
37+
Assert.AreEqual(data.Age, result.Age);
38+
}
39+
40+
[TestMethod]
41+
public void GetAllKeysSuccessTest()
42+
{
43+
var data = new TestData() { Name = "Test", Age = 20 };
44+
_cacheService.Set("TestKey1", data);
45+
_cacheService.Set("TestKey2", data);
46+
_cacheService.Set("TestKey3", data);
47+
48+
var keys = _cacheService.GetAllKeys();
49+
Assert.IsNotNull(keys);
50+
Assert.AreEqual(3, keys.Count);
51+
}
52+
53+
[TestMethod]
54+
public void GetAllDataSuccessTest()
55+
{
56+
var data = new TestData() { Name = "Test", Age = 20 };
57+
_cacheService.Set("TestKey1", data);
58+
_cacheService.Set("TestKey2", data);
59+
_cacheService.Set("TestKey3", data);
60+
61+
var result = _cacheService.GetAll();
62+
Assert.IsNotNull(result);
63+
Assert.AreEqual(3, result.Count);
64+
}
65+
66+
[TestMethod]
67+
public void NullDataTest()
68+
{
69+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>("TestKey1", null));
70+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>(null, null));
71+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Get<TestData>(null));
72+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Remove(null));
73+
}
74+
75+
[TestMethod]
76+
public void AddDataRetriveNullTest()
77+
{
78+
var data = new TestData() { Name = "Test", Age = 20 };
79+
_cacheService.Set("TestKey1", data);
80+
81+
var result = _cacheService.Get<TestData>("TestKey2");
82+
Assert.IsNull(result);
83+
}
84+
85+
[TestCleanup]
86+
public void CleanUp()
87+
{
88+
_keys.ToList().ForEach(key => _cacheService.Remove(key));
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.Extensions.Caching.Distributed;
2+
using Promact.Core.Caching;
3+
4+
namespace Promact.Caching.Test
5+
{
6+
[TestClass]
7+
public class NotSupportedProviderTest
8+
{
9+
private ICachingService _cacheService;
10+
[TestInitialize]
11+
public void Setup()
12+
{
13+
var _distributedCache = new FakeCachingProvider();
14+
_cacheService = new DistributedCachingServices(_distributedCache);
15+
}
16+
17+
[TestMethod]
18+
public void GetAllKeyNotSupportedTest()
19+
{
20+
Assert.ThrowsException<NotSupportedException>(() => _cacheService.GetAllKeys());
21+
}
22+
23+
}
24+
25+
internal class FakeCachingProvider : IDistributedCache
26+
{
27+
public byte[] Get(string key)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
32+
public Task<byte[]> GetAsync(string key, CancellationToken token = default)
33+
{
34+
throw new NotImplementedException();
35+
}
36+
37+
public void Refresh(string key)
38+
{
39+
throw new NotImplementedException();
40+
}
41+
42+
public Task RefreshAsync(string key, CancellationToken token = default)
43+
{
44+
throw new NotImplementedException();
45+
}
46+
47+
public void Remove(string key)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
52+
public Task RemoveAsync(string key, CancellationToken token = default)
53+
{
54+
throw new NotImplementedException();
55+
}
56+
57+
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
}
67+
}

Promact.Caching/Promact.Caching.Test/Promact.Caching.Test.csproj

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.6" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
15-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
16-
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
17-
<PackageReference Include="coverlet.collector" Version="3.1.2" />
18-
<PackageReference Include="Promact.Core" Version="1.0.0.2" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
15+
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
16+
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
17+
<PackageReference Include="coverlet.collector" Version="6.0.2">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
<PackageReference Include="Promact.Core" Version="1.0.0.3" />
1922
</ItemGroup>
2023

2124
<ItemGroup>

Promact.Caching/Promact.Caching.Test/RedisCacheTests.cs

+45
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,51 @@ public void AddDataSuccessTest()
3434
Assert.AreEqual(data.Age, result.Age);
3535
}
3636

37+
[TestMethod]
38+
public void GetAllKeysSuccessTest()
39+
{
40+
var data = new TestData() { Name = "Test", Age = 20 };
41+
_cacheService.Set("TestKey1", data);
42+
_cacheService.Set("TestKey2", data);
43+
_cacheService.Set("TestKey3", data);
44+
45+
var keys = _cacheService.GetAllKeys();
46+
Assert.IsNotNull(keys);
47+
Assert.AreEqual(3, keys.Count);
48+
}
49+
50+
[TestMethod]
51+
public void GetAllDataSuccessTest()
52+
{
53+
var data = new TestData() { Name = "Test", Age = 20 };
54+
_cacheService.Set("TestKey1", data);
55+
_cacheService.Set("TestKey2", data);
56+
_cacheService.Set("TestKey3", data);
57+
58+
var result = _cacheService.GetAll();
59+
Assert.IsNotNull(result);
60+
Assert.AreEqual(3, result.Count);
61+
}
62+
63+
[TestMethod]
64+
public void NullDataTest()
65+
{
66+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>("TestKey1", null));
67+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>(null, null));
68+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Get<TestData>(null));
69+
Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Remove(null));
70+
}
71+
72+
[TestMethod]
73+
public void AddDataRetriveNullTest()
74+
{
75+
var data = new TestData() { Name = "Test", Age = 20 };
76+
_cacheService.Set("TestKey1", data);
77+
78+
var result = _cacheService.Get<TestData>("TestKey2");
79+
Assert.IsNull(result);
80+
}
81+
3782
[TestCleanup]
3883
public void CleanUp()
3984
{

Promact.Caching/Promact.Caching/DistributedCachingServices.cs

+31-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Caching.StackExchangeRedis;
55
using Promact.Core.Caching;
66
using StackExchange.Redis;
7+
using System.Collections;
78
using System.Reflection;
89
using System.Text.Json;
910

@@ -59,19 +60,44 @@ public HashSet<string> GetAllKeys()
5960
{
6061
var memoryCache = _distributedCache as MemoryDistributedCache;
6162
var cache = memoryCache.GetType().GetField("_memCache", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(memoryCache) as MemoryCache;
62-
dynamic _coherentState = cache.GetType().GetField("_coherentState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cache);
63+
var _coherentState = cache.GetType().GetField("_coherentState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cache);
64+
dynamic entries = _coherentState.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_coherentState);
65+
// entries is of type ICollection<KeyValuePair<object, CacheEntry>>, Here CacheEntry is a private class in MemoryCache. So, it is not accessible. Write a code to interate over entries and get Keys
6366
var keys = new HashSet<string>();
64-
foreach (KeyValuePair<object, dynamic> pairs in _coherentState._entries)
67+
var entriesCollection = entries as ICollection;
68+
if (entriesCollection != null)
6569
{
66-
keys.Add(Convert.ToString(pairs.Key));
67-
}
68-
return keys;
70+
foreach (var entry in entriesCollection)
71+
{
72+
var keyProperty = entry.GetType().GetProperty("Key");
73+
if (keyProperty != null)
74+
{
75+
var key = keyProperty.GetValue(entry);
76+
if (key != null)
77+
{
78+
keys.Add(Convert.ToString(key));
79+
}
80+
}
81+
}
82+
}
83+
return keys;
6984
}
7085
else
7186
{
7287
throw new NotSupportedException("This method is not supported for the current caching provider");
7388
}
7489
}
90+
91+
public IDictionary<string, string> GetAll()
92+
{
93+
var keys = GetAllKeys();
94+
var dictionary = new Dictionary<string, string>();
95+
foreach (var key in keys)
96+
{
97+
dictionary.Add(key, _distributedCache.GetString(key));
98+
}
99+
return dictionary;
100+
}
75101
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
76102
#pragma warning restore CS8602 // Dereference of a possibly null reference.
77103
#pragma warning restore CS8604 // Possible null reference argument.

Promact.Caching/Promact.Caching/Promact.Caching.csproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@
1212
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.6" />
15-
<PackageReference Include="Promact.Core" Version="1.0.0.2" />
15+
<PackageReference Include="Promact.Core" Version="1.0.0.3" />
1616
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
1717
<PackageReference Include="xxHash.Core" Version="2.0.1" />
1818
</ItemGroup>
19+
20+
<PropertyGroup>
21+
<PackageReadmeFile>README.md</PackageReadmeFile>
22+
<RepositoryUrl>https://github.com/Promact/promact-caching.git</RepositoryUrl>
23+
<PackageProjectUrl>https://github.com/Promact/promact-caching</PackageProjectUrl>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
28+
</ItemGroup>
1929

2030
</Project>

0 commit comments

Comments
 (0)