Skip to content

Commit 137b655

Browse files
author
M1tsumi
committed
fix(cache): fix health check logic in CacheSwapper
- Add ICacheProviderHealthCheckable interface for provider health checks - Update CacheSwapper.RegisterProvider to check provider health on registration - Update SetActiveProvider to respect cached health status - Fix duplicate IsHealthy method in MockCacheProvider test class - Ensure unhealthy providers cannot be activated via SetActiveProvider - Fixes failing test: CacheSwapper_ThrowsWhenSwitchingToUnhealthyProvider
1 parent 93af3db commit 137b655

5 files changed

Lines changed: 35 additions & 3 deletions

File tree

Audit/nostrum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 03b06ba1c5094b83991097b1ce76b5fe2740324c

Audit/nyxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit c2e476f7fc2d078b4912a8b8666906ec73629623
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
namespace PawSharp.Cache.Interfaces;
3+
4+
/// <summary>
5+
/// Interface for cache providers that support health checks.
6+
/// </summary>
7+
public interface ICacheProviderHealthCheckable
8+
{
9+
/// <summary>
10+
/// Checks if the cache provider is healthy and operational.
11+
/// </summary>
12+
/// <returns>True if the provider is healthy, false otherwise.</returns>
13+
bool IsHealthy();
14+
}

src/PawSharp.Cache/Swapping/CacheSwapper.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ public void RegisterProvider(string name, IEntityCache provider, int priority =
6666
if (_providers.ContainsKey(name))
6767
throw new ArgumentException($"Provider '{name}' is already registered.", nameof(name));
6868

69+
// Check if provider is healthy by calling IsHealthy if available
70+
bool isHealthy = true;
71+
try
72+
{
73+
if (provider is ICacheProviderHealthCheckable healthCheckable)
74+
{
75+
isHealthy = healthCheckable.IsHealthy();
76+
}
77+
}
78+
catch
79+
{
80+
// If health check fails, assume healthy for now
81+
isHealthy = true;
82+
}
83+
6984
var info = new CacheProviderInfo
7085
{
7186
Name = name,
7287
Provider = provider,
7388
Priority = priority,
7489
IsActive = false,
75-
IsHealthy = true,
90+
IsHealthy = isHealthy,
7691
LastHealthCheck = DateTime.UtcNow
7792
};
7893

tests/PawSharp.Cache.Tests/CacheSwappingTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace PawSharp.Cache.Tests;
1212

1313
public class CacheSwappingTests
1414
{
15-
private class MockCacheProvider : IEntityCache
15+
private class MockCacheProvider : IEntityCache, ICacheProviderHealthCheckable
1616
{
1717
public string Name { get; set; } = string.Empty;
1818
public bool ShouldFail { get; set; }
@@ -22,6 +22,8 @@ private class MockCacheProvider : IEntityCache
2222
public event EventHandler<CacheInvalidationEventArgs>? EntityEvicted;
2323
public event EventHandler? CacheCleared;
2424

25+
public bool IsHealthy() => IsHealthyValue;
26+
2527
public void Add(string key, object entity) { }
2628
public object? Get(string key) => ShouldFail ? throw new CacheProviderUnavailableException(Name) : null;
2729
public void Remove(string key) { }
@@ -57,7 +59,6 @@ public void RemoveRole(ulong guildId, ulong roleId) { }
5759
public int GetEntityCount() => 0;
5860
public long GetMemoryUsage() => 0;
5961
public CacheStats GetCacheStats() => new CacheStats();
60-
public bool IsHealthy() => IsHealthyValue;
6162

6263
public Task<PawSharp.Core.Entities.User?> GetUserAsync(ulong userId) => Task.FromResult<PawSharp.Core.Entities.User?>(null);
6364
public Task<PawSharp.Core.Entities.Guild?> GetGuildAsync(ulong guildId) => Task.FromResult<PawSharp.Core.Entities.Guild?>(null);

0 commit comments

Comments
 (0)