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

Added PolicyRegistry Feature #231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8a37693
Added IPolicyRegistry interface
ankitbko Feb 11, 2017
7541a44
Removed singleton implementation
ankitbko Feb 11, 2017
df51e49
Changed the implementation to use ConcurrentDictionary
ankitbko Feb 13, 2017
cb1cdb6
Polly now targets .NETStandard 1.1
ankitbko Feb 13, 2017
d81b7ea
Added comments to Add method.
ankitbko Feb 13, 2017
a48fa1d
Removed inheritance of IDictionary Interface from IPolicyRegistry.
ankitbko Feb 16, 2017
6e68bda
Added unit test cases for DefaultPolicyRegistry
ankitbko Feb 16, 2017
82a0e92
Renamed folder and csproj from Polly.NetStandard10 to Polly.NetStanda…
ankitbko Feb 20, 2017
eecd9b9
- Added a test to check adding policy wuth duplicate key through inde…
ankitbko Feb 20, 2017
a0386ac
Updated Cake script to reflect folder and csproj renaming to NetStand…
ankitbko Feb 20, 2017
0c6a8f4
Changed namespace and renamed specs to follow Polly naming convention.
ankitbko Mar 6, 2017
e596c71
Removed duplicate tests of indexer.
ankitbko Mar 6, 2017
2d0fa73
Explicitly named multiple policy and key accordingly
ankitbko Mar 6, 2017
c5a1fa2
Changed ShouldBeEquivalentTo to Should().BeSameAs()
ankitbko Mar 6, 2017
37d7363
Removed redundant test
ankitbko Mar 6, 2017
44bc1f2
Made 'K' small case in Key in spec names
ankitbko Mar 6, 2017
e733513
Added tests to check
ankitbko Mar 6, 2017
dbded5a
Added test to check if exception is thrown when key is null which usi…
ankitbko Mar 6, 2017
a129649
Removed redundant ShouldNotThrow() from tests
ankitbko Mar 6, 2017
a890d76
Refactored the comments.
ankitbko Mar 6, 2017
bfd2a5a
Followed MS convention of prefixing 'T' to type param.
ankitbko Mar 6, 2017
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
8 changes: 4 additions & 4 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var snkFile = srcDir + File(keyName);

var projectToNugetFolderMap = new Dictionary<string, string[]>() {
{ "Net45" , new [] {"net45"} },
{ "NetStandard10", new [] {"netstandard1.0"} },
{ "NetStandard11", new [] {"netstandard1.1"} },
};

