Skip to content

Commit c3df1ca

Browse files
xunit v3 preparation
Takes some changes from #2440 which is blocked on support for xunit v3 in Stryker. with the aim of making #2440 a smaller change. Contributes to #2439.
1 parent ca22978 commit c3df1ca

File tree

103 files changed

+875
-581
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+875
-581
lines changed

src/Snippets/Docs/Testing.cs

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static void PipelineProviderProviderMocking()
8282
}
8383
}
8484

85+
#pragma warning disable IDE0290
8586
#region testing-resilience-pipeline-provider-usage
8687

8788
// Represents an arbitrary API that needs resilience support
@@ -124,3 +125,4 @@ public static IServiceCollection AddMyApi(this IServiceCollection services)
124125
}
125126

126127
#endregion
128+
#pragma warning restore IDE0290

test/Polly.Core.Tests/CircuitBreaker/BrokenCircuitExceptionTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Ctor_Message_RetryAfter_InnerException_Ok()
5454
exception.RetryAfter.Should().Be(TestRetryAfter);
5555
}
5656

57-
#if !NETCOREAPP
57+
#if NETFRAMEWORK
5858
[Fact]
5959
public void BinarySerialization_NonNullRetryAfter_Ok()
6060
{

test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerManualControlTests.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ public void Ctor_Isolated(bool isolated)
2929
[Theory]
3030
public async Task IsolateAsync_NotInitialized_Ok(bool closedAfter)
3131
{
32+
var cancellationToken = CancellationToken.None;
3233
var control = new CircuitBreakerManualControl();
33-
await control.IsolateAsync();
34+
await control.IsolateAsync(cancellationToken);
3435
if (closedAfter)
3536
{
36-
await control.CloseAsync();
37+
await control.CloseAsync(cancellationToken);
3738
}
3839

3940
var isolated = false;
@@ -56,7 +57,7 @@ public async Task ResetAsync_NotInitialized_Ok()
5657
var control = new CircuitBreakerManualControl();
5758

5859
await control
59-
.Invoking(c => c.CloseAsync(CancellationToken.None))
60+
.Invoking(c => c.CloseAsync())
6061
.Should()
6162
.NotThrowAsync();
6263
}
@@ -65,12 +66,13 @@ await control
6566
public async Task Initialize_Twice_Ok()
6667
{
6768
int called = 0;
69+
var cancellationToken = CancellationToken.None;
6870
var control = new CircuitBreakerManualControl();
6971
control.Initialize(_ => Task.CompletedTask, _ => Task.CompletedTask);
7072
control.Initialize(_ => { called++; return Task.CompletedTask; }, _ => { called++; return Task.CompletedTask; });
7173

72-
await control.IsolateAsync();
73-
await control.CloseAsync();
74+
await control.IsolateAsync(cancellationToken);
75+
await control.CloseAsync(cancellationToken);
7476

7577
called.Should().Be(2);
7678
}
@@ -79,23 +81,25 @@ public async Task Initialize_Twice_Ok()
7981
public async Task Initialize_DisposeRegistration_ShuldBeCancelled()
8082
{
8183
int called = 0;
84+
var cancellationToken = CancellationToken.None;
8285
var control = new CircuitBreakerManualControl();
8386
var reg = control.Initialize(_ => { called++; return Task.CompletedTask; }, _ => { called++; return Task.CompletedTask; });
8487

85-
await control.IsolateAsync();
86-
await control.CloseAsync();
88+
await control.IsolateAsync(cancellationToken);
89+
await control.CloseAsync(cancellationToken);
8790

8891
reg.Dispose();
8992

90-
await control.IsolateAsync();
91-
await control.CloseAsync();
93+
await control.IsolateAsync(cancellationToken);
94+
await control.CloseAsync(cancellationToken);
9295

9396
called.Should().Be(2);
9497
}
9598

9699
[Fact]
97100
public async Task Initialize_Ok()
98101
{
102+
var cancellationToken = CancellationToken.None;
99103
var control = new CircuitBreakerManualControl();
100104
var isolateCalled = false;
101105
var resetCalled = false;
@@ -116,8 +120,8 @@ public async Task Initialize_Ok()
116120
return Task.CompletedTask;
117121
});
118122

