Skip to content

Commit 39de81a

Browse files
authored
Port Akka.Tests.Pattern tests to async/await - RetrySpec (#5831)
1 parent 6bc5caa commit 39de81a

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/core/Akka.Tests/Pattern/RetrySpec.cs

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,44 @@ namespace Akka.Tests.Pattern
1616
public class RetrySpec : AkkaSpec
1717
{
1818
[Fact]
19-
public Task Pattern_Retry_must_run_a_successful_task_immediately()
19+
public async Task Pattern_Retry_must_run_a_successful_task_immediately()
2020
{
21-
var retried = Retry(() => Task.FromResult(5), 5, TimeSpan.FromSeconds(1), Sys.Scheduler);
22-
23-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
21+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
2422
{
25-
var remaining = await retried;
23+
var remaining = await Retry(() => Task.FromResult(5), 5, TimeSpan.FromSeconds(1), Sys.Scheduler);
2624
Assert.Equal(5, remaining);
2725
});
2826
}
2927

3028
[Fact]
31-
public Task Pattern_Retry_must_run_a_successful_task_only_once()
29+
public async Task Pattern_Retry_must_run_a_successful_task_only_once()
3230
{
33-
var counter = 0;
34-
var retried = Retry(() =>
35-
{
36-
counter++;
37-
return Task.FromResult(counter);
38-
}, 5, TimeSpan.FromSeconds(1), Sys.Scheduler);
39-
40-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
31+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
4132
{
42-
var remaining = await retried;
33+
var counter = 0;
34+
var remaining = await Retry(() =>
35+
{
36+
counter++;
37+
return Task.FromResult(counter);
38+
}, 5, TimeSpan.FromSeconds(1), Sys.Scheduler);
4339
Assert.Equal(1, remaining);
4440
});
4541
}
4642

4743
[Fact]
48-
public Task Pattern_Retry_must_eventually_return_a_failure_for_a_task_that_will_never_succeed()
44+
public async Task Pattern_Retry_must_eventually_return_a_failure_for_a_task_that_will_never_succeed()
4945
{
50-
var retried = Retry(() => Task.FromException<int>(new InvalidOperationException("Mexico")), 5, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
51-
52-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
46+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
5347
{
54-
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => retried);
48+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
49+
await Retry(() => Task.FromException<int>(new InvalidOperationException("Mexico")),
50+
5, TimeSpan.FromMilliseconds(100), Sys.Scheduler));
5551
Assert.Equal("Mexico", exception.Message);
5652
});
5753
}
5854

5955
[Fact]
60-
public Task Pattern_Retry_must_return_a_success_for_a_task_that_succeeds_eventually()
56+
public async Task Pattern_Retry_must_return_a_success_for_a_task_that_succeeds_eventually()
6157
{
6258
var failCount = 0;
6359

@@ -74,17 +70,15 @@ Task<int> Attempt()
7470
}
7571
}
7672

77-
var retried = Retry(() => Attempt(), 10, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
78-
79-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
73+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
8074
{
81-
var remaining = await retried;
75+
var remaining = await Retry(Attempt, 10, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
8276
Assert.Equal(5, remaining);
8377
});
8478
}
8579

8680
[Fact]
87-
public Task Pattern_Retry_must_return_a_failure_for_a_task_that_would_have_succeeded_but_retries_were_exhausted()
81+
public async Task Pattern_Retry_must_return_a_failure_for_a_task_that_would_have_succeeded_but_retries_were_exhausted()
8882
{
8983
var failCount = 0;
9084

@@ -101,17 +95,16 @@ Task<int> Attempt()
10195
}
10296
}
10397

104-
var retried = Retry(() => Attempt(), 5, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
105-
106-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
98+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
10799
{
108-
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => retried);
100+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
101+
await Retry(Attempt, 5, TimeSpan.FromMilliseconds(100), Sys.Scheduler));
109102
Assert.Equal("6", exception.Message);
110103
});
111104
}
112105

113106
[Fact]
114-
public Task Pattern_Retry_must_return_a_failure_for_a_task_that_would_have_succeeded_but_retries_were_exhausted_with_delay_function()
107+
public async Task Pattern_Retry_must_return_a_failure_for_a_task_that_would_have_succeeded_but_retries_were_exhausted_with_delay_function()
115108
{
116109
var failCount = 0;
117110
var attemptedCount = 0;
@@ -129,22 +122,21 @@ Task<int> Attempt()
129122
}
130123
}
131124

132-
var retried = Retry(() => Attempt(), 5, attempted =>
133-
{
134-
attemptedCount = attempted;
135-
return TimeSpan.FromMilliseconds(100 + attempted);
136-
}, Sys.Scheduler);
137-
138-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
125+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
139126
{
140-
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => retried);
127+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
128+
await Retry(Attempt, 5, attempted =>
129+
{
130+
attemptedCount = attempted;
131+
return TimeSpan.FromMilliseconds(100 + attempted);
132+
}, Sys.Scheduler));
141133
Assert.Equal("6", exception.Message);
142134
Assert.Equal(5, attemptedCount);
143135
});
144136
}
145137

146138
[Fact]
147-
public Task Pattern_Retry_can_be_attempted_without_any_delay()
139+
public async Task Pattern_Retry_can_be_attempted_without_any_delay()
148140
{
149141
var failCount = 0;
150142

@@ -162,11 +154,9 @@ Task<int> Attempt()
162154
}
163155

164156
var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
165-
var retried = Retry(() => Attempt(), 999);
166-
167-
return WithinAsync(TimeSpan.FromSeconds(1), async () =>
157+
await WithinAsync(TimeSpan.FromSeconds(1), async () =>
168158
{
169-
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => retried);
159+
var exception = await Assert.ThrowsAsync<InvalidOperationException>( async () => await Retry(Attempt, 999));
170160
Assert.Equal("1000", exception.Message);
171161

172162
var elapse = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start;
@@ -175,7 +165,7 @@ Task<int> Attempt()
175165
}
176166

177167
[Fact]
178-
public Task Pattern_Retry_must_handle_thrown_exceptions_in_same_way_as_failed_task()
168+
public async Task Pattern_Retry_must_handle_thrown_exceptions_in_same_way_as_failed_task()
179169
{
180170
var failCount = 0;
181171

@@ -192,11 +182,9 @@ Task<int> Attempt()
192182
}
193183
}
194184

195-
var retried = Retry(() => Attempt(), 10, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
196-
197-
return WithinAsync(TimeSpan.FromSeconds(3), async () =>
185+
await WithinAsync(TimeSpan.FromSeconds(3), async () =>
198186
{
199-
var remaining = await retried;
187+
var remaining = await Retry(Attempt, 10, TimeSpan.FromMilliseconds(100), Sys.Scheduler);
200188
Assert.Equal(5, remaining);
201189
});
202190
}

0 commit comments

Comments
 (0)