Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
adamsitnik committed Aug 18, 2021
commit 40df3a349bf8d98b633f321c817629ee3119cebf
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,19 @@ public void NoCompactionWhenNoMaximumEntriesCountSpecified()
// There should be 6 items in the cache
Assert.Equal(6, cache.Count);
}

[Fact]
public void ClearZeroesTheSize()
{
var cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 10 });
Assert.Equal(0, cache.Size);

cache.Set("key", "value", new MemoryCacheEntryOptions { Size = 5 });
Assert.Equal(5, cache.Size);

cache.Clear();
Assert.Equal(0, cache.Size);
Assert.Equal(0, cache.Count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,29 @@ public void RemoveRemoves()
Assert.Null(result);
}

[Fact]
public void ClearClears()
{
var cache = (MemoryCache)CreateCache();
var obj = new object();
string[] keys = new string[] { "key1", "key2", "key3", "key4" };

foreach (var key in keys)
{
var result = cache.Set(key, obj);
Assert.Same(obj, result);
Assert.Same(obj, cache.Get(key));
}

cache.Clear();

Assert.Equal(0, cache.Count);
foreach (var key in keys)
{
Assert.Null(cache.Get(key));
}
}

[Fact]
public void RemoveRemovesAndInvokesCallback()
{
Expand Down Expand Up @@ -397,6 +420,38 @@ public void RemoveRemovesAndInvokesCallback()
Assert.Null(result);
}

[Fact]
public void ClearClearsAndInvokesCallback()
{
var cache = (MemoryCache)CreateCache();
var value = new object();
string key = "myKey";
var callbackInvoked = new ManualResetEvent(false);

var options = new MemoryCacheEntryOptions();
options.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration()
{
EvictionCallback = (subkey, subValue, reason, state) =>
{
Assert.Equal(key, subkey);
Assert.Same(value, subValue);
Assert.Equal(EvictionReason.Removed, reason);
var localCallbackInvoked = (ManualResetEvent)state;
localCallbackInvoked.Set();
},
State = callbackInvoked
});
var result = cache.Set(key, value, options);
Assert.Same(value, result);

cache.Clear();
Assert.Equal(0, cache.Count);
Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)), "Callback");

result = cache.Get(key);
Assert.Null(result);
}

[Fact]
public void RemoveAndReAddFromCallbackWorks()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ public void RemoveItemDisposesTokenRegistration()
Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)), "Callback");
}

[Fact]
public void ClearingCacheDisposesTokenRegistration()
{
var cache = (MemoryCache)CreateCache();
string key = "myKey";
var value = new object();
var callbackInvoked = new ManualResetEvent(false);
var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true };
cache.Set(key, value, new MemoryCacheEntryOptions()
.AddExpirationToken(expirationToken)
.RegisterPostEvictionCallback((subkey, subValue, reason, state) =>
{
// TODO: Verify params
var localCallbackInvoked = (ManualResetEvent)state;
localCallbackInvoked.Set();
}, state: callbackInvoked));
cache.Clear();

Assert.Equal(0, cache.Count);
Assert.NotNull(expirationToken.Registration);
Assert.True(expirationToken.Registration.Disposed);
Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)), "Callback");
}

[Fact]
public void AddExpiredTokenPreventsCaching()
{
Expand Down