Skip to content

Commit

Permalink
Introduce Timeout Strategy (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Mar 27, 2023
1 parent 82c3467 commit 94b309d
Show file tree
Hide file tree
Showing 56 changed files with 1,218 additions and 138 deletions.
5 changes: 5 additions & 0 deletions eng/Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>

<ItemGroup>
<Using Include="FluentAssertions"/>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" />
Expand Down
2 changes: 1 addition & 1 deletion eng/analyzers/Library.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2937,7 +2937,7 @@ dotnet_diagnostic.S1659.severity = warning
# Category : Minor Code Smell
# Help Link: https://rules.sonarsource.com/csharp/RSPEC-1694
# Tags : C#, MainSourceScope
dotnet_diagnostic.S1694.severity = warning
dotnet_diagnostic.S1694.severity = suggestion

# Title : NullReferenceException should not be caught
# Category : Major Code Smell
Expand Down
4 changes: 2 additions & 2 deletions eng/analyzers/Test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ dotnet_diagnostic.CA1062.severity = none
# Category : Design
# Help Link: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1063
# Tags : PortedFromFxCop, Telemetry, EnabledRuleInAggressiveMode
dotnet_diagnostic.CA1063.severity = warning
dotnet_diagnostic.CA1063.severity = none

# Title : Exceptions should be public
# Category : Design
Expand Down Expand Up @@ -713,7 +713,7 @@ dotnet_diagnostic.CA1815.severity = none
# Category : Usage
# Help Link: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1816
# Tags : PortedFromFxCop, Telemetry, EnabledRuleInAggressiveMode
dotnet_diagnostic.CA1816.severity = warning
dotnet_diagnostic.CA1816.severity = none

# Title : Properties should not return arrays
# Category : Performance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using FluentAssertions;
using Polly.Builder;
using Polly.Telemetry;
using Xunit;

namespace Polly.Core.Tests.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using FluentAssertions;
using Polly.Builder;
using Polly.Telemetry;
using Polly.Utils;
using Xunit;

namespace Polly.Core.Tests.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using Moq;
using Polly.Builder;
using Polly.Core.Tests.Utils;
using Polly.Telemetry;
using Xunit;

namespace Polly.Core.Tests.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using FluentAssertions;
using Polly.Builder;
using Xunit;

namespace Polly.Core.Tests.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using FluentAssertions;
using Polly.Builder;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests.Builder;

Expand Down
23 changes: 23 additions & 0 deletions src/Polly.Core.Tests/ExecutionRejectedExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Polly.Core.Tests.Timeout;

public class ExecutionRejectedExceptionTests
{
[Fact]
public void Ctor_Ok()
{
new CustomException().Message.Should().Be("Exception of type 'Polly.Core.Tests.Timeout.ExecutionRejectedExceptionTests+CustomException' was thrown.");
new CustomException("Dummy").Message.Should().Be("Dummy");
}

private class CustomException : ExecutionRejectedException
{
public CustomException()
{
}

public CustomException(string message)
: base(message)
{
}
}
}
3 changes: 0 additions & 3 deletions src/Polly.Core.Tests/NullResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using FluentAssertions;
using Xunit;

namespace Polly.Core.Tests;

public class NullResilienceStrategyTests
Expand Down
6 changes: 5 additions & 1 deletion src/Polly.Core.Tests/Polly.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
<Nullable>enable</Nullable>
<SkipPollyUsings>true</SkipPollyUsings>
<Threshold>100</Threshold>
<NoWarn>$(NoWarn);SA1600</NoWarn>
<NoWarn>$(NoWarn);SA1600;SA1204</NoWarn>
<Include>[Polly.Core]*</Include>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Polly.Core\Polly.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Polly.Core.Tests.Utils"/>
</ItemGroup>
</Project>
3 changes: 0 additions & 3 deletions src/Polly.Core.Tests/ResilienceContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using FluentAssertions;
using Xunit;

namespace Polly.Core.Tests;

public class ResilienceContextTests
Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResiliencePropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using FluentAssertions;
using Xunit;

namespace Polly.Core.Tests;

public class ResiliencePropertiesTests
Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResiliencePropertyKeyTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using FluentAssertions;
using Xunit;

namespace Polly.Core.Tests;

public class ResiliencePropertyKeyTests
Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Async.Task.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Async.TaskT.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
Expand Down
5 changes: 0 additions & 5 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Sync.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System.Threading.Tasks;
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

public partial class ResilienceStrategyTests
Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResilienceStrategyTests.SyncT.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using FluentAssertions;
using Polly.Core.Tests.Utils;
using Xunit;

namespace Polly.Core.Tests;

public partial class ResilienceStrategyTests
Expand Down
2 changes: 0 additions & 2 deletions src/Polly.Core.Tests/ResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using FluentAssertions;

namespace Polly.Core.Tests;

public partial class ResilienceStrategyTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using FluentAssertions;
using Polly.Telemetry;
using Xunit;

namespace Polly.Core.Tests.Telemetry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using FluentAssertions;
using Polly.Telemetry;
using Xunit;

namespace Polly.Core.Tests.Telemetry;

Expand Down
64 changes: 64 additions & 0 deletions src/Polly.Core.Tests/Timeout/OnTimeoutEventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Polly.Timeout;

namespace Polly.Core.Tests.Timeout;

public class OnTimeoutEventTests
{
[Fact]
public async Task Add_EnsureOrdering()
{
var ev = new OnTimeoutEvent();
List<int> raisedEvents = new List<int>();

ev.Add(() => raisedEvents.Add(1));
ev.Add(args => raisedEvents.Add(2));
ev.Add(args => { raisedEvents.Add(3); return default; });

var handler = ev.CreateHandler()!;

await handler(TimeoutTestUtils.OnTimeoutArguments());

raisedEvents.Should().HaveCount(3);
raisedEvents.Should().BeInAscendingOrder();
}

[Fact]
public void Add_EnsureValidation()
{
var ev = new OnTimeoutEvent();

Assert.Throws<ArgumentNullException>(() => ev.Add((Action)null!));
Assert.Throws<ArgumentNullException>(() => ev.Add((Action<OnTimeoutArguments>)null!));
Assert.Throws<ArgumentNullException>(() => ev.Add(null!));
}

[InlineData(1)]
[InlineData(2)]
[InlineData(100)]
[Theory]
public async Task CreateHandler_Ok(int count)
{
var ev = new OnTimeoutEvent();
var events = new List<int>();

for (int i = 0; i < count; i++)
{
ev.Add(() => events.Add(i));
}

await ev.CreateHandler()!(TimeoutTestUtils.OnTimeoutArguments());

events.Should().HaveCount(count);
events.Should().BeInAscendingOrder();
}

[Fact]
public void CreateHandler_NoEvents_Null()
{
var ev = new OnTimeoutEvent();

var handler = ev.CreateHandler();

handler.Should().BeNull();
}
}
13 changes: 13 additions & 0 deletions src/Polly.Core.Tests/Timeout/TimeoutAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
using Polly.Timeout;

namespace Polly.Core.Tests.Timeout;

public class TimeoutAttributeTests
{
[Fact]
public void IsValid_Object_True()
{
new TimeoutAttribute().IsValid(new object()).Should().BeTrue();
}
}
13 changes: 13 additions & 0 deletions src/Polly.Core.Tests/Timeout/TimeoutConstantsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Polly.Timeout;

namespace Polly.Core.Tests.Timeout;

public class TimeoutConstantsTests
{
[Fact]
public void EnsureDefaultValues()
{
TimeoutConstants.OnTimeoutEvent.Should().Be("OnTimeout");
TimeoutConstants.StrategyType.Should().Be("Timeout");
}
}
49 changes: 49 additions & 0 deletions src/Polly.Core.Tests/Timeout/TimeoutGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Polly.Timeout;

namespace Polly.Core.Tests.Timeout;

public class TimeoutGeneratorTests
{
[Fact]
public void Default_EnsureCorrectValue()
{
var generator = new TimeoutGenerator();

generator.CreateHandler().Should().BeNull();
}

[Fact]
public async Task SetTimeout_Callback_Ok()
{
var generator = new TimeoutGenerator();
var delay = TimeSpan.FromSeconds(1);

generator.SetTimeout(args => delay);

var actualDelay = await generator.CreateHandler()!(TimeoutTestUtils.TimeoutGeneratorArguments());

actualDelay.Should().Be(delay);
}

[Fact]
public async Task SetTimeout_AsyncCallback_Ok()
{
var generator = new TimeoutGenerator();
var delay = TimeSpan.FromSeconds(1);

generator.SetTimeout(args => new ValueTask<TimeSpan>(delay));

var actualDelay = await generator.CreateHandler()!(TimeoutTestUtils.TimeoutGeneratorArguments());

actualDelay.Should().Be(delay);
}

[Fact]
public void ArgValidation_EnsureThrows()
{
var generator = new TimeoutGenerator();

Assert.Throws<ArgumentNullException>(() => generator.SetTimeout((Func<TimeoutGeneratorArguments, TimeSpan>)null!));
Assert.Throws<ArgumentNullException>(() => generator.SetTimeout((Func<TimeoutGeneratorArguments, ValueTask<TimeSpan>>)null!));
}
}
Loading

0 comments on commit 94b309d

Please sign in to comment.