Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PostEvictionCallbacks bug #46807

Merged
merged 4 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private sealed class CacheEntryTokens
internal List<IChangeToken> ExpirationTokens => _expirationTokens ??= new List<IChangeToken>();
internal List<PostEvictionCallbackRegistration> PostEvictionCallbacks => _postEvictionCallbacks ??= new List<PostEvictionCallbackRegistration>();

internal void AttachTokens()
internal void AttachTokens(CacheEntry cacheEntry)
{
if (_expirationTokens != null)
{
Expand All @@ -35,7 +35,7 @@ internal void AttachTokens()
if (expirationToken.ActiveChangeCallbacks)
{
_expirationTokenRegistrations ??= new List<IDisposable>(1);
IDisposable registration = expirationToken.RegisterChangeCallback(ExpirationCallback, this);
IDisposable registration = expirationToken.RegisterChangeCallback(ExpirationCallback, cacheEntry);
_expirationTokenRegistrations.Add(registration);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ bool FullCheck(in DateTimeOffset offset)
}
}

internal void AttachTokens() => _tokens?.AttachTokens();
internal void AttachTokens() => _tokens?.AttachTokens(this);

private static void ExpirationTokensExpired(object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonTestPath)System\Threading\Tasks\TaskTimeoutExtensions.cs"
Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Microsoft.Extensions.Caching.Memory.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection\src\Microsoft.Extensions.DependencyInjection.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,33 @@ public void TokenExpiresOnRegister()
Assert.Null(result);
}

[Fact]
public async Task PostEvictionCallbacksGetInvokedWhenMemoryCacheEntriesExpireWithAnActiveChangeToken()
{
var cache = new MemoryCache(new MemoryCacheOptions());
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
var key = new object();

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
var tcs = new TaskCompletionSource<object>();

cache.Set(key, new object(), new MemoryCacheEntryOptions
{
ExpirationTokens = { new Microsoft.Extensions.Primitives.CancellationChangeToken(cts.Token) },
PostEvictionCallbacks = { new PostEvictionCallbackRegistration { EvictionCallback = OnEntryEvicted } },
});

Assert.True(cache.TryGetValue(key, out _));

await Task.Run(() => tcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10)));
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved

Assert.False(cache.TryGetValue(key, out _));

void OnEntryEvicted(object key, object value, EvictionReason reason, object state)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
tcs.TrySetResult(new object());
}
}

internal class TestToken : IChangeToken
{
private bool _hasChanged;
Expand Down