-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add missing coverage #2514
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
Merged
Merged
Add missing coverage #2514
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| /// <remarks>Uses ConcurrentDictionary to store the collection.</remarks> | ||
| public class PolicyRegistry : IConcurrentPolicyRegistry<string> | ||
| { | ||
| private readonly IDictionary<string, IsPolicy> _registry = new ConcurrentDictionary<string, IsPolicy>(); | ||
| private readonly ConcurrentDictionary<string, IsPolicy> _registry = []; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PolicyRegistry"/> class, with <see cref="string"/> keys. | ||
|
|
@@ -26,22 +26,9 @@ public PolicyRegistry() | |
| /// </summary> | ||
| /// <param name="registry">a dictionary containing keys and policies used for testing.</param> | ||
| internal PolicyRegistry(IDictionary<string, IsPolicy> registry) | ||
| { | ||
| #pragma warning disable S3236 // Remove this argument from the method call; it hides the caller information. | ||
| Debug.Assert(registry != null, "This constructor is for testing only, and should not be called with a null registry."); | ||
| #pragma warning restore S3236 // Remove this argument from the method call; it hides the caller information. | ||
| _registry = registry; | ||
| } | ||
|
|
||
| private ConcurrentDictionary<string, IsPolicy> ThrowIfNotConcurrentImplementation() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant if we just enforce that the test constructor creates a concurrent dictionary. |
||
| { | ||
| if (_registry is ConcurrentDictionary<string, IsPolicy> concurrentRegistry) | ||
| { | ||
| return concurrentRegistry; | ||
| } | ||
|
|
||
| throw new InvalidOperationException($"This {nameof(PolicyRegistry)} is not configured for concurrent operations. This exception should never be thrown in production code as the only public constructors create {nameof(PolicyRegistry)} instances of the correct form."); | ||
| } | ||
| #pragma warning disable IDE0306 // Simplify collection initialization | ||
| => _registry = new(registry); | ||
| #pragma warning restore IDE0306 // Simplify collection initialization | ||
|
|
||
| /// <summary> | ||
| /// Gets the total number of policies in the registry. | ||
|
|
@@ -57,7 +44,11 @@ private ConcurrentDictionary<string, IsPolicy> ThrowIfNotConcurrentImplementatio | |
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is <see langword="null"/>.</exception> | ||
| /// <exception cref="ArgumentException">A Policy with same <paramref name="key"/> already exists.</exception> | ||
| public void Add<TPolicy>(string key, TPolicy policy) | ||
| where TPolicy : IsPolicy => _registry.Add(key, policy); | ||
| where TPolicy : IsPolicy | ||
| { | ||
| IDictionary<string, IsPolicy> dictionary = _registry; | ||
| dictionary.Add(key, policy); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a policy with the provided key and policy to the registry. | ||
|
|
@@ -68,11 +59,7 @@ public void Add<TPolicy>(string key, TPolicy policy) | |
| /// <returns>True if Policy was added. False otherwise.</returns> | ||
| public bool TryAdd<TPolicy>(string key, TPolicy policy) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return registry.TryAdd(key, policy); | ||
| } | ||
| => _registry.TryAdd(key, policy); | ||
|
|
||
| /// <summary> | ||
| /// Gets of sets the <see cref="IsPolicy"/> with the specified key. | ||
|
|
@@ -145,8 +132,11 @@ public bool ContainsKey(string key) => | |
| /// <param name="key">The <paramref name="key"/> of the policy to remove.</param> | ||
| /// <returns>True if the policy is successfully removed. Otherwise false.</returns> | ||
| /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> | ||
| public bool Remove(string key) => | ||
| _registry.Remove(key); | ||
| public bool Remove(string key) | ||
| { | ||
| IDictionary<string, IsPolicy> dictionary = _registry; | ||
| return dictionary.Remove(key); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Removes the policy stored under the specified <paramref name="key"/> from the registry. | ||
|
|
@@ -162,10 +152,8 @@ public bool Remove(string key) => | |
| public bool TryRemove<TPolicy>(string key, out TPolicy policy) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| policy = default; | ||
| bool got = registry.TryRemove(key, out IsPolicy value); | ||
| bool got = _registry.TryRemove(key, out IsPolicy value); | ||
|
|
||
| if (got) | ||
| { | ||
|
|
@@ -188,11 +176,7 @@ public bool TryRemove<TPolicy>(string key, out TPolicy policy) | |
| /// </returns> | ||
| public bool TryUpdate<TPolicy>(string key, TPolicy newPolicy, TPolicy comparisonPolicy) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return registry.TryUpdate(key, newPolicy, comparisonPolicy); | ||
| } | ||
| => _registry.TryUpdate(key, newPolicy, comparisonPolicy); | ||
|
|
||
| /// <summary> | ||
| /// Adds a policy with the provided key and policy to the registry | ||
|
|
@@ -206,11 +190,7 @@ public bool TryUpdate<TPolicy>(string key, TPolicy newPolicy, TPolicy comparison | |
| /// if the key was not in the registry.</returns> | ||
| public TPolicy GetOrAdd<TPolicy>(string key, Func<string, TPolicy> policyFactory) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return (TPolicy)registry.GetOrAdd(key, k => policyFactory(k)); | ||
| } | ||
| => (TPolicy)_registry.GetOrAdd(key, k => policyFactory(k)); | ||
|
|
||
| /// <summary> | ||
| /// Adds a key/policy pair to the registry | ||
|
|
@@ -223,11 +203,7 @@ public TPolicy GetOrAdd<TPolicy>(string key, Func<string, TPolicy> policyFactory | |
| /// key is already in the registry, or the new policy if the key was not in the registry.</returns> | ||
| public TPolicy GetOrAdd<TPolicy>(string key, TPolicy policy) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return (TPolicy)registry.GetOrAdd(key, policy); | ||
| } | ||
| => (TPolicy)_registry.GetOrAdd(key, policy); | ||
|
|
||
| /// <summary> | ||
| /// Adds a key/policy pair to the registry if the key does not already | ||
|
|
@@ -243,11 +219,7 @@ public TPolicy GetOrAdd<TPolicy>(string key, TPolicy policy) | |
| /// absent) or the result of updatePolicyFactory (if the key was present).</returns> | ||
| public TPolicy AddOrUpdate<TPolicy>(string key, Func<string, TPolicy> addPolicyFactory, Func<string, TPolicy, TPolicy> updatePolicyFactory) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return (TPolicy)registry.AddOrUpdate(key, k => addPolicyFactory(k), (k, e) => updatePolicyFactory(k, (TPolicy)e)); | ||
| } | ||
| => (TPolicy)_registry.AddOrUpdate(key, k => addPolicyFactory(k), (k, e) => updatePolicyFactory(k, (TPolicy)e)); | ||
|
|
||
| /// <summary> | ||
| /// Adds a key/policy pair to the registry if the key does not already | ||
|
|
@@ -263,11 +235,7 @@ public TPolicy AddOrUpdate<TPolicy>(string key, Func<string, TPolicy> addPolicyF | |
| /// absent) or the result of updatePolicyFactory (if the key was present).</returns> | ||
| public TPolicy AddOrUpdate<TPolicy>(string key, TPolicy addPolicy, Func<string, TPolicy, TPolicy> updatePolicyFactory) | ||
| where TPolicy : IsPolicy | ||
| { | ||
| var registry = ThrowIfNotConcurrentImplementation(); | ||
|
|
||
| return (TPolicy)registry.AddOrUpdate(key, addPolicy, (k, e) => updatePolicyFactory(k, (TPolicy)e)); | ||
| } | ||
| => (TPolicy)_registry.AddOrUpdate(key, addPolicy, (k, e) => updatePolicyFactory(k, (TPolicy)e)); | ||
|
|
||
| /// <summary>Returns an enumerator that iterates through the policy objects in the <see | ||
| /// cref="PolicyRegistry"/>.</summary> | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
AddOptions<T>()on Line 258 does this under-the-hood.