-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FlowFlattenMergeSpec.cs
391 lines (330 loc) · 15 KB
/
FlowFlattenMergeSpec.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//-----------------------------------------------------------------------
// <copyright file="FlowFlattenMergeSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Akka.Streams.Dsl;
using Akka.Streams.Stage;
using Akka.Streams.TestKit;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.TestKit.Xunit2.Attributes;
using Akka.Util.Internal;
using FluentAssertions;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;
using static FluentAssertions.FluentActions;
namespace Akka.Streams.Tests.Dsl
{
public class FlowFlattenMergeSpec : AkkaSpec
{
private ActorMaterializer Materializer { get; }
public FlowFlattenMergeSpec(ITestOutputHelper helper) : base(helper)
{
Materializer = ActorMaterializer.Create(Sys);
}
private static Source<int, NotUsed> Src10(int i) => Source.From(Enumerable.Range(i, 10));
private static Source<int, NotUsed> Blocked => Source.FromTask(new TaskCompletionSource<int>().Task);
private static Sink<int, Task<IEnumerable<int>>> ToSeq
=> Flow.Create<int>().Grouped(1000).ToMaterialized(Sink.First<IEnumerable<int>>(), Keep.Right);
private static Sink<int, Task<ImmutableHashSet<int>>> ToSet =>
Flow.Create<int>()
.Grouped(1000)
.Select(x => x.ToImmutableHashSet())
.ToMaterialized(Sink.First<ImmutableHashSet<int>>(), Keep.Right);
[Fact]
public async Task A_FlattenMerge_must_work_in_the_nominal_case()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var task = Source.From(new[] {Src10(0), Src10(10), Src10(20), Src10(30)})
.MergeMany(4, s => s)
.RunWith(ToSet, Materializer);
await task.ShouldCompleteWithin(1.Seconds());
task.Result.Should().BeEquivalentTo(Enumerable.Range(0, 40));
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_not_be_held_back_by_one_slow_stream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var task = Source.From(new[] { Src10(0), Src10(10), Blocked, Src10(20), Src10(30) })
.MergeMany(3, s => s)
.Take(40)
.RunWith(ToSet, Materializer);
await task.ShouldCompleteWithin(1.Seconds());
task.Result.Should().BeEquivalentTo(Enumerable.Range(0, 40));
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_respect_breadth()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var task = Source.From(new[] { Src10(0), Src10(10), Src10(20), Blocked, Blocked, Src10(30) })
.MergeMany(3, s => s)
.Take(40)
.RunWith(ToSeq, Materializer);
await task.ShouldCompleteWithin(1.Seconds());
task.Result.Take(30).Should().BeEquivalentTo(Enumerable.Range(0, 30));
task.Result.Drop(30).Should().BeEquivalentTo(Enumerable.Range(30, 10));
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_propagate_early_failure_from_main_stream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var ex = new TestException("buh");
var future = Source.Failed<Source<int, NotUsed>>(ex)
.MergeMany(1, x => x)
.RunWith(Sink.First<int>(), Materializer);
(await Awaiting(() => future.ShouldCompleteWithin(1.Seconds()))
.Should().ThrowAsync<TestException>()).And.Should().Be(ex);
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_propagate_late_failure_from_main_stream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var ex = new TestException("buh");
var future = Source.Combine(Source.From(new[] {Blocked, Blocked}), Source.Failed<Source<int, NotUsed>>(ex),
i => new Merge<Source<int, NotUsed>>(i))
.MergeMany(10, x => x)
.RunWith(Sink.First<int>(), Materializer);
(await Awaiting(() => future.ShouldCompleteWithin(1.Seconds()))
.Should().ThrowAsync<TestException>()).And.Should().Be(ex);
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_propagate_failure_from_map_function()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var ex = new TestException("buh");
var future = Source.From(Enumerable.Range(1, 3))
.MergeMany(10, x =>
{
if (x == 3)
throw ex;
return Blocked;
})
.RunWith(Sink.First<int>(), Materializer);
(await Awaiting(() => future.ShouldCompleteWithin(1.Seconds()))
.Should().ThrowAsync<TestException>()).And.Should().Be(ex);
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_bubble_up_subStream_exceptions()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var ex = new TestException("buh");
var future = Source.From(new[] { Blocked, Blocked, Source.Failed<int>(ex) })
.MergeMany(10, x => x)
.RunWith(Sink.First<int>(), Materializer);
(await Awaiting(() => future.ShouldCompleteWithin(1.Seconds()))
.Should().ThrowAsync<TestException>()).And.Should().Be(ex);
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_cancel_subStreams_when_failing_from_main_stream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var p1 = this.CreatePublisherProbe<int>();
var p2 = this.CreatePublisherProbe<int>();
var ex = new TestException("buh");
var p = new TaskCompletionSource<Source<int, NotUsed>>();
// task is intentionally not awaited
var task = Source.Combine(
Source.From(new[] {Source.FromPublisher(p1), Source.FromPublisher(p2)}),
Source.FromTask(p.Task), i => new Merge<Source<int, NotUsed>>(i))
.MergeMany(5, x => x)
.RunWith(Sink.First<int>(), Materializer);
await p1.ExpectRequestAsync();
await p2.ExpectRequestAsync();
p.SetException(ex);
await p1.ExpectCancellationAsync();
await p2.ExpectCancellationAsync();
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_cancel_subStreams_when_failing_from_subStream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var p1 = this.CreatePublisherProbe<int>();
var p2 = this.CreatePublisherProbe<int>();
var ex = new TestException("buh");
var p = new TaskCompletionSource<int>();
// task is intentionally not awaited
var task = Source.From(new[]
{
Source.FromPublisher(p1),
Source.FromPublisher(p2),
Source.FromTask(p.Task)
})
.MergeMany(5, x => x)
.RunWith(Sink.First<int>(), Materializer);
await p1.ExpectRequestAsync();
await p2.ExpectRequestAsync();
p.SetException(ex);
await p1.ExpectCancellationAsync();
await p2.ExpectCancellationAsync();
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_cancel_subStreams_when_failing_map_function()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var settings = ActorMaterializerSettings.Create(Sys).WithSyncProcessingLimit(1).WithInputBuffer(1, 1);
var materializer = ActorMaterializer.Create(Sys, settings);
var p = this.CreatePublisherProbe<int>();
var ex = new TestException("buh");
var latch = new TestLatch();
// task is intentionally not awaited
var task = Source.From(Enumerable.Range(1, 3)).MergeMany(10, i =>
{
if (i == 1)
return Source.FromPublisher(p);
latch.Ready(TimeSpan.FromSeconds(3));
throw ex;
}).RunWith(Sink.First<int>(), materializer);
await p.ExpectRequestAsync();
latch.CountDown();
await p.ExpectCancellationAsync();
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_cancel_subStreams_when_being_cancelled()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var p1 = this.CreatePublisherProbe<int>();
var p2 = this.CreatePublisherProbe<int>();
var sink = Source.From(new[] {Source.FromPublisher(p1), Source.FromPublisher(p2)})
.MergeMany(5, x => x)
.RunWith(this.SinkProbe<int>(), Materializer);
sink.Request(1);
await p1.ExpectRequestAsync();
await p2.ExpectRequestAsync();
sink.Cancel();
await p1.ExpectCancellationAsync();
await p2.ExpectCancellationAsync();
}, Materializer);
}
[LocalFact(SkipLocal = "Racy on Azure AzDo")]
public async Task A_FlattenMerge_must_work_with_many_concurrently_queued_events()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
const int noOfSources = 100;
var p = Source.From(Enumerable.Range(0, noOfSources).Select(i => Src10(10*i)))
.MergeMany(int.MaxValue, x => x)
.RunWith(this.SinkProbe<int>(), Materializer);
await p.EnsureSubscriptionAsync();
await p.ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
var elems = new List<int>();
foreach (var _ in Enumerable.Range(0, noOfSources * 10))
{
elems.Add(await p.RequestNextAsync());
}
await p.ExpectCompleteAsync();
elems.Should().BeEquivalentTo(Enumerable.Range(0, noOfSources * 10));
}, Materializer);
}
private sealed class AttributesSourceStage : GraphStage<SourceShape<Attributes>>
{
#region Logic
private sealed class Logic : OutGraphStageLogic
{
private readonly AttributesSourceStage _stage;
private readonly Attributes _inheritedAttributes;
public Logic(AttributesSourceStage stage, Attributes inheritedAttributes) : base(stage.Shape)
{
_stage = stage;
_inheritedAttributes = inheritedAttributes;
SetHandler(stage.Out, this);
}
public override void OnPull()
{
Push(_stage.Out, _inheritedAttributes);
CompleteStage();
}
}
#endregion
public AttributesSourceStage()
{
Shape = new SourceShape<Attributes>(Out);
}
private Outlet<Attributes> Out { get; } = new Outlet<Attributes>("AttributesSource.out");
public override SourceShape<Attributes> Shape { get; }
protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes) => new Logic(this, inheritedAttributes);
}
[Fact]
public async Task A_FlattenMerge_must_propagate_attributes_to_inner_stream()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var attributesSource = Source.FromGraph(new AttributesSourceStage());
var task = Source.Single(attributesSource.AddAttributes(Attributes.CreateName("inner")))
.MergeMany(1, x => x)
.AddAttributes(Attributes.CreateName("outer"))
.RunWith(Sink.First<Attributes>(), Materializer);
var attributes = (await task.ShouldCompleteWithin(3.Seconds())).AttributeList.ToList();
var innerName = new Attributes.Name("inner");
var outerName = new Attributes.Name("outer");
attributes.Should().Contain(innerName);
attributes.Should().Contain(outerName);
attributes.IndexOf(outerName).Should().BeLessThan(attributes.IndexOf(innerName));
}, Materializer);
}
[Fact]
public async Task A_FlattenMerge_must_bubble_up_subStream_materialization_exception()
{
await this.AssertAllStagesStoppedAsync(async () =>
{
var matFail = new TestException("fail!");
var task = Source.Single("whatever")
.MergeMany(4, x => Source.FromGraph(new FailingInnerMat(matFail)))
.RunWith(Sink.Ignore<string>(), Materializer);
await task.ShouldThrowWithin(matFail, 1.Seconds());
}, Materializer);
}
private sealed class FailingInnerMat : GraphStage<SourceShape<string>>
{
#region Logic
private sealed class FailingLogic : GraphStageLogic
{
public FailingLogic(Shape shape, TestException ex) : base(shape)
{
throw ex;
}
}
#endregion
public FailingInnerMat(TestException ex)
{
var outlet = new Outlet<string>("out");
Shape = new SourceShape<string>(outlet);
_ex = ex;
}
private readonly TestException _ex;
public override SourceShape<string> Shape { get; }
protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes)
{
return new FailingLogic(Shape, _ex);
}
}
}
}