@@ -17,9 +17,14 @@ import org.apache.pekko
17
17
import pekko .NotUsed
18
18
import pekko .stream ._
19
19
import pekko .stream .impl .TraversalTestUtils ._
20
- import pekko .stream .scaladsl .Keep
20
+ import pekko .stream .impl .fusing .IterableSource
21
+ import pekko .stream .impl .fusing .GraphStages .{ FutureSource , SingleSource }
22
+ import pekko .stream .scaladsl .{ Keep , Source }
23
+ import pekko .util .OptionVal
21
24
import pekko .testkit .PekkoSpec
22
25
26
+ import scala .concurrent .Future
27
+
23
28
class TraversalBuilderSpec extends PekkoSpec {
24
29
25
30
" CompositeTraversalBuilder" must {
@@ -447,4 +452,93 @@ class TraversalBuilderSpec extends PekkoSpec {
447
452
}
448
453
}
449
454
455
+ " find Source.single via TraversalBuilder" in {
456
+ TraversalBuilder .getSingleSource(Source .single(" a" )).get.elem should === (" a" )
457
+ TraversalBuilder .getSingleSource(Source (List (" a" , " b" ))) should be(OptionVal .None )
458
+
459
+ val singleSourceA = new SingleSource (" a" )
460
+ TraversalBuilder .getSingleSource(singleSourceA) should be(OptionVal .Some (singleSourceA))
461
+
462
+ TraversalBuilder .getSingleSource(Source .single(" c" ).async) should be(OptionVal .None )
463
+ TraversalBuilder .getSingleSource(Source .single(" d" ).mapMaterializedValue(_ => " Mat" )) should be(OptionVal .None )
464
+ }
465
+
466
+ " find Source.single via TraversalBuilder with getValuePresentedSource" in {
467
+ TraversalBuilder .getValuePresentedSource(Source .single(" a" )).get.asInstanceOf [SingleSource [String ]].elem should === (
468
+ " a" )
469
+ val singleSourceA = new SingleSource (" a" )
470
+ TraversalBuilder .getValuePresentedSource(singleSourceA) should be(OptionVal .Some (singleSourceA))
471
+
472
+ TraversalBuilder .getValuePresentedSource(Source .single(" c" ).async) should be(OptionVal .None )
473
+ TraversalBuilder .getValuePresentedSource(Source .single(" d" ).mapMaterializedValue(_ => " Mat" )) should be(
474
+ OptionVal .None )
475
+ }
476
+
477
+ " find Source.empty via TraversalBuilder with getValuePresentedSource" in {
478
+ val emptySource = EmptySource
479
+ TraversalBuilder .getValuePresentedSource(emptySource) should be(OptionVal .Some (emptySource))
480
+
481
+ TraversalBuilder .getValuePresentedSource(Source .empty.async) should be(OptionVal .None )
482
+ TraversalBuilder .getValuePresentedSource(Source .empty.mapMaterializedValue(_ => " Mat" )) should be(OptionVal .None )
483
+ }
484
+
485
+ " find javadsl Source.empty via TraversalBuilder with getValuePresentedSource" in {
486
+ import pekko .stream .javadsl .Source
487
+ val emptySource = Source .empty()
488
+ TraversalBuilder .getValuePresentedSource(Source .empty()) should be(OptionVal .Some (emptySource))
489
+
490
+ TraversalBuilder .getValuePresentedSource(Source .empty().async) should be(OptionVal .None )
491
+ TraversalBuilder .getValuePresentedSource(Source .empty().mapMaterializedValue(_ => " Mat" )) should be(OptionVal .None )
492
+ }
493
+
494
+ " find Source.future via TraversalBuilder with getValuePresentedSource" in {
495
+ val future = Future .successful(" a" )
496
+ TraversalBuilder .getValuePresentedSource(Source .future(future)).get.asInstanceOf [FutureSource [String ]].future should === (
497
+ future)
498
+ val futureSourceA = new FutureSource (future)
499
+ TraversalBuilder .getValuePresentedSource(futureSourceA) should be(OptionVal .Some (futureSourceA))
500
+
501
+ TraversalBuilder .getValuePresentedSource(Source .future(future).async) should be(OptionVal .None )
502
+ TraversalBuilder .getValuePresentedSource(Source .future(future).mapMaterializedValue(_ => " Mat" )) should be(
503
+ OptionVal .None )
504
+ }
505
+
506
+ " find Source.iterable via TraversalBuilder with getValuePresentedSource" in {
507
+ val iterable = List (" a" )
508
+ TraversalBuilder .getValuePresentedSource(Source (iterable)).get.asInstanceOf [IterableSource [String ]].elements should === (
509
+ iterable)
510
+ val iterableSource = new IterableSource (iterable)
511
+ TraversalBuilder .getValuePresentedSource(iterableSource) should be(OptionVal .Some (iterableSource))
512
+
513
+ TraversalBuilder .getValuePresentedSource(Source (iterable).async) should be(OptionVal .None )
514
+ TraversalBuilder .getValuePresentedSource(Source (iterable).mapMaterializedValue(_ => " Mat" )) should be(
515
+ OptionVal .None )
516
+ }
517
+
518
+ " find Source.javaStreamSource via TraversalBuilder with getValuePresentedSource" in {
519
+ val javaStream = java.util.stream.Stream .empty[String ]()
520
+ TraversalBuilder .getValuePresentedSource(Source .fromJavaStream(() => javaStream)).get
521
+ .asInstanceOf [JavaStreamSource [String , _]].open() shouldEqual javaStream
522
+ val streamSource = new JavaStreamSource (() => javaStream)
523
+ TraversalBuilder .getValuePresentedSource(streamSource) should be(OptionVal .Some (streamSource))
524
+
525
+ TraversalBuilder .getValuePresentedSource(Source .fromJavaStream(() => javaStream).async) should be(OptionVal .None )
526
+ TraversalBuilder .getValuePresentedSource(
527
+ Source .fromJavaStream(() => javaStream).mapMaterializedValue(_ => " Mat" )) should be(
528
+ OptionVal .None )
529
+ }
530
+
531
+ " find Source.failed via TraversalBuilder with getValuePresentedSource" in {
532
+ val failure = new RuntimeException (" failure" )
533
+ TraversalBuilder .getValuePresentedSource(Source .failed(failure)).get.asInstanceOf [FailedSource [String ]]
534
+ .failure should === (
535
+ failure)
536
+ val failedSourceA = new FailedSource (failure)
537
+ TraversalBuilder .getValuePresentedSource(failedSourceA) should be(OptionVal .Some (failedSourceA))
538
+
539
+ TraversalBuilder .getValuePresentedSource(Source .failed(failure).async) should be(OptionVal .None )
540
+ TraversalBuilder .getValuePresentedSource(Source .failed(failure).mapMaterializedValue(_ => " Mat" )) should be(
541
+ OptionVal .None )
542
+ }
543
+
450
544
}
0 commit comments