var net40AsyncProjectToNugetFolderMap = new Dictionary<string, string[]>() {
Expand Down Expand Up @@ -141,11 +141,11 @@ Task("__UpdateDotNetStandardAssemblyVersionNumber")
// NOTE: TEMPORARY fix only, while GitVersionTask does not support .Net Standard assemblies. See https://github.com/App-vNext/Polly/issues/176.
// This build Task can be removed when GitVersionTask supports .Net Standard assemblies.
var assemblySemVer = gitVersionOutput["AssemblySemVer"].ToString();
Information("Updating NetStandard10 AssemblyVersion to {0}", assemblySemVer);
var replacedFiles = ReplaceRegexInFiles("./src/Polly.NetStandard10/Properties/AssemblyInfo.cs", "AssemblyVersion[(]\".*\"[)]", "AssemblyVersion(\"" + assemblySemVer +"\")");
Information("Updating NetStandard11 AssemblyVersion to {0}", assemblySemVer);
var replacedFiles = ReplaceRegexInFiles("./src/Polly.NetStandard11/Properties/AssemblyInfo.cs", "AssemblyVersion[(]\".*\"[)]", "AssemblyVersion(\"" + assemblySemVer +"\")");
if (!replacedFiles.Any())
{
Information("NetStandard1.0 AssemblyVersion could not be updated.");
Information("NetStandard1.1 AssemblyVersion could not be updated.");
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"NETStandard.Library": "1.6.1"
},
"frameworks": {
"netstandard1.0": {}
"netstandard1.1": {}
}
}
10 changes: 5 additions & 5 deletions src/Polly.Pcl.Specs/Polly.Pcl.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
Expand Down Expand Up @@ -72,21 +72,21 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Polly.NetStandard10\Polly.NetStandard10.csproj">
<ProjectReference Include="..\Polly.NetStandard11\Polly.NetStandard11.csproj">
<Project>{5017174e-dac5-4408-ab15-1f902f40c612}</Project>
<Name>Polly.NetStandard10</Name>
<Name>Polly.NetStandard11</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\Polly.SharedSpecs\Polly.SharedSpecs.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\packages\GitVersionTask.3.1.2\Build\portable-net+sl+win+wpa+wp\GitVersionTask.targets" Condition="Exists('..\packages\GitVersionTask.3.1.2\Build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\GitVersionTask.3.1.2\Build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitVersionTask.3.1.2\Build\portable-net+sl+win+wpa+wp\GitVersionTask.targets'))" />
<Error Condition="!Exists('..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props'))" />
<Error Condition="!Exists('..\packages\GitVersionTask.3.1.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitVersionTask.3.1.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets'))" />
</Target>
<Import Project="..\packages\GitVersionTask.3.1.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets" Condition="Exists('..\packages\GitVersionTask.3.1.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
16 changes: 8 additions & 8 deletions src/Polly.Pcl.Specs/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="4.17.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="GitVersionTask" version="3.1.2" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="xunit" version="2.1.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="xunit.assert" version="2.1.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="xunit.core" version="2.1.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="FluentAssertions" version="4.17.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="GitVersionTask" version="3.1.2" targetFramework="portable45-net45+win8+wpa81" developmentDependency="true" />
<package id="xunit" version="2.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="xunit.assert" version="2.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="xunit.core" version="2.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="portable45-net45+win8+wpa81" />
</packages>
2 changes: 2 additions & 0 deletions src/Polly.Shared/Polly.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
<Compile Include="$(MSBuildThisFileDirectory)PolicyBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicyBuilder.OrSyntax.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicyResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\DefaultPolicyRegistry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\IPolicyRegistry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ResultPredicate.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetrySyntax.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetrySyntaxAsync.cs" />
Expand Down
82 changes: 82 additions & 0 deletions src/Polly.Shared/Registry/DefaultPolicyRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System;

namespace Polly.Registry
{
/// <summary>
/// Stores a registry of <see cref="System.String"/> and <see cref="Policy"/> pair.
/// </summary>
/// <remarks>Uses ConcurrentDictionary to store the collection.</remarks>
public class DefaultPolicyRegistry : IPolicyRegistry<string, Policy>
{
private IDictionary<string, Policy> _registry = new ConcurrentDictionary<string, Policy>();

/// <summary>
/// Gets or sets the <see cref="Policy"/> associated with the specified key.
/// </summary>
/// <param name="key">The key of the <see cref="Policy"/> to get or set.</param>
/// <returns>The <see cref="Policy"/> with specified <paramref name="key"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="KeyNotFoundException">Policy with the specified <paramref name="key"/> does not exists in the registry.</exception>
public Policy this[string key]
{
get { return _registry[key]; }

set { _registry[key] = value; }
}

/// <summary>
/// Total number of policies in the registry.
/// </summary>
public int Count => _registry.Count;

/// <summary>
/// Adds an element with the provided key and <see cref="Policy"/> to the registry.
/// </summary>
/// <param name="key">The string to use as the key of the element to add.</param>
/// <param name="value">The <see cref="Policy"/> to store in the registry.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="ArgumentException">A Policy with same <paramref name="key"/> already exists.</exception>
public void Add(string key, Policy value) =>
_registry.Add(key, value);

/// <summary>
/// Removes all keys and policies from registry.
/// </summary>
public void Clear() =>
_registry.Clear();

/// <summary>
/// Determines whether the specified <paramref name="key"/> exists.
/// </summary>
/// <param name="key">The key to locate in the registry.</param>
/// <returns>True if <paramref name="key"/> exists otherwise false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
public bool ContainsKey(string key) =>
_registry.ContainsKey(key);

/// <summary>
/// Removes the stored <see cref="Policy"/> under specified <paramref name="key"/> from the registry.
/// </summary>
/// <param name="key">The <paramref name="key"/> of the policy to remove.</param>
/// <returns>True if <see cref="Policy"/> is successfully removed. Otherwise false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
public bool Remove(string key) =>
_registry.Remove(key);

/// <summary>
/// Gets the <see cref="Policy"/> associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <param name="value">
/// This method returns the <see cref="Policy"/> associated with the specified <paramref name="key"/>, if the
/// key is found; otherwise null.
/// This parameter is passed uninitialized.
/// </param>
/// <returns>True if Policy exists for the provided Key. False otherwise.</returns>
public bool TryGetValue(string key, out Policy value) =>
_registry.TryGetValue(key, out value);
}
}
69 changes: 69 additions & 0 deletions src/Polly.Shared/Registry/IPolicyRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;

