Skip to content

Commit 1241a4a

Browse files
authored
Introduce Polly.Extensions project (App-vNext#1088)
1 parent 305650e commit 1241a4a

19 files changed

+593
-7
lines changed

.github/dependabot.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ updates:
1818
# Ignore the libraries which are pinned
1919
- dependency-name: "System.ComponentModel.Annotations"
2020
- dependency-name: "System.Threading.Tasks.Extensions"
21-
- dependency-name: "System.ValueTuplens"
21+
- dependency-name: "System.ValueTuple"
22+
- dependency-name: "Microsoft.Extensions.Options"
23+
- dependency-name: "Microsoft.Extensions.Logging.Abstractions"
24+
- dependency-name: "System.Diagnostics.DiagnosticSource"

build.cake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ Task("__RunMutationTests")
216216
.Does(() =>
217217
{
218218
TestProject(File("./src/Polly.Core/Polly.Core.csproj"), File("./src/Polly.Core.Tests/Polly.Core.Tests.csproj"), "Polly.Core.csproj");
219+
TestProject(File("./src/Polly.Extensions/Polly.Extensions.csproj"), File("./src/Polly.Extensions.Tests/Polly.Extensions.Tests.csproj"), "Polly.Extensions.csproj");
219220
TestProject(File("./src/Polly/Polly.csproj"), File("./src/Polly.Specs/Polly.Specs.csproj"), "Polly.csproj");
220221

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

265266
Information("Building Polly.{0}.nupkg", nugetVersion);
266267
DotNetPack(System.IO.Path.Combine(srcDir, "Polly", "Polly.csproj"), dotNetPackSettings);
268+
269+
Information("Building Polly.Extensions.{0}.nupkg", nugetVersion);
270+
DotNetPack(System.IO.Path.Combine(srcDir, "Polly.Extensions", "Polly.Extensions.csproj"), dotNetPackSettings);
267271
});
268272

269273
//////////////////////////////////////////////////////////////////////

eng/Common.targets

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</ItemGroup>
1515

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

src/Directory.Packages.props

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@
66
<PackageVersion Include="GitHubActionsTestLogger" Version="2.0.1" />
77
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1" />
88
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
9+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
910
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
1011
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
1112
<PackageVersion Include="Moq" Version="4.18.4" />
1213
<PackageVersion Include="ReportGenerator" Version="5.1.19" />
1314
<PackageVersion Include="SonarAnalyzer.CSharp" Version="8.51.0.59060" />
1415
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
1516
<PackageVersion Include="System.ComponentModel.Annotations" Version="4.5.0" />
17+
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
1618
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
1719
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
1820
<PackageVersion Include="xunit" Version="2.4.2" />
1921
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
2022
</ItemGroup>
21-
</Project>
23+
<ItemGroup Condition="$(TargetFramework) == 'net7.0'">
24+
<PackageVersion Include="Microsoft.Extensions.Options" Version="7.0.0" />
25+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
26+
</ItemGroup>
27+
<ItemGroup Condition="$(TargetFramework) == 'net6.0'">
28+
<PackageVersion Include="Microsoft.Extensions.Options" Version="6.0.0" />
29+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
30+
</ItemGroup>
31+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))">
32+
<PackageVersion Include="Microsoft.Extensions.Options" Version="2.2.0" />
33+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
34+
</ItemGroup>
35+
</Project>

src/Polly.Core.Tests/Builder/ResilienceStrategyBuilderOptionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Moq;
12
using Polly.Builder;
23
using Polly.Telemetry;
34
using Polly.Utils;
@@ -16,4 +17,25 @@ public void Ctor_EnsureDefaults()
1617
options.TimeProvider.Should().Be(TimeProvider.System);
1718
options.TelemetryFactory.Should().Be(NullResilienceTelemetryFactory.Instance);
1819
}
20+
21+
[Fact]
22+
public void Ctor_Copy_EnsureCopied()
23+
{
24+
var options = new ResilienceStrategyBuilderOptions
25+
{
26+
BuilderName = "test",
27+
TelemetryFactory = Mock.Of<ResilienceTelemetryFactory>(),
28+
TimeProvider = new FakeTimeProvider().Object
29+
};
30+
31+
options.Properties.Set(new ResiliencePropertyKey<int>("A"), 1);
32+
options.Properties.Set(new ResiliencePropertyKey<int>("B"), 2);
33+
34+
var other = new ResilienceStrategyBuilderOptions(options);
35+
36+
other.BuilderName.Should().Be("test");
37+
other.TelemetryFactory.Should().BeSameAs(options.TelemetryFactory);
38+
other.TimeProvider.Should().BeSameAs(options.TimeProvider);
39+
other.Properties.Should().BeEquivalentTo(options.Properties);
40+
}
1941
}

