Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Async TestKit] Convert Akka.Stream.TestKit to async - TestSubscriber #5911

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Convert Akka.Stream.TestKit to async - TestSubscriber
  • Loading branch information
Arkatufus committed May 3, 2022
commit 396cee6fe0be5d63d44d5caec5485550f53d404b
43 changes: 38 additions & 5 deletions src/core/Akka.Streams.TestKit/TestSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,31 +439,64 @@ public IAsyncEnumerable<TOther> ReceiveWithinAsync<TOther>(
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="execute"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public TOther Within<TOther>(TimeSpan min, TimeSpan max, Func<TOther> execute) => TestProbe.Within(min, max, execute);
public TOther Within<TOther>(TimeSpan min, TimeSpan max, Func<TOther> execute, CancellationToken cancellationToken = default) =>
TestProbe.Within(min, max, execute, cancellationToken: cancellationToken);

public async Task<TOther> WithinAsync<TOther>(TimeSpan min, TimeSpan max, Func<TOther> execute, CancellationToken cancellationToken = default) =>
await TestProbe.WithinAsync(min, max, execute, cancellationToken: cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Sane as calling Within(TimeSpan.Zero, max, function).
/// </summary>
public TOther Within<TOther>(TimeSpan max, Func<TOther> execute, CancellationToken cancellationToken = default) =>
TestProbe.Within(max, execute, cancellationToken: cancellationToken);

/// <summary>
/// Sane as calling Within(TimeSpan.Zero, max, function).
/// </summary>
public TOther Within<TOther>(TimeSpan max, Func<TOther> execute) => TestProbe.Within(max, execute);
public async Task<TOther> WithinAsync<TOther>(TimeSpan max, Func<TOther> execute, CancellationToken cancellationToken = default) =>
await TestProbe.WithinAsync(max, execute, cancellationToken: cancellationToken)
.ConfigureAwait(false);

public async Task WithinAsync(
TimeSpan max,
Func<Task> actionAsync,
TimeSpan? epsilonValue = null,
CancellationToken cancellationToken = default)
=> await TestProbe.WithinAsync(max, actionAsync, epsilonValue, cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Attempt to drain the stream into a strict collection (by requesting long.MaxValue elements).
/// </summary>
/// <remarks>
/// Use with caution: Be warned that this may not be a good idea if the stream is infinite or its elements are very large!
/// </remarks>
public IList<T> ToStrict(TimeSpan atMost, CancellationToken cancellationToken = default)
=> ToStrictAsync(atMost, cancellationToken)
.ConfigureAwait(false).GetAwaiter().GetResult();

/// <summary>
/// Attempt to drain the stream into a strict collection (by requesting long.MaxValue elements).
/// </summary>
/// <remarks>
/// Use with caution: Be warned that this may not be a good idea if the stream is infinite or its elements are very large!
/// </remarks>
public IList<T> ToStrict(TimeSpan atMost)
public async Task<IList<T>> ToStrictAsync(TimeSpan atMost, CancellationToken cancellationToken = default)
{
var deadline = DateTime.UtcNow + atMost;
// if no subscription was obtained yet, we expect it
if (_subscription == null) ExpectSubscription();
if (_subscription == null)
await ExpectSubscriptionAsync(cancellationToken);
_subscription.Request(long.MaxValue);

var result = new List<T>();
while (true)
{
var e = ExpectEvent(TimeSpan.FromTicks(Math.Max(deadline.Ticks - DateTime.UtcNow.Ticks, 0)));
var e = await ExpectEventAsync(TimeSpan.FromTicks(Math.Max(deadline.Ticks - DateTime.UtcNow.Ticks, 0)), cancellationToken);
if (e is OnError error)
throw new ArgumentException(
$"ToStrict received OnError while draining stream! Accumulated elements: ${string.Join(", ", result)}",
Expand Down