Skip to content

Commit

Permalink
Introduce Polly.Extensions project (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Apr 3, 2023
1 parent 305650e commit 1241a4a
Show file tree
Hide file tree
Showing 19 changed files with 593 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ updates:
# Ignore the libraries which are pinned
- dependency-name: "System.ComponentModel.Annotations"
- dependency-name: "System.Threading.Tasks.Extensions"
- dependency-name: "System.ValueTuplens"
- dependency-name: "System.ValueTuple"
- dependency-name: "Microsoft.Extensions.Options"
- dependency-name: "Microsoft.Extensions.Logging.Abstractions"
- dependency-name: "System.Diagnostics.DiagnosticSource"
4 changes: 4 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ Task("__RunMutationTests")
.Does(() =>
{
TestProject(File("./src/Polly.Core/Polly.Core.csproj"), File("./src/Polly.Core.Tests/Polly.Core.Tests.csproj"), "Polly.Core.csproj");
TestProject(File("./src/Polly.Extensions/Polly.Extensions.csproj"), File("./src/Polly.Extensions.Tests/Polly.Extensions.Tests.csproj"), "Polly.Extensions.csproj");
TestProject(File("./src/Polly/Polly.csproj"), File("./src/Polly.Specs/Polly.Specs.csproj"), "Polly.csproj");

void TestProject(FilePath proj, FilePath testProj, string project)
Expand Down Expand Up @@ -264,6 +265,9 @@ Task("__CreateSignedNuGetPackages")

Information("Building Polly.{0}.nupkg", nugetVersion);
DotNetPack(System.IO.Path.Combine(srcDir, "Polly", "Polly.csproj"), dotNetPackSettings);

Information("Building Polly.Extensions.{0}.nupkg", nugetVersion);
DotNetPack(System.IO.Path.Combine(srcDir, "Polly.Extensions", "Polly.Extensions.csproj"), dotNetPackSettings);
});

//////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions eng/Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
</ItemGroup>

<ItemGroup Condition="'$(LegacySupport)' == 'true' AND !$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))">
<Compile Include="$(MSBuildThisFileDirectory)\..\src\LegacySupport\*.cs" LinkBase="LegacySupport" />
<Compile Include="$(MSBuildThisFileDirectory)..\src\LegacySupport\*.cs" LinkBase="LegacySupport" />
</ItemGroup>

</Project>
16 changes: 15 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@
<PackageVersion Include="GitHubActionsTestLogger" Version="2.0.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="ReportGenerator" Version="5.1.19" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="8.51.0.59060" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="4.5.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>
</Project>
<ItemGroup Condition="$(TargetFramework) == 'net7.0'">
<PackageVersion Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'net6.0'">
<PackageVersion Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))">
<PackageVersion Include="Microsoft.Extensions.Options" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Moq;
using Polly.Builder;
using Polly.Telemetry;
using Polly.Utils;
Expand All @@ -16,4 +17,25 @@ public void Ctor_EnsureDefaults()
options.TimeProvider.Should().Be(TimeProvider.System);
options.TelemetryFactory.Should().Be(NullResilienceTelemetryFactory.Instance);
}

[Fact]
public void Ctor_Copy_EnsureCopied()
{
var options = new ResilienceStrategyBuilderOptions
{
BuilderName = "test",
TelemetryFactory = Mock.Of<ResilienceTelemetryFactory>(),
TimeProvider = new FakeTimeProvider().Object
};

options.Properties.Set(new ResiliencePropertyKey<int>("A"), 1);
options.Properties.Set(new ResiliencePropertyKey<int>("B"), 2);

var other = new ResilienceStrategyBuilderOptions(options);

other.BuilderName.Should().Be("test");
other.TelemetryFactory.Should().BeSameAs(options.TelemetryFactory);
other.TimeProvider.Should().BeSameAs(options.TimeProvider);
other.Properties.Should().BeEquivalentTo(options.Properties);
}
}
18 changes: 17 additions & 1 deletion src/Polly.Core.Tests/Utils/TimeProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,28 @@ await TestUtils.AssertWithTimeoutAsync(async () =>
}
}

[Fact]
public async Task DelayAsync_SystemSynchronous_Ok()
{
var delay = TimeSpan.FromMilliseconds(5);
var context = ResilienceContext.Get();
context.Initialize<VoidResult>(isSynchronous: true);

await TestUtils.AssertWithTimeoutAsync(async () =>
{
var watch = Stopwatch.StartNew();
await TimeProvider.System.DelayAsync(delay, context);
var elapsed = watch.Elapsed;
elapsed.Should().BeGreaterThanOrEqualTo(delay);
});
}

[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
[Theory]
public async Task DelayAsync_CancellationRequestedbefore_Throws(bool synchronous, bool mocked)
public async Task DelayAsync_CancellationRequestedBefore_Throws(bool synchronous, bool mocked)
{
using var tcs = new CancellationTokenSource();
tcs.Cancel();
Expand Down
29 changes: 29 additions & 0 deletions src/Polly.Core/Builder/ResilienceStrategyBuilderOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Polly.Telemetry;

Expand All @@ -8,6 +9,34 @@ namespace Polly.Builder;
/// </summary>
public class ResilienceStrategyBuilderOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="ResilienceStrategyBuilderOptions"/> class.
/// </summary>
public ResilienceStrategyBuilderOptions()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ResilienceStrategyBuilderOptions"/> class.
/// </summary>
/// <param name="other">The options to copy the values from.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public ResilienceStrategyBuilderOptions(ResilienceStrategyBuilderOptions other)
{
Guard.NotNull(other);

BuilderName = other.BuilderName;
TelemetryFactory = other.TelemetryFactory;
TimeProvider = other.TimeProvider;

IDictionary<string, object?> props = Properties;

foreach (KeyValuePair<string, object?> pair in other.Properties)
{
props.Add(pair.Key, pair.Value);
}
}

/// <summary>
/// Gets or sets the name of the builder.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Polly.Core/Polly.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0;netstandard2.0;net472;net461</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0;netstandard2.0;net472;net462</TargetFrameworks>
<AssemblyTitle>Polly.Core</AssemblyTitle>
<RootNamespace>Polly</RootNamespace>
<Nullable>enable</Nullable>
Expand All @@ -11,6 +11,7 @@
<SkipPollyUsings>true</SkipPollyUsings>
<MutationScore>100</MutationScore>
<LegacySupport>true</LegacySupport>
<InjectSharedSources>true</InjectSharedSources>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Polly.Extensions.DependencyInjection;

namespace Polly.Extensions.Tests.DependencyInjection;

public class PollyDependencyInjectionKeysTests
{
[Fact]
public void ServiceProvider_Ok()
{
PollyDependencyInjectionKeys.ServiceProvider.Key.Should().Be("Polly.DependencyInjection.ServiceProvider");
}
}
Loading

0 comments on commit 1241a4a

Please sign in to comment.