|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="SeqSinkSpec.cs" company="Akka.NET Project"> |
| 3 | +// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com> |
| 4 | +// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> |
| 5 | +// </copyright> |
| 6 | +//----------------------------------------------------------------------- |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using Akka.Streams.Dsl; |
| 11 | +using Akka.TestKit; |
| 12 | +using FluentAssertions; |
| 13 | +using Xunit; |
| 14 | +using Xunit.Abstractions; |
| 15 | + |
| 16 | +namespace Akka.Streams.Tests.Dsl |
| 17 | +{ |
| 18 | + public class SetupSpec : AkkaSpec |
| 19 | + { |
| 20 | + private ActorMaterializer Materializer { get; } |
| 21 | + |
| 22 | + public SetupSpec(ITestOutputHelper helper) |
| 23 | + : base(helper) => Materializer = ActorMaterializer.Create(Sys); |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void SourceSetup_should_expose_materializer() |
| 27 | + { |
| 28 | + var source = Source.Setup((mat, _) => Source.Single(mat.IsShutdown)); |
| 29 | + source.RunWith(Sink.First<bool>(), Materializer).Result.Should().BeFalse(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void SourceSetup_should_expose_attributes() |
| 34 | + { |
| 35 | + var source = Source.Setup((_, attr) => Source.Single(attr.AttributeList)); |
| 36 | + source.RunWith(Sink.First<IEnumerable<Attributes.IAttribute>>(), Materializer).Result.Should().NotBeEmpty(); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void SourceSetup_should_propagate_materialized_value() |
| 41 | + { |
| 42 | + var source = Source.Setup((_, _) => Source.Maybe<NotUsed>()); |
| 43 | + |
| 44 | + var (completion, element) = source.ToMaterialized(Sink.First<NotUsed>(), Keep.Both).Run(Materializer); |
| 45 | + completion.Result.TrySetResult(NotUsed.Instance); |
| 46 | + element.Result.ShouldBe(NotUsed.Instance); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void SourceSetup_should_propagate_attributes() |
| 51 | + { |
| 52 | + var source = Source.Setup((_, attr) => Source.Single(attr.GetNameLifted)).Named("my-name"); |
| 53 | + source.RunWith(Sink.First<Func<string>>(), Materializer).Result.Invoke().ShouldBe("setup-my-name"); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void SourceSetup_should_propagate_attributes_when_nested() |
| 58 | + { |
| 59 | + var source = Source.Setup((_, _) => Source.Setup((_, attr) => Source.Single(attr.GetNameLifted))).Named("my-name"); |
| 60 | + source.RunWith(Sink.First<Func<string>>(), Materializer).Result.Invoke().ShouldBe("setup-my-name-setup"); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void SourceSetup_should_handle_factory_failure() |
| 65 | + { |
| 66 | + var error = new ApplicationException("boom"); |
| 67 | + var source = Source.Setup<NotUsed, NotUsed>((_, _) => throw error); |
| 68 | + |
| 69 | + var (materialized, completion) = source.ToMaterialized(Sink.First<NotUsed>(), Keep.Both).Run(Materializer); |
| 70 | + |
| 71 | + Assert.Throws<AggregateException>(() => materialized.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 72 | + Assert.Throws<AggregateException>(() => completion.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void SourceSetup_should_handle_materialization_failure() |
| 77 | + { |
| 78 | + var error = new ApplicationException("boom"); |
| 79 | + var source = Source.Setup((_, _) => Source.Empty<NotUsed>().MapMaterializedValue<NotUsed>(_ => throw error)); |
| 80 | + |
| 81 | + var (materialized, completion) = source.ToMaterialized(Sink.First<NotUsed>(), Keep.Both).Run(Materializer); |
| 82 | + |
| 83 | + Assert.Throws<AggregateException>(() => materialized.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 84 | + Assert.Throws<AggregateException>(() => completion.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void FlowSetup_should_expose_materializer() |
| 89 | + { |
| 90 | + var flow = Flow.Setup((mat, _) => Flow.FromSinkAndSource( |
| 91 | + Sink.Ignore<object>().MapMaterializedValue(_ => NotUsed.Instance), |
| 92 | + Source.Single(mat.IsShutdown))); |
| 93 | + |
| 94 | + Source.Empty<object>().Via(flow).RunWith(Sink.First<bool>(), Materializer).Result.Should().BeFalse(); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void FlowSetup_should_expose_attributes() |
| 99 | + { |
| 100 | + var flow = Flow.Setup((_, attr) => Flow.FromSinkAndSource( |
| 101 | + Sink.Ignore<object>().MapMaterializedValue(_ => NotUsed.Instance), |
| 102 | + Source.Single(attr.AttributeList))); |
| 103 | + |
| 104 | + Source.Empty<object>().Via(flow).RunWith(Sink.First<IEnumerable<Attributes.IAttribute>>(), Materializer).Result.Should().NotBeEmpty(); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void FlowSetup_should_propagate_materialized_value() |
| 109 | + { |
| 110 | + var flow = Flow.Setup((_, _) => Flow.FromSinkAndSource( |
| 111 | + Sink.Ignore<object>().MapMaterializedValue(_ => NotUsed.Instance), |
| 112 | + Source.Maybe<NotUsed>(), Keep.Right)); |
| 113 | + |
| 114 | + var (completion, element) = Source.Empty<object>() |
| 115 | + .ViaMaterialized(flow, Keep.Right) |
| 116 | + .ToMaterialized(Sink.First<NotUsed>(), Keep.Both).Run(Materializer); |
| 117 | + |
| 118 | + completion.Result.TrySetResult(NotUsed.Instance); |
| 119 | + element.Result.ShouldBe(NotUsed.Instance); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void FlowSetup_should_propagate_attributes() |
| 124 | + { |
| 125 | + var flow = Flow.Setup((_, attr) => Flow.FromSinkAndSource( |
| 126 | + Sink.Ignore<object>().MapMaterializedValue(_ => NotUsed.Instance), |
| 127 | + Source.Single(attr.GetNameLifted))).Named("my-name"); |
| 128 | + |
| 129 | + Source.Empty<object>().Via(flow).RunWith(Sink.First<Func<string>>(), Materializer).Result.Invoke().ShouldBe("setup-my-name"); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void FlowSetup_should_propagate_attributes_when_nested() |
| 134 | + { |
| 135 | + var flow = Flow.Setup((_, _) => Flow.Setup((_, attr) => Flow.FromSinkAndSource( |
| 136 | + Sink.Ignore<object>().MapMaterializedValue(_ => NotUsed.Instance), |
| 137 | + Source.Single(attr.GetNameLifted)))).Named("my-name"); |
| 138 | + |
| 139 | + Source.Empty<object>().Via(flow).RunWith(Sink.First<Func<string>>(), Materializer).Result.Invoke().ShouldBe("setup-my-name-setup"); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void FlowSetup_should_handle_factory_failure() |
| 144 | + { |
| 145 | + var error = new ApplicationException("boom"); |
| 146 | + var flow = Flow.Setup<NotUsed, NotUsed, NotUsed>((_, _) => throw error); |
| 147 | + |
| 148 | + var (materialized, completion) = Source.Empty<NotUsed>() |
| 149 | + .ViaMaterialized(flow, Keep.Right) |
| 150 | + .ToMaterialized(Sink.First<NotUsed>(), Keep.Both) |
| 151 | + .Run(Materializer); |
| 152 | + |
| 153 | + Assert.Throws<AggregateException>(() => materialized.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 154 | + Assert.Throws<AggregateException>(() => completion.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void FlowSetup_should_handle_materialization_failure() |
| 159 | + { |
| 160 | + var error = new ApplicationException("boom"); |
| 161 | + var flow = Flow.Setup((_, _) => Flow.Create<NotUsed>().MapMaterializedValue<NotUsed>(_ => throw error)); |
| 162 | + |
| 163 | + var (materialized, completion) = Source.Empty<NotUsed>() |
| 164 | + .ViaMaterialized(flow, Keep.Right) |
| 165 | + .ToMaterialized(Sink.First<NotUsed>(), Keep.Both) |
| 166 | + .Run(Materializer); |
| 167 | + |
| 168 | + Assert.Throws<AggregateException>(() => materialized.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 169 | + Assert.Throws<AggregateException>(() => completion.Result).InnerException?.Should().BeOfType<ApplicationException>(); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments