-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
262a663
commit ad3cfb4
Showing
2 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using Jitbit.Utils; | ||
|
||
namespace UnitTests; | ||
|
||
[TestClass] | ||
public class EvictionCallbackTests | ||
{ | ||
private List<string> _evictedKeys; | ||
Check warning on line 9 in UnitTests/EvictionCallbackTests.cs GitHub Actions / build
|
||
private FastCache<string, string> _cache; | ||
Check warning on line 10 in UnitTests/EvictionCallbackTests.cs GitHub Actions / build
|
||
|
||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
_evictedKeys = new List<string>(); | ||
_cache = new FastCache<string, string>( | ||
cleanupJobInterval: 100, | ||
itemEvicted: key => _evictedKeys.Add(key)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task WhenItemExpires_EvictionCallbackFires() | ||
{ | ||
// Arrange | ||
var key = "test-key"; | ||
_cache.AddOrUpdate(key, "value", TimeSpan.FromMilliseconds(1)); | ||
|
||
// Act | ||
await Task.Delay(110); // Wait for expiration | ||
|
||
// Assert | ||
Assert.AreEqual(1, _evictedKeys.Count); | ||
Assert.AreEqual(key, _evictedKeys[0]); | ||
} | ||
|
||
[TestMethod] | ||
public async Task WhenMultipleItemsExpire_CallbackFiresForEach() | ||
{ | ||
// Arrange | ||
var keys = new[] { "key1", "key2", "key3" }; | ||
foreach (var key in keys) | ||
{ | ||
_cache.AddOrUpdate(key, "value", TimeSpan.FromMilliseconds(1)); | ||
} | ||
|
||
// Act | ||
await Task.Delay(5); // Wait for 1ms expiration | ||
_cache.EvictExpired(); | ||
await Task.Delay(5); // Wait for callback to finish on another thread | ||
|
||
// Assert | ||
CollectionAssert.AreEquivalent(keys, _evictedKeys); | ||
} | ||
|
||
[TestMethod] | ||
public void WhenItemNotExpired_CallbackDoesNotFire() | ||
{ | ||
// Arrange | ||
_cache.AddOrUpdate("key", "value", TimeSpan.FromMinutes(1)); | ||
|
||
// Act | ||
_cache.EvictExpired(); | ||
|
||
// Assert | ||
Assert.AreEqual(0, _evictedKeys.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AutomaticCleanup_FiresCallback() | ||
{ | ||
// Arrange | ||
_cache.AddOrUpdate("key", "value", TimeSpan.FromMilliseconds(1)); | ||
|
||
// Act | ||
await Task.Delay(110); // Wait for cleanup job | ||
|
||
// Assert | ||
Assert.AreEqual(1, _evictedKeys.Count); | ||
} | ||
} |