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 - ChainSetup #5909

Merged
Show file tree
Hide file tree
Changes from all commits
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
103 changes: 88 additions & 15 deletions src/core/Akka.Streams.TestKit/ChainSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Streams.Dsl;
using Akka.TestKit;
Expand All @@ -15,6 +16,9 @@ namespace Akka.Streams.TestKit
{
public class ChainSetup<TIn, TOut, TMat>
{
private readonly Func<Source<TOut, NotUsed>, ActorMaterializer, IPublisher<TOut>> _toPublisher;
private readonly Func<Flow<TIn, TIn, NotUsed>, Flow<TIn, TOut, TMat>> _stream;
private readonly ActorMaterializer _materializer;
protected readonly TestKitBase System;

public ChainSetup(
Expand All @@ -24,18 +28,11 @@ public ChainSetup(
Func<Source<TOut, NotUsed>, ActorMaterializer, IPublisher<TOut>> toPublisher,
TestKitBase system)
{

Settings = settings;
System = system;

Upstream = system.CreateManualPublisherProbe<TIn>();
Downstream = system.CreateSubscriberProbe<TOut>();

var s = Source.FromPublisher(Upstream).Via(stream(Flow.Identity<TIn>().Select(x => x).Named("buh")));
Publisher = toPublisher(s, materializer);
UpstreamSubscription = Upstream.ExpectSubscription();
Publisher.Subscribe(Downstream);
DownstreamSubscription = Downstream.ExpectSubscription();
_toPublisher = toPublisher;
_stream = stream;
_materializer = materializer;
}

public ChainSetup(
Expand All @@ -57,11 +54,87 @@ public ChainSetup(
{
}

[Obsolete("Will be removed after async_testkit conversion is complete. Use InitializeAsync instead")]
public ChainSetup<TIn, TOut, TMat> Initialize()
=> InitializeAsync()
.ConfigureAwait(false).GetAwaiter().GetResult();

public virtual async Task<ChainSetup<TIn, TOut, TMat>> InitializeAsync()
{
_upstream = System.CreateManualPublisherProbe<TIn>();
_downstream = System.CreateSubscriberProbe<TOut>();

var s = Source.FromPublisher(_upstream).Via(_stream(Flow.Identity<TIn>().Select(x => x).Named("buh")));
_publisher = _toPublisher(s, _materializer);
_upstreamSubscription = await _upstream.ExpectSubscriptionAsync();
_publisher.Subscribe(_downstream);
_downstreamSubscription = await _downstream.ExpectSubscriptionAsync();

Initialized = true;

return this;
}

private TestPublisher.ManualProbe<TIn> _upstream;
private TestSubscriber.ManualProbe<TOut> _downstream;
private IPublisher<TOut> _publisher;
private StreamTestKit.PublisherProbeSubscription<TIn> _upstreamSubscription;
private ISubscription _downstreamSubscription;

public bool Initialized { get; private set; }

public ActorMaterializerSettings Settings { get; }
public TestPublisher.ManualProbe<TIn> Upstream { get; }
public TestSubscriber.ManualProbe<TOut> Downstream { get; }
public IPublisher<TOut> Publisher { get; }
public StreamTestKit.PublisherProbeSubscription<TIn> UpstreamSubscription { get; }
public ISubscription DownstreamSubscription { get; }

public TestPublisher.ManualProbe<TIn> Upstream
{
get
{
EnsureInitialized();
return _upstream;
}
}

public TestSubscriber.ManualProbe<TOut> Downstream
{
get
{
EnsureInitialized();
return _downstream;
}
}

public IPublisher<TOut> Publisher
{
get
{
EnsureInitialized();
return _publisher;
}
}

public StreamTestKit.PublisherProbeSubscription<TIn> UpstreamSubscription
{
get
{
EnsureInitialized();
return _upstreamSubscription;
}
}

public ISubscription DownstreamSubscription
{
get
{
EnsureInitialized();
return _downstreamSubscription;
}
}

protected virtual void EnsureInitialized()
{
if (!Initialized)
throw new InvalidOperationException(
$"ChainSetup has not been initialized. Please make sure to call {nameof(InitializeAsync)} first.");
}
}
}
4 changes: 3 additions & 1 deletion src/core/Akka.Streams.TestKit/ScriptedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Akka.Streams.Dsl;
using Akka.TestKit;
using Akka.Util;
using Akka.Util.Internal;
using Reactive.Streams;
using Xunit.Abstractions;

Expand Down Expand Up @@ -306,7 +307,8 @@ protected void RunScript<TIn2, TOut2, TMat2>(Script<TIn2, TOut2> script, ActorMa
Func<Flow<TIn2, TIn2, NotUsed>, Flow<TIn2, TOut2, TMat2>> op,
int maximumOverrun = 3, int maximumRequest = 3, int maximumBuffer = 3)
{
new ScriptRunner<TIn2, TOut2, TMat2>(op, settings, script, maximumOverrun, maximumRequest, maximumBuffer, this).Run();
new ScriptRunner<TIn2, TOut2, TMat2>(op, settings, script, maximumOverrun, maximumRequest, maximumBuffer, this)
.Initialize().AsInstanceOf<ScriptRunner<TIn2, TOut2, TMat2>>().Run();
}

protected static IPublisher<TOut> ToPublisher<TOut>(Source<TOut, NotUsed> source, IMaterializer materializer)
Expand Down
45 changes: 30 additions & 15 deletions src/core/Akka.Streams.Tests/Dsl/FlowSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ public void A_flow_must_request_initial_elements_from_upstream(string name, int

if (name.Equals("identity"))
setup = new ChainSetup<int, int, NotUsed>(Identity, Settings.WithInputBuffer(n, n),
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
else
setup = new ChainSetup<int, int, NotUsed>(Identity2, Settings.WithInputBuffer(n, n),
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();

setup.Upstream.ExpectRequest(setup.UpstreamSubscription, setup.Settings.MaxInputBufferSize);
}
Expand All @@ -75,7 +77,8 @@ public void A_flow_must_request_initial_elements_from_upstream(string name, int
public void A_Flow_must_request_more_elements_from_upstream_when_downstream_requests_more_elements()
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings,
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
setup.Upstream.ExpectRequest(setup.UpstreamSubscription, Settings.MaxInputBufferSize);
setup.DownstreamSubscription.Request(1);
setup.Upstream.ExpectNoMsg(TimeSpan.FromMilliseconds(100));
Expand All @@ -96,7 +99,8 @@ public void A_Flow_must_request_more_elements_from_upstream_when_downstream_requ
public void A_Flow_must_deliver_events_when_publisher_sends_elements_and_then_completes()
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings,
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
setup.DownstreamSubscription.Request(1);
setup.UpstreamSubscription.SendNext("test");
setup.UpstreamSubscription.SendComplete();
Expand All @@ -108,7 +112,8 @@ public void A_Flow_must_deliver_events_when_publisher_sends_elements_and_then_co
public void A_Flow_must_deliver_complete_signal_when_publisher_immediately_completes()
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings,
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
setup.UpstreamSubscription.SendComplete();
setup.Downstream.ExpectComplete();
}
Expand All @@ -117,7 +122,8 @@ public void A_Flow_must_deliver_complete_signal_when_publisher_immediately_compl
public void A_Flow_must_deliver_error_signal_when_publisher_immediately_fails()
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings,
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
var weirdError = new Exception("weird test exception");
setup.UpstreamSubscription.SendError(weirdError);
setup.Downstream.ExpectError().Should().Be(weirdError);
Expand All @@ -127,7 +133,8 @@ public void A_Flow_must_deliver_error_signal_when_publisher_immediately_fails()
public void A_Flow_must_cancel_upstream_when_single_subscriber_cancels_subscription_while_receiving_data()
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this);
(settings, factory) => ActorMaterializer.Create(factory, settings), ToPublisher, this)
.Initialize();
setup.DownstreamSubscription.Request(5);
setup.UpstreamSubscription.ExpectRequest(1);
setup.UpstreamSubscription.SendNext("test");
Expand Down Expand Up @@ -350,7 +357,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_adapt_speed_to_the_c
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this)
.Initialize();
var downstream2 = this.CreateManualSubscriberProbe<string>();
setup.Publisher.Subscribe(downstream2);
var downstream2Subscription = downstream2.ExpectSubscription();
Expand Down Expand Up @@ -379,7 +387,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_support_slow_subscri
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 2), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 2), this)
.Initialize();
var downstream2 = this.CreateManualSubscriberProbe<string>();
setup.Publisher.Subscribe(downstream2);
var downstream2Subscription = downstream2.ExpectSubscription();
Expand Down Expand Up @@ -422,7 +431,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_support_incoming_sub
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this)
.Initialize();