namespace Polly.Registry
{
/// <summary>
/// Represents a collection of <see cref="Polly.Policy"/> keyed by <typeparamref name="TKey"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
/// <typeparam name="TPolicy">The type of Policy to store. Must be derived from <see cref="Polly.Policy"/>.</typeparam>
public interface IPolicyRegistry<TKey, TPolicy> where TPolicy: Polly.Policy
{
/// <summary>
/// Gets or sets <typeparamref name="TPolicy"/> with specified Key.
/// </summary>
/// <param name="key">The key of the policy to get or set.</param>
/// <returns>The policy with specified Key.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="KeyNotFoundException">Policy with the specified <paramref name="key"/> is not found in the registry.</exception>
TPolicy this[TKey key] { get; set; }

/// <summary>
/// Total number of policies in the registry.
/// </summary>
int Count { get; }

/// <summary>
/// Adds a <typeparamref name="TPolicy"/> with the provided key to the registry.
/// </summary>
/// <param name="key">The key of the <typeparamref name="TPolicy"/> to add.</param>
/// <param name="value">The <typeparamref name="TPolicy"/> to store in the registry.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
/// <exception cref="ArgumentException">A Policy with same <paramref name="key"/> already exists.</exception>
void Add(TKey key, TPolicy value);

/// <summary>
/// Determines whether the specified <paramref name="key"/> exists.
/// </summary>
/// <param name="key">The Key to locate in the registry</param>
/// <returns>True if <paramref name="key"/> exists otherwise false</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
bool ContainsKey(TKey key);

/// <summary>
/// Removes the specified <typeparamref name="TPolicy"/> from the registry.
/// </summary>
/// <param name="key">The key of the policy to remove.</param>
/// <returns>True if <see cref="Polly.Policy"/> is successfully removed. Otherwise false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
bool Remove(TKey key);

/// <summary>
/// Gets the <typeparamref name="TPolicy"/> associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <param name="value">
/// This method returns the <typeparamref name="TPolicy"/> associated with the specified <paramref name="key"/>, if the
/// key is found; otherwise null.
/// This parameter is passed uninitialized.
/// </param>
/// <returns>True if Policy exists for the provided Key. False otherwise.</returns>
bool TryGetValue(TKey key, out TPolicy value);

/// <summary>
/// Removes all keys and policies from registry.
/// </summary>
void Clear();
}
}
1 change: 1 addition & 0 deletions src/Polly.SharedSpecs/Polly.SharedSpecs.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="$(MSBuildThisFileDirectory)PolicyContextAndKeyAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicyContextAndKeySpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PolicySpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Registry\DefaultPolicyRegistrySpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryForeverAsyncSpecs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Retry\RetryForeverSpecs.cs" />
Expand Down
Loading