Skip to content

Commit c24a3a6

Browse files
Intercept async detached func 5586 (#5588)
* repro issue: InterceptAsync seems to run func() as a detached task #5586 * resolved issue: InterceptAsync seems to run func() as a detached task #5586 * added notes: "func might not get awaited" * Fix API breaking changes * Remove null default value Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent 74f11fd commit c24a3a6

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/core/Akka.TestKit.Tests/TestEventListenerTests/AllTestForEventFilterBase.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, actionAsync: asyn
186186
});
187187
}
188188

189+
// issue: InterceptAsync seems to run func() as a detached task #5586
190+
[Fact]
191+
public async Task InterceptAsync_should_await_func()
192+
{
193+
await Assert.ThrowsAnyAsync<FalseException>(async () =>
194+
{
195+
await _testingEventFilter.ForLogLevel(LogLevel).ExpectAsync(0, async () =>
196+
{
197+
Assert.False(true);
198+
await Task.CompletedTask;
199+
}, TimeSpan.FromSeconds(.1));
200+
});
201+
}
202+
189203
[Fact]
190204
public void Messages_can_be_muted()
191205
{

src/core/Akka.TestKit/EventFilter/IEventFilterApplier.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public interface IEventFilterApplier
8787
/// <param name="expectedCount">The expected number of events</param>
8888
/// <param name="action">The action.</param>
8989
Task ExpectAsync(int expectedCount, Action action);
90-
90+
9191
/// <summary>
9292
/// Executes <paramref name="actionAsync"/> task and expects the specified number
9393
/// of events to be logged during the execution.
@@ -99,6 +99,18 @@ public interface IEventFilterApplier
9999
/// <param name="actionAsync">The async action.</param>
100100
Task ExpectAsync(int expectedCount, Func<Task> actionAsync);
101101

102+
/// <summary>
103+
/// Executes <paramref name="actionAsync"/> task and expects the specified number
104+
/// of events to be logged during the execution.
105+
/// This method fails and throws an exception if more events than expected are logged,
106+
/// or if a timeout occurs. The timeout is taken from the config value
107+
/// "akka.test.filter-leeway", see <see cref="TestKitSettings.TestEventFilterLeeway"/>.
108+
/// </summary>
109+
/// <param name="expectedCount">The expected number of events</param>
110+
/// <param name="actionAsync">The async action.</param>
111+
/// <param name="timeout"></param>
112+
Task ExpectAsync(int expectedCount, Func<Task> actionAsync, TimeSpan? timeout);
113+
102114
/// <summary>
103115
/// Executes <paramref name="action"/> and expects the specified number
104116
/// of events to be logged during the execution.
@@ -183,13 +195,14 @@ public interface IEventFilterApplier
183195
/// <param name="func">The function.</param>
184196
/// <returns>The returned value from <paramref name="func"/>.</returns>
185197
T Expect<T>(int expectedCount, Func<T> func);
186-
198+
187199
/// <summary>
188200
/// Executes <paramref name="func"/> and expects the specified number
189201
/// of events to be logged during the execution.
190202
/// This function fails and throws an exception if more events than expected are logged,
191203
/// or if a timeout occurs. The timeout is taken from the config value
192204
/// "akka.test.filter-leeway", see <see cref="TestKitSettings.TestEventFilterLeeway"/>.
205+
/// Note: <paramref name="func"/> might not get awaited.
193206
/// </summary>
194207
/// <typeparam name="T">The return value of the function</typeparam>
195208
/// <param name="expectedCount">The expected number of events</param>

src/core/Akka.TestKit/EventFilter/Internal/EventFilterApplier.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ public void Expect(int expectedCount, Action action)
9393
/// Async version of Expect
9494
/// </summary>
9595
public Task ExpectAsync(int expectedCount, Func<Task> actionAsync)
96+
=> InternalExpectAsync(actionAsync, _actorSystem, expectedCount, null);
97+
98+
/// <summary>
99+
/// Async version of Expect
100+
/// </summary>
101+
public Task ExpectAsync(int expectedCount, Func<Task> actionAsync, TimeSpan? timeout)
96102
{
97-
return InternalExpectAsync(actionAsync, _actorSystem, expectedCount, null);
103+
return InternalExpectAsync(actionAsync, _actorSystem, expectedCount, timeout);
98104
}
99105

100106
/// <summary>
@@ -198,6 +204,7 @@ public T Expect<T>(int expectedCount, TimeSpan timeout, Func<T> func)
198204

199205
/// <summary>
200206
/// Async version of Expect
207+
/// Note: <paramref name="func"/> might not get awaited.
201208
/// </summary>
202209
public async Task<T> ExpectAsync<T>(int expectedCount, TimeSpan timeout, Func<T> func)
203210
{

0 commit comments

Comments
 (0)