setup.DownstreamSubscription.Request(5);
setup.Upstream.ExpectRequest(setup.UpstreamSubscription, 1);
Expand Down Expand Up @@ -463,7 +473,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_be_unblocked_when_bl
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this)
.Initialize();
var downstream2 = this.CreateManualSubscriberProbe<string>();
setup.Publisher.Subscribe(downstream2);
var downstream2Subscription = downstream2.ExpectSubscription();
Expand Down Expand Up @@ -503,7 +514,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_call_future_subscrib
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this)
.Initialize();
var downstream2 = this.CreateManualSubscriberProbe<string>();
// don't link it just yet

Expand Down Expand Up @@ -547,7 +559,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_call_future_subscrib
throw new TestException("test");
}), Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 1), this)
.Initialize();

setup.DownstreamSubscription.Request(1);
setup.UpstreamSubscription.ExpectRequest(1);
Expand All @@ -567,7 +580,8 @@ public void A_Flow_with_multiple_subscribers_FanOutBox_must_call_future_subscrib
{
var setup = new ChainSetup<string, string, NotUsed>(Identity, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 16), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 16), this)
.Initialize();

// make sure stream is initialized before canceling downstream
Thread.Sleep(100);
Expand Down Expand Up @@ -596,7 +610,8 @@ public void A_broken_Flow_must_cancel_upstream_and_call_onError_on_current_and_f
{
var setup = new ChainSetup<string, string, NotUsed>(FaultyFlow<string,string,string>, Settings.WithInputBuffer(1, 1),
(settings, factory) => ActorMaterializer.Create(factory, settings),
(source, materializer) => ToFanoutPublisher(source, materializer, 16), this);
(source, materializer) => ToFanoutPublisher(source, materializer, 16), this)
.Initialize();

Action<TestSubscriber.ManualProbe<string>> checkError = sprobe =>
{
Expand Down