From d8e62f69583b8a9a0eabdee3f3b42147cda59390 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 10 Jun 2022 23:51:59 +0700 Subject: [PATCH] Convert Akka.Streams.Tests to async - Dsl.LastSinkSpec (#5990) --- .../Akka.Streams.Tests/Dsl/LastSinkSpec.cs | 74 +++++++++++-------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/src/core/Akka.Streams.Tests/Dsl/LastSinkSpec.cs b/src/core/Akka.Streams.Tests/Dsl/LastSinkSpec.cs index d361b408793..a162507e2b9 100644 --- a/src/core/Akka.Streams.Tests/Dsl/LastSinkSpec.cs +++ b/src/core/Akka.Streams.Tests/Dsl/LastSinkSpec.cs @@ -7,11 +7,15 @@ using System; using System.Linq; +using System.Threading.Tasks; using Akka.Streams.Dsl; using Akka.Streams.TestKit; +using Akka.TestKit.Extensions; using FluentAssertions; +using FluentAssertions.Extensions; using Xunit; using Xunit.Abstractions; +using static FluentAssertions.FluentActions; namespace Akka.Streams.Tests.Dsl { @@ -26,75 +30,83 @@ public LastSinkSpec(ITestOutputHelper helper):base(helper) } [Fact] - public void A_Flow_with_Sink_Last_must_yield_the_last_value() + public async Task A_Flow_with_Sink_Last_must_yield_the_last_value() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - var task = Source.From(Enumerable.Range(1,42)).Select(x=>x).RunWith(Sink.Last(), Materializer); - task.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); - task.Result.Should().Be(42); + var result = await Source.From(Enumerable.Range(1,42)).Select(x=>x) + .RunWith(Sink.Last(), Materializer) + .ShouldCompleteWithin(1.Seconds()); + result.Should().Be(42); }, Materializer); } [Fact] - public void A_Flow_with_Sink_Last_must_yield_the_first_error() + public async Task A_Flow_with_Sink_Last_must_yield_the_first_error() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - Source.Failed(new Exception("ex")) - .Invoking(s => s.RunWith(Sink.Last(), Materializer).Wait(TimeSpan.FromSeconds(1))) - .Should().Throw() + (await Awaiting(() => + Source.Failed(new Exception("ex")) + .RunWith(Sink.Last(), Materializer)) + .Should().ThrowAsync() + .ShouldCompleteWithin(1.Seconds())) .WithInnerException() .WithMessage("ex"); }, Materializer); } [Fact] - public void A_Flow_with_Sink_Last_must_yield_NoSuchElementException_for_empty_stream() + public async Task A_Flow_with_Sink_Last_must_yield_NoSuchElementException_for_empty_stream() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - Source.Empty() - .Invoking(s => s.RunWith(Sink.Last(), Materializer).Wait(TimeSpan.FromSeconds(1))) - .Should().Throw() + (await Awaiting(() => + Source.Empty() + .RunWith(Sink.Last(), Materializer)) + .Should().ThrowAsync() + .ShouldCompleteWithin(1.Seconds())) .WithInnerException() .WithMessage("Last of empty stream"); }, Materializer); } - [Fact] - public void A_Flow_with_Sink_LastOption_must_yield_the_last_value() + public async Task A_Flow_with_Sink_LastOption_must_yield_the_last_value() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - var task = Source.From(Enumerable.Range(1, 42)).Select(x => x).RunWith(Sink.LastOrDefault(), Materializer); - task.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); - task.Result.Should().Be(42); + var result = await Source.From(Enumerable.Range(1, 42)).Select(x => x) + .RunWith(Sink.LastOrDefault(), Materializer) + .ShouldCompleteWithin(1.Seconds()); + result.Should().Be(42); }, Materializer); } [Fact] - public void A_Flow_with_Sink_LastOption_must_yield_the_first_error() + public async Task A_Flow_with_Sink_LastOption_must_yield_the_first_error() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - Source.Failed(new Exception("ex")) - .Invoking(s => s.RunWith(Sink.LastOrDefault(), Materializer).Wait(TimeSpan.FromSeconds(1))) - .Should().Throw() + (await Awaiting(async () => + Source.Failed(new Exception("ex")) + .RunWith(Sink.LastOrDefault(), Materializer)) + .Should().ThrowAsync() + .ShouldCompleteWithin(1.Seconds())) .WithInnerException() .WithMessage("ex"); }, Materializer); } [Fact] - public void A_Flow_with_Sink_LastOption_must_yield_default_for_empty_stream() + public async Task A_Flow_with_Sink_LastOption_must_yield_default_for_empty_stream() { - this.AssertAllStagesStopped(() => + await this.AssertAllStagesStoppedAsync(async () => { - var task = Source.Empty().RunWith(Sink.LastOrDefault(), Materializer); - task.Wait(TimeSpan.FromSeconds(1)).Should().BeTrue(); - task.Result.Should().Be(0); + var result = await Source.Empty() + .RunWith(Sink.LastOrDefault(), Materializer) + .ShouldCompleteWithin(1.Seconds()); + result.Should().Be(0); }, Materializer); } }