119-
await control.IsolateAsync(CancellationToken.None);
120-
await control.CloseAsync(CancellationToken.None);
123+
await control.IsolateAsync(cancellationToken);
124+
await control.CloseAsync(cancellationToken);
121125

122126
isolateCalled.Should().BeTrue();
123127
resetCalled.Should().BeTrue();

test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerPredicateArgumentsTests.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Polly.Core.Tests.CircuitBreaker;
44

5-
public class CircuitBreakerPredicateArgumentsTests
5+
public static class CircuitBreakerPredicateArgumentsTests
66
{
77
[Fact]
8-
public void Ctor_Ok()
8+
public static void Ctor_Ok()
99
{
10-
var args = new CircuitBreakerPredicateArguments<int>(ResilienceContextPool.Shared.Get(), Outcome.FromResult(1));
10+
var args = new CircuitBreakerPredicateArguments<int>(
11+
ResilienceContextPool.Shared.Get(CancellationToken.None),
12+
Outcome.FromResult(1));
1113

1214
args.Context.Should().NotBeNull();
1315
args.Outcome.Result.Should().Be(1);

test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderTests.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void AddCircuitBreaker_Validation()
6464
[Fact]
6565
public void AddCircuitBreaker_IntegrationTest()
6666
{
67+
var cancellationToken = CancellationToken.None;
6768
int opened = 0;
6869
int closed = 0;
6970
int halfOpened = 0;
@@ -88,36 +89,36 @@ public void AddCircuitBreaker_IntegrationTest()
8889

8990
for (int i = 0; i < 10; i++)
9091
{
91-
strategy.Execute(_ => -1);
92+
strategy.Execute(_ => -1, cancellationToken);
9293
}
9394

9495
// Circuit opened
9596
opened.Should().Be(1);
9697
halfOpened.Should().Be(0);
9798
closed.Should().Be(0);
98-
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
99+
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
99100
exception.RetryAfter.Should().Be(breakDuration);
100101

101102
// Circuit still open after some time
102103
timeProvider.Advance(halfBreakDuration);
103104
opened.Should().Be(1);
104105
halfOpened.Should().Be(0);
105106
closed.Should().Be(0);
106-
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
107+
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
107108
exception.RetryAfter.Should().Be(halfBreakDuration);
108109

109110
// Circuit Half Opened
110111
timeProvider.Advance(halfBreakDuration);
111-
strategy.Execute(_ => -1);
112-
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
112+
strategy.Execute(_ => -1, cancellationToken);
113+
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
113114
opened.Should().Be(2);
114115
halfOpened.Should().Be(1);
115116
closed.Should().Be(0);
116117
exception.RetryAfter.Should().Be(breakDuration);
117118

118119
// Now close it
119120
timeProvider.Advance(breakDuration);
120-
strategy.Execute(_ => 0);
121+
strategy.Execute(_ => 0, cancellationToken);
121122
opened.Should().Be(2);
122123
halfOpened.Should().Be(2);
123124
closed.Should().Be(1);
@@ -126,6 +127,7 @@ public void AddCircuitBreaker_IntegrationTest()
126127
[Fact]
127128
public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
128129
{
130+
var cancellationToken = CancellationToken.None;
129131
int opened = 0;
130132
int closed = 0;
131133
int halfOpened = 0;
@@ -151,36 +153,36 @@ public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
151153

152154
for (int i = 0; i < 10; i++)
153155
{
154-
strategy.Execute(_ => -1);
156+
strategy.Execute(_ => -1, cancellationToken);
155157
}
156158

157159
// Circuit opened
158160
opened.Should().Be(1);
159161
halfOpened.Should().Be(0);
160162
closed.Should().Be(0);
161-
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
163+
BrokenCircuitException exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
162164
exception.RetryAfter.Should().Be(breakDuration);
163165

164166
// Circuit still open after some time
165167
timeProvider.Advance(halfBreakDuration);
166168
opened.Should().Be(1);
167169
halfOpened.Should().Be(0);
168170
closed.Should().Be(0);
169-
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
171+
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
170172
exception.RetryAfter.Should().Be(halfBreakDuration);
171173

172174
// Circuit Half Opened
173175
timeProvider.Advance(halfBreakDuration);
174-
strategy.Execute(_ => -1);
175-
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
176+
strategy.Execute(_ => -1, cancellationToken);
177+
exception = Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0, cancellationToken));
176178
opened.Should().Be(2);
177179
halfOpened.Should().Be(1);
178180
closed.Should().Be(0);
179181
exception.RetryAfter.Should().Be(breakDuration);
180182

181183
// Now close it
182184
timeProvider.Advance(breakDuration);
183-
strategy.Execute(_ => 0);
185+
strategy.Execute(_ => 0, cancellationToken);
184186
opened.Should().Be(2);
185187
halfOpened.Should().Be(2);
186188
closed.Should().Be(1);
@@ -189,8 +191,9 @@ public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
189191
[Fact]
190192
public async Task AddCircuitBreakers_WithIsolatedManualControl_ShouldBeIsolated()
191193
{
194+
var cancellationToken = CancellationToken.None;
192195
var manualControl = new CircuitBreakerManualControl();
193-
await manualControl.IsolateAsync();
196+
await manualControl.IsolateAsync(cancellationToken);
194197

195198
var strategy1 = new ResiliencePipelineBuilder()
196199
.AddCircuitBreaker(new() { ManualControl = manualControl })
@@ -203,7 +206,7 @@ public async Task AddCircuitBreakers_WithIsolatedManualControl_ShouldBeIsolated(
203206
strategy1.Invoking(s => s.Execute(() => { })).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);
204207
strategy2.Invoking(s => s.Execute(() => { })).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);
205208

206-
await manualControl.CloseAsync();
209+
await manualControl.CloseAsync(cancellationToken);
207210

208211
strategy1.Execute(() => { });
209212
strategy2.Execute(() => { });

test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResilienceStrategyTests.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public CircuitBreakerResilienceStrategyTests()
3030
null);
3131
}
3232