src/Polly.Core.Tests/Utils/TimeProviderExtensionsTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,28 @@ await TestUtils.AssertWithTimeoutAsync(async () =>
3838
}
3939
}
4040

41+
[Fact]
42+
public async Task DelayAsync_SystemSynchronous_Ok()
43+
{
44+
var delay = TimeSpan.FromMilliseconds(5);
45+
var context = ResilienceContext.Get();
46+
context.Initialize<VoidResult>(isSynchronous: true);
47+
48+
await TestUtils.AssertWithTimeoutAsync(async () =>
49+
{
50+
var watch = Stopwatch.StartNew();
51+
await TimeProvider.System.DelayAsync(delay, context);
52+
var elapsed = watch.Elapsed;
53+
elapsed.Should().BeGreaterThanOrEqualTo(delay);
54+
});
55+
}
56+
4157
[InlineData(false, false)]
4258
[InlineData(false, true)]
4359
[InlineData(true, false)]
4460
[InlineData(true, true)]
4561
[Theory]
46-
public async Task DelayAsync_CancellationRequestedbefore_Throws(bool synchronous, bool mocked)
62+
public async Task DelayAsync_CancellationRequestedBefore_Throws(bool synchronous, bool mocked)
4763
{
4864
using var tcs = new CancellationTokenSource();
4965
tcs.Cancel();

src/Polly.Core/Builder/ResilienceStrategyBuilderOptions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.ComponentModel;
12
using System.ComponentModel.DataAnnotations;
23
using Polly.Telemetry;
34

@@ -8,6 +9,34 @@ namespace Polly.Builder;
89
/// </summary>
910
public class ResilienceStrategyBuilderOptions
1011
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="ResilienceStrategyBuilderOptions"/> class.
14+
/// </summary>
15+
public ResilienceStrategyBuilderOptions()
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ResilienceStrategyBuilderOptions"/> class.
21+
/// </summary>
22+
/// <param name="other">The options to copy the values from.</param>
23+
[EditorBrowsable(EditorBrowsableState.Never)]
24+
public ResilienceStrategyBuilderOptions(ResilienceStrategyBuilderOptions other)
25+
{
26+
Guard.NotNull(other);
27+
28+
BuilderName = other.BuilderName;
29+
TelemetryFactory = other.TelemetryFactory;
30+
TimeProvider = other.TimeProvider;
31+
32+
IDictionary<string, object?> props = Properties;
33+
34+
foreach (KeyValuePair<string, object?> pair in other.Properties)
35+
{
36+
props.Add(pair.Key, pair.Value);
37+
}
38+
}
39+
1140
/// <summary>
1241
/// Gets or sets the name of the builder.
1342
/// </summary>

src/Polly.Core/Polly.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0;net6.0;netstandard2.0;net472;net461</TargetFrameworks>
4+
<TargetFrameworks>net7.0;net6.0;netstandard2.0;net472;net462</TargetFrameworks>
55
<AssemblyTitle>Polly.Core</AssemblyTitle>
66
<RootNamespace>Polly</RootNamespace>
77
<Nullable>enable</Nullable>
@@ -11,6 +11,7 @@
1111
<SkipPollyUsings>true</SkipPollyUsings>
1212
<MutationScore>100</MutationScore>
1313
<LegacySupport>true</LegacySupport>
14+
<InjectSharedSources>true</InjectSharedSources>
1415
</PropertyGroup>
1516

1617
<ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Polly.Extensions.DependencyInjection;
2+
3+
namespace Polly.Extensions.Tests.DependencyInjection;
4+
5+
public class PollyDependencyInjectionKeysTests
6+
{
7+
[Fact]
8+
public void ServiceProvider_Ok()
9+
{
10+
PollyDependencyInjectionKeys.ServiceProvider.Key.Should().Be("Polly.DependencyInjection.ServiceProvider");
11+
}
12+
}

0 commit comments

Comments
 (0)