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

Remove last ! from Caching.Abstractions #85918

Merged
merged 5 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Caching.Memory
Expand Down Expand Up @@ -93,7 +94,7 @@ public static ICacheEntry RegisterPostEvictionCallback(
{
ThrowHelper.ThrowIfNull(callback);

return entry.RegisterPostEvictionCallback(callback, state: null);
return entry.RegisterPostEvictionCallbackNoValidation(callback, state: null);
}

/// <summary>
Expand All @@ -110,6 +111,14 @@ public static ICacheEntry RegisterPostEvictionCallback(
{
ThrowHelper.ThrowIfNull(callback);

return entry.RegisterPostEvictionCallbackNoValidation(callback, state);
}

private static ICacheEntry RegisterPostEvictionCallbackNoValidation(
this ICacheEntry entry,
PostEvictionDelegate callback,
object? state)
{
entry.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration()
{
EvictionCallback = callback,
Expand Down Expand Up @@ -172,12 +181,24 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt
entry.AddExpirationToken(expirationToken);
}

foreach (PostEvictionCallbackRegistration postEvictionCallback in options.PostEvictionCallbacks)
for (int i = 0; i < options.PostEvictionCallbacks.Count; i++)
{
entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback!, postEvictionCallback.State);
PostEvictionCallbackRegistration postEvictionCallback = options.PostEvictionCallbacks[i];
if (postEvictionCallback.EvictionCallback is null)
ThrowNullCallback(i, nameof(options));

entry.RegisterPostEvictionCallbackNoValidation(postEvictionCallback.EvictionCallback, postEvictionCallback.State);
}

return entry;
}

[DoesNotReturn]
private static void ThrowNullCallback(int index, string paramName)
{
string message =
$"MemoryCacheEntryOptions.PostEvictionCallbacks contains a PostEvictionCallbackRegistration with a null EvictionCallback at index {index}.";
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException(message, paramName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,29 @@ public void ClearClears()
}
}

[Fact]
public void SetNullCallback_NotAllowed_ArgumentException()
{
var cache = CreateCache();
const string someKey = "test";
var entry = cache.CreateEntry(someKey);

var options = new MemoryCacheEntryOptions();

var notNullCallback = new PostEvictionCallbackRegistration()
{
EvictionCallback = (_, _, _, _) => {}
};

options.PostEvictionCallbacks.Add(notNullCallback);

var nullCallback = new PostEvictionCallbackRegistration();

options.PostEvictionCallbacks.Add(nullCallback);

Assert.Throws<ArgumentException>(() => entry.SetOptions(options));
}

[Fact]
public void RemoveRemovesAndInvokesCallback()
{
Expand Down