33+
private static CancellationToken CancellationToken => CancellationToken.None;
34+
3335
[Fact]
3436
public void Ctor_Ok() =>
3537
this.Invoking(_ => Create()).Should().NotThrow();
@@ -52,10 +54,10 @@ public async Task Ctor_ManualControl_EnsureAttached()
5254
_options.ManualControl = new CircuitBreakerManualControl();
5355
var strategy = Create();
5456

55-
await _options.ManualControl.IsolateAsync(CancellationToken.None);
57+
await _options.ManualControl.IsolateAsync(CancellationToken);
5658
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<IsolatedCircuitException>().Where(e => e.RetryAfter == null);
5759

58-
await _options.ManualControl.CloseAsync(CancellationToken.None);
60+
await _options.ManualControl.CloseAsync(CancellationToken);
5961

6062
strategy.Invoking(s => s.Execute(_ => 0)).Should().NotThrow();
6163

@@ -73,7 +75,7 @@ public void Execute_HandledResult_OnFailureCalled()
7375
_behavior.When(v => v.OnActionFailure(CircuitState.Closed, out Arg.Any<bool>()))
7476
.Do(x => x[1] = shouldBreak);
7577

76-
strategy.Execute(_ => -1).Should().Be(-1);
78+
strategy.Execute(_ => -1, CancellationToken).Should().Be(-1);
7779

7880
_behavior.Received().OnActionFailure(CircuitState.Closed, out Arg.Any<bool>());
7981
}
@@ -84,7 +86,7 @@ public void Execute_UnhandledResult_OnActionSuccess()
8486
_options.ShouldHandle = args => new ValueTask<bool>(args.Outcome.Result is -1);
8587
var strategy = Create();
8688

87-
strategy.Execute(_ => 0).Should().Be(0);
89+
strategy.Execute(_ => 0, CancellationToken).Should().Be(0);
8890

8991
_behavior.Received(1).OnActionSuccess(CircuitState.Closed);
9092
}

0 commit comments

Comments
 (0)