Skip to content

Commit

Permalink
Fix unstable tests (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Apr 12, 2023
1 parent e891364 commit 97c2307
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var configuration = Argument<string>("configuration", "Release");
//////////////////////////////////////////////////////////////////////

#Tool "xunit.runner.console&version=2.4.2"
#Tool "dotnet-stryker&version=3.7.0"
#Tool "dotnet-stryker&version=3.7.1"

//////////////////////////////////////////////////////////////////////
// EXTERNAL NUGET LIBRARIES
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
Expand Up @@ -15,14 +15,12 @@ private static IEnumerable<ExecuteParameters> ExecuteAsTaskAsync_EnsureCorrectBe
{
Caption = "ExecuteAsTaskAsync_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.ExecuteTaskAsync(async t => { t.Should().Be(CancellationToken); }, CancellationToken))
{
Caption = "ExecuteAsTaskAsync_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.ExecuteTaskAsync(async (_, s) => { s.Should().Be("dummy-state"); }, ResilienceContext.Get(), "dummy-state"))
Expand All @@ -45,8 +43,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

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
Expand Up @@ -17,14 +17,12 @@ private static IEnumerable<ExecuteParameters> ExecuteAsTaskAsyncT_EnsureCorrectB
{
Caption = "ExecuteAsTaskAsyncT_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.ExecuteTaskAsync(async t => { t.Should().Be(CancellationToken); return result; }, CancellationToken), result)
{
Caption = "ExecuteAsTaskAsyncT_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.ExecuteTaskAsync(async (_, s) => { s.Should().Be("dummy-state"); return result; }, ResilienceContext.Get(), "dummy-state"), result)
Expand All @@ -48,8 +46,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ private static IEnumerable<ExecuteParameters> ExecuteAsync_EnsureCorrectBehavior
{
Caption = "ExecuteAsync_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.ExecuteValueTaskAsync(async t => { t.Should().Be(CancellationToken); }, CancellationToken))
{
Caption = "ExecuteAsync_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.ExecuteValueTaskAsync(async (_, s) => { s.Should().Be("dummy-state"); }, ResilienceContext.Get(), "dummy-state"))
Expand All @@ -45,8 +43,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ private static IEnumerable<ExecuteParameters> ExecuteAsyncT_EnsureCorrectBehavio
{
Caption = "ExecuteAsyncT_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.ExecuteValueTaskAsync(async t => { t.Should().Be(CancellationToken); return result; }, CancellationToken), result)
{
Caption = "ExecuteAsyncT_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.ExecuteValueTaskAsync(async (_, s) => { s.Should().Be("dummy-state"); return result; }, ResilienceContext.Get(), "dummy-state"), result)
Expand All @@ -48,8 +46,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core.Tests/ResilienceStrategyTests.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ private static IEnumerable<ExecuteParameters> Execute_EnsureCorrectBehavior_Exec
{
Caption = "Execute_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.Execute(t => { t.Should().Be(CancellationToken); }, CancellationToken))
{
Caption = "Execute_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters(r => r.Execute((_, s) => { s.Should().Be("dummy-state"); }, ResilienceContext.Get(), "dummy-state"))
Expand All @@ -43,8 +41,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

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
Expand Up @@ -15,14 +15,12 @@ private static IEnumerable<ExecuteParameters> ExecuteT_EnsureCorrectBehavior_Exe
{
Caption = "ExecuteT_NoCancellation",
AssertContext = AssertResilienceContext,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.Execute(t => { t.Should().Be(CancellationToken); return result; }, CancellationToken), result)
{
Caption = "ExecuteT_Cancellation",
AssertContext = AssertResilienceContextAndToken,
AssertContextAfter = AssertContextNotInitialized,
};

yield return new ExecuteParameters<long>(r => r.Execute((_, s) => { s.Should().Be("dummy-state"); return result; }, ResilienceContext.Get(), "dummy-state"), result)
Expand All @@ -46,8 +44,6 @@ static void AssertResilienceContextAndToken(ResilienceContext context)
context.CancellationToken.Should().Be(CancellationToken);
}

static void AssertContextNotInitialized(ResilienceContext context) => context.IsInitialized.Should().BeFalse();

static void AssertContextInitialized(ResilienceContext context) => context.IsInitialized.Should().BeTrue();
}

Expand Down
18 changes: 18 additions & 0 deletions src/Polly.Core.Tests/Utils/TimeProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ await TestUtils.AssertWithTimeoutAsync(async () =>
});
}

[Fact]
public async Task DelayAsync_SystemSynchronousWhenCancelled_Ok()
{
using var cts = new CancellationTokenSource(5);
var delay = TimeSpan.FromMilliseconds(10);
var context = ResilienceContext.Get();
context.Initialize<VoidResult>(isSynchronous: true);
context.CancellationToken = cts.Token;

await TestUtils.AssertWithTimeoutAsync(async () =>
{
await TimeProvider.System
.Invoking(p => p.DelayAsync(delay, context))
.Should()
.ThrowAsync<OperationCanceledException>();
});
}

[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class RateLimiterResilienceStrategyBuilderExtensionsTests
};

[MemberData(nameof(Data))]
[Theory]
[Theory(Skip = "https://github.com/stryker-mutator/stryker-net/issues/2144")]
public void AddRateLimiter_Extensions_Ok(Action<ResilienceStrategyBuilder> configure)
{
var builder = new ResilienceStrategyBuilder();
Expand All @@ -66,6 +66,19 @@ public void AddRateLimiter_Extensions_Ok(Action<ResilienceStrategyBuilder> confi
builder.Build().Should().BeOfType<RateLimiterResilienceStrategy>();
}

[Fact]
public void AddRateLimiter_AllExtensions_Ok()
{
foreach (var configure in Data.Select(v => v[0]).Cast<Action<ResilienceStrategyBuilder>>())
{
var builder = new ResilienceStrategyBuilder();

configure(builder);

builder.Build().Should().BeOfType<RateLimiterResilienceStrategy>();
}
}

[Fact]
public void AddRateLimiter_InvalidOptions_Throws()
{
Expand Down

0 comments on commit 97c2307

Please sign in to comment.