/** * Copyright (C) 2014-2017 Lightbend Inc. */ package akka.stream.javadsl; import akka.Done; import akka.NotUsed; import akka.actor.ActorRef; import akka.dispatch.Foreach; import akka.dispatch.Futures; import akka.japi.JavaPartialFunction; import akka.japi.Pair; import akka.japi.function.*; import akka.stream.*; import akka.stream.impl.ConstantFun; import akka.stream.javadsl.GraphDSL.Builder; import akka.stream.stage.*; import akka.testkit.AkkaSpec; import akka.stream.testkit.TestPublisher; import akka.testkit.JavaTestKit; import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; import org.reactivestreams.Publisher; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; import akka.testkit.AkkaJUnitActorSystemResource; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Stream; import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription; import static org.junit.Assert.*; @SuppressWarnings("serial") public class FlowTest extends StreamTest { public FlowTest() { super(actorSystemResource); } @ClassRule public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest", AkkaSpec.testConf()); @Test public void mustBeAbleToUseSimpleOperators() { final JavaTestKit probe = new JavaTestKit(system); final String[] lookup = { "a", "b", "c", "d", "e", "f" }; final java.lang.Iterable input = Arrays.asList(0, 1, 2, 3, 4, 5); final Source ints = Source.from(input); final Flow flow1 = Flow.of(Integer.class).drop(2).take(3 ).takeWithin(FiniteDuration.create(10, TimeUnit.SECONDS )).map(new Function() { public String apply(Integer elem) { return lookup[elem]; } }).filter(new Predicate() { public boolean test(String elem) { return !elem.equals("c"); } }); final Flow flow2 = Flow.of(String.class).grouped(2 ).mapConcat(new Function, java.lang.Iterable>() { public java.util.List apply(java.util.List elem) { return elem; } }).groupedWithin(100, FiniteDuration.create(50, TimeUnit.MILLISECONDS) ).mapConcat(new Function, java.lang.Iterable>() { public java.util.List apply(java.util.List elem) { return elem; } }); ints.via(flow1.via(flow2)) .runFold("", (acc, elem) -> acc + elem, materializer) .thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender())); probe.expectMsgEquals("de"); } @Test public void mustBeAbleToUseDropWhile() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList(0, 1, 2, 3)); final Flow flow = Flow.of(Integer.class).dropWhile(elem -> elem < 2); final CompletionStage future = source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); probe.expectMsgEquals(2); probe.expectMsgEquals(3); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Test public void mustBeAbleToUseStatefulMaponcat() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList(1, 2, 3, 4, 5); final Source ints = Source.from(input); final Flow flow = Flow.of(Integer.class).statefulMapConcat( () -> { int[] state = new int[] {0}; return (elem) -> { List list = new ArrayList<>(Collections.nCopies(state[0], elem)); state[0] = elem; return list; }; }); ints.via(flow) .runFold("", (acc, elem) -> acc + elem, materializer) .thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender())); probe.expectMsgEquals("2334445555"); } @Test public void mustBeAbleToUseIntersperse() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList("0", "1", "2", "3")); final Flow flow = Flow.of(String.class).intersperse("[", ",", "]"); final CompletionStage future = source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); probe.expectMsgEquals("["); probe.expectMsgEquals("0"); probe.expectMsgEquals(","); probe.expectMsgEquals("1"); probe.expectMsgEquals(","); probe.expectMsgEquals("2"); probe.expectMsgEquals(","); probe.expectMsgEquals("3"); probe.expectMsgEquals("]"); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Test public void mustBeAbleToUseIntersperseAndConcat() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList("0", "1", "2", "3")); final Flow flow = Flow.of(String.class).intersperse(","); final CompletionStage future = Source.single(">> ").concat(source.via(flow)).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); probe.expectMsgEquals(">> "); probe.expectMsgEquals("0"); probe.expectMsgEquals(","); probe.expectMsgEquals("1"); probe.expectMsgEquals(","); probe.expectMsgEquals("2"); probe.expectMsgEquals(","); probe.expectMsgEquals("3"); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Test public void mustBeAbleToUseTakeWhile() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList(0, 1, 2, 3)); final Flow flow = Flow.of(Integer.class).takeWhile (new Predicate() { public boolean test(Integer elem) { return elem < 2; } }); final CompletionStage future = source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); probe.expectMsgEquals(0); probe.expectMsgEquals(1); FiniteDuration duration = Duration.apply(200, TimeUnit.MILLISECONDS); probe.expectNoMsg(duration); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Ignore("StatefulStage to be converted to GraphStage when Java Api is available (#18817)") @Test public void mustBeAbleToUseTransform() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7); // duplicate each element, stop after 4 elements, and emit sum to the end final Flow flow = Flow.of(Integer.class).via(new GraphStage>() { public final Inlet in = Inlet.create("in"); public final Outlet out = Outlet.create("out"); @Override public GraphStageLogic createLogic(Attributes inheritedAttributes) throws Exception { return new GraphStageLogic(shape()) { int sum = 0; int count = 0; { setHandler(in, new AbstractInHandler() { @Override public void onPush() throws Exception { final Integer element = grab(in); sum += element; count += 1; if (count == 4) { emitMultiple(out, Arrays.asList(element, element, sum).iterator(), () -> completeStage()); } else { emitMultiple(out, Arrays.asList(element, element).iterator()); } } }); setHandler(out, new AbstractOutHandler() { @Override public void onPull() throws Exception { pull(in); } }); } }; } @Override public FlowShape shape() { return FlowShape.of(in, out); } } ); Source.from(input).via(flow).runForeach((Procedure) elem -> probe.getRef().tell(elem, ActorRef.noSender()), materializer); probe.expectMsgEquals(0); probe.expectMsgEquals(0); probe.expectMsgEquals(1); probe.expectMsgEquals(1); probe.expectMsgEquals(2); probe.expectMsgEquals(2); probe.expectMsgEquals(3); probe.expectMsgEquals(3); probe.expectMsgEquals(6); } @SuppressWarnings("unchecked") @Test public void mustBeAbleToUseGroupBy() throws Exception { final Iterable input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee"); final Flow, NotUsed> flow = Flow .of(String.class) .groupBy(3, new Function() { public String apply(String elem) { return elem.substring(0, 1); } }) .grouped(10) .mergeSubstreams(); final CompletionStage>> future = Source.from(input).via(flow).limit(10).runWith(Sink.> seq(), materializer); final Object[] result = future.toCompletableFuture().get(1, TimeUnit.SECONDS).toArray(); Arrays.sort(result, (Comparator)(Object) new Comparator>() { @Override public int compare(List o1, List o2) { return o1.get(0).charAt(0) - o2.get(0).charAt(0); } }); assertArrayEquals(new Object[] { Arrays.asList("Aaa", "Abb"), Arrays.asList("Bcc"), Arrays.asList("Cdd", "Cee") }, result); } @Test public void mustBeAbleToUseSplitWhen() throws Exception { final Iterable input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F"); final Flow, NotUsed> flow = Flow .of(String.class) .splitWhen(new Predicate() { public boolean test(String elem) { return elem.equals("."); } }) .grouped(10) .concatSubstreams(); final CompletionStage>> future = Source.from(input).via(flow).limit(10).runWith(Sink.> seq(), materializer); final List> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS); assertEquals(Arrays.asList(Arrays.asList("A", "B", "C"), Arrays.asList(".", "D"), Arrays.asList(".", "E", "F")), result); } @Test public void mustBeAbleToUseSplitAfter() throws Exception { final Iterable input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F"); final Flow, NotUsed> flow = Flow .of(String.class) .splitAfter(new Predicate() { public boolean test(String elem) { return elem.equals("."); } }) .grouped(10) .concatSubstreams(); final CompletionStage>> future = Source.from(input).via(flow).limit(10).runWith(Sink.> seq(), materializer); final List> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS); assertEquals(Arrays.asList(Arrays.asList("A", "B", "C", "."), Arrays.asList("D", "."), Arrays.asList("E", "F")), result); } public GraphStage> op() { return new GraphStage>() { public final Inlet in = Inlet.create("in"); public final Outlet out = Outlet.create("out"); @Override public GraphStageLogic createLogic(Attributes inheritedAttributes) throws Exception { return new GraphStageLogic(shape()) { { setHandler(in, new AbstractInHandler() { @Override public void onPush() throws Exception { push(out, grab(in)); } }); setHandler(out, new AbstractOutHandler() { @Override public void onPull() throws Exception { pull(in); } }); } }; } @Override public FlowShape shape() { return FlowShape.of(in, out); } }; } @Test public void mustBeAbleToUseMerge() throws Exception { final Flow f1 = Flow.of(String.class).via(FlowTest.this.op()).named("f1"); final Flow f2 = Flow.of(String.class).via(FlowTest.this.op()).named("f2"); @SuppressWarnings("unused") final Flow f3 = Flow.of(String.class).via(FlowTest.this.op()).named("f3"); final Source in1 = Source.from(Arrays.asList("a", "b", "c")); final Source in2 = Source.from(Arrays.asList("d", "e", "f")); final Sink> publisher = Sink.asPublisher(AsPublisher.WITHOUT_FANOUT); final Source source = Source.fromGraph( GraphDSL.create(new Function, SourceShape>() { @Override public SourceShape apply(Builder b) throws Exception { final UniformFanInShape merge = b.add(Merge.create(2)); b.from(b.add(in1)).via(b.add(f1)).toInlet(merge.in(0)); b.from(b.add(in2)).via(b.add(f2)).toInlet(merge.in(1)); return new SourceShape(merge.out()); } })); // collecting final Publisher pub = source.runWith(publisher, materializer); final CompletionStage> all = Source.fromPublisher(pub).limit(100).runWith(Sink.seq(), materializer); final List result = all.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); assertEquals(new HashSet(Arrays.asList("a", "b", "c", "d", "e", "f")), new HashSet(result)); } @Test public void mustBeAbleToUseZip() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList(1, 2, 3); RunnableGraph.fromGraph(GraphDSL.create(new Function, ClosedShape>(){ public ClosedShape apply(Builder b) { final Outlet in1 = b.add(Source.from(input1)).out(); final Outlet in2 = b.add(Source.from(input2)).out(); final FanInShape2> zip = b.add(Zip.create()); final SinkShape> out = b.add(Sink.foreach(new Procedure>() { @Override public void apply(Pair param) throws Exception { probe.getRef().tell(param, ActorRef.noSender()); } })); b.from(in1).toInlet(zip.in0()); b.from(in2).toInlet(zip.in1()); b.from(zip.out()).to(out); return ClosedShape.getInstance(); } })).run(materializer); List output = Arrays.asList(probe.receiveN(3)); List> expected = Arrays.asList(new Pair("A", 1), new Pair( "B", 2), new Pair("C", 3)); assertEquals(expected, output); } @Test public void mustBeAbleToUseConcat() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList("D", "E", "F"); final Source in1 = Source.from(input1); final Source in2 = Source.from(input2); final Flow flow = Flow.of(String.class); in1.via(flow.concat(in2)).runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); List output = Arrays.asList(probe.receiveN(6)); assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output); } @Test public void mustBeAbleToUsePrepend() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList("D", "E", "F"); final Source in1 = Source.from(input1); final Source in2 = Source.from(input2); final Flow flow = Flow.of(String.class); in2.via(flow.prepend(in1)).runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); List output = Arrays.asList(probe.receiveN(6)); assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output); } @Test public void mustBeAbleToUsePrefixAndTail() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList(1, 2, 3, 4, 5, 6); final Flow, Source>, NotUsed> flow = Flow.of(Integer.class).prefixAndTail(3); CompletionStage, Source>> future = Source.from(input).via(flow).runWith(Sink., Source>>head(), materializer); Pair, Source> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(Arrays.asList(1, 2, 3), result.first()); CompletionStage> tailFuture = result.second().limit(4).runWith(Sink.seq(), materializer); List tailResult = tailFuture.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(Arrays.asList(4, 5, 6), tailResult); } @Test public void mustBeAbleToUseConcatAllWithSources() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList(1, 2, 3); final Iterable input2 = Arrays.asList(4, 5); final List> mainInputs = new ArrayList>(); mainInputs.add(Source.from(input1)); mainInputs.add(Source.from(input2)); final Flow, List, NotUsed> flow = Flow.>create(). flatMapConcat(ConstantFun.>javaIdentityFunction()).grouped(6); CompletionStage> future = Source.from(mainInputs).via(flow) .runWith(Sink.>head(), materializer); List result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(Arrays.asList(1, 2, 3, 4, 5), result); } @Test public void mustBeAbleToUseFlatMapMerge() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); final Iterable input2 = Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19); final Iterable input3 = Arrays.asList(20, 21, 22, 23, 24, 25, 26, 27, 28, 29); final Iterable input4 = Arrays.asList(30, 31, 32, 33, 34, 35, 36, 37, 38, 39); final List> mainInputs = new ArrayList>(); mainInputs.add(Source.from(input1)); mainInputs.add(Source.from(input2)); mainInputs.add(Source.from(input3)); mainInputs.add(Source.from(input4)); final Flow, List, NotUsed> flow = Flow.>create(). flatMapMerge(3, ConstantFun.>javaIdentityFunction()).grouped(60); CompletionStage> future = Source.from(mainInputs).via(flow) .runWith(Sink.>head(), materializer); List result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); final Set set = new HashSet(); for (Integer i: result) { set.add(i); } final Set expected = new HashSet(); for (int i = 0; i < 40; ++i) { expected.add(i); } assertEquals(expected, set); } @Test public void mustBeAbleToUseBuffer() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); final Flow, NotUsed> flow = Flow.of(String.class).buffer(2, OverflowStrategy.backpressure()).grouped(4); final CompletionStage> future = Source.from(input).via(flow) .runWith(Sink.>head(), materializer); List result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(input, result); } @Test public void mustBeAbleToUseWatchTermination() throws Exception { final List input = Arrays.asList("A", "B", "C"); CompletionStage future = Source.from(input) .watchTermination(Keep.right()) .to(Sink.ignore()).run(materializer); assertEquals(Done.getInstance(), future.toCompletableFuture().get(3, TimeUnit.SECONDS)); } @Test public void mustBeAbleToUseConflate() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); final Flow flow = Flow.of(String.class).conflateWithSeed(new Function() { @Override public String apply(String s) throws Exception { return s; } }, new Function2() { @Override public String apply(String aggr, String in) throws Exception { return aggr + in; } }); CompletionStage future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result); final Flow flow2 = Flow.of(String.class).conflate((a, b) -> a + b); CompletionStage future2 = Source.from(input).via(flow2).runFold("", (a, b) -> a + b, materializer); String result2 = future2.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result2); } @Test public void mustBeAbleToUseBatch() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); final Flow flow = Flow.of(String.class).batch(3L, new Function() { @Override public String apply(String s) throws Exception { return s; } }, new Function2() { @Override public String apply(String aggr, String in) throws Exception { return aggr + in; } }); CompletionStage future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result); } @Test public void mustBeAbleToUseBatchWeighted() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); final Flow flow = Flow.of(String.class).batchWeighted(3L, new Function() { @Override public Object apply(String s) throws Exception { return 1L; } }, new Function() { @Override public String apply(String s) throws Exception { return s; } }, new Function2() { @Override public String apply(String aggr, String in) throws Exception { return aggr + in; } }); CompletionStage future = Source.from(input).via(flow).runFold("", (aggr, in) -> aggr + in, materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result); } @Test public void mustBeAbleToUseExpand() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); final Flow flow = Flow.of(String.class).expand(in -> Stream.iterate(in, i -> i).iterator()); final Sink> sink = Sink.head(); CompletionStage future = Source.from(input).via(flow).runWith(sink, materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("A", result); } @Test public void mustBeAbleToUseMapAsync() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("a", "b", "c"); final Flow flow = Flow.of(String.class).mapAsync(4, elem -> CompletableFuture.completedFuture(elem.toUpperCase())); Source.from(input).via(flow).runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("A"); probe.expectMsgEquals("B"); probe.expectMsgEquals("C"); } @Test public void mustBeAbleToRecover() throws Exception { final TestPublisher.ManualProbe publisherProbe = TestPublisher.manualProbe(true,system); final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.fromPublisher(publisherProbe); final Flow flow = Flow.of(Integer.class).map( new Function() { public Integer apply(Integer elem) { if (elem == 2) throw new RuntimeException("ex"); else return elem; } }) .recover(new JavaPartialFunction() { public Integer apply(Throwable elem, boolean isCheck) { if (isCheck) return null; return 0; } }); final CompletionStage future = source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); final PublisherProbeSubscription s = publisherProbe.expectSubscription(); s.sendNext(0); probe.expectMsgEquals(0); s.sendNext(1); probe.expectMsgEquals(1); s.sendNext(2); probe.expectMsgEquals(0); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Test public void mustBeAbleToRecoverWithSource() throws Exception { final TestPublisher.ManualProbe publisherProbe = TestPublisher.manualProbe(true,system); final JavaTestKit probe = new JavaTestKit(system); final Iterable recover = Arrays.asList(55, 0); final Source source = Source.fromPublisher(publisherProbe); final Flow flow = Flow.of(Integer.class).map( new Function() { public Integer apply(Integer elem) { if (elem == 2) throw new RuntimeException("ex"); else return elem; } }) .recoverWith(new JavaPartialFunction>() { public Source apply(Throwable elem, boolean isCheck) { if (isCheck) return null; return Source.from(recover); } }); final CompletionStage future = source.via(flow).runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); final PublisherProbeSubscription s = publisherProbe.expectSubscription(); s.sendNext(0); probe.expectMsgEquals(0); s.sendNext(1); probe.expectMsgEquals(1); s.sendNext(2); probe.expectMsgEquals(55); probe.expectMsgEquals(0); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); } @Test public void mustBeAbleToMaterializeIdentityWithJavaFlow() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); Flow otherFlow = Flow.of(String.class); Flow myFlow = Flow.of(String.class).via(otherFlow); Source.from(input).via(myFlow).runWith(Sink.foreach(new Procedure() { // Scala Future public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }), materializer); probe.expectMsgAllOf("A","B","C"); } @Test public void mustBeAbleToMaterializeIdentityToJavaSink() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); Flow otherFlow = Flow.of(String.class); Sink sink = Flow.of(String.class).to(otherFlow.to(Sink.foreach(new Procedure() { // Scala Future public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }))); Source.from(input).to(sink).run(materializer); probe.expectMsgAllOf("A","B","C"); } @Test public void mustBeAbleToBroadcastEagerCancel() throws Exception { final Sink sink = Sink.fromGraph( GraphDSL.create(new Function, SinkShape>() { @Override public SinkShape apply(Builder b) throws Exception { final UniformFanOutShape broadcast = b.add(Broadcast.create(2, true)); final SinkShape out1 = b.add(Sink.cancelled()); final SinkShape out2 = b.add(Sink.ignore()); b.from(broadcast.out(0)).to(out1); b.from(broadcast.out(1)).to(out2); return new SinkShape(broadcast.in()); } })); final JavaTestKit probe = new JavaTestKit(system); Source source = Source.actorRef(1, OverflowStrategy.dropNew()); final ActorRef actor = source.toMat(sink, Keep.left()).run(materializer); probe.watch(actor); probe.expectTerminated(actor); } @Test public void mustBeAbleToUseZipWith() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList("D", "E", "F"); Source.from(input1).via(Flow.of(String.class).zipWith(Source.from(input2), new Function2() { public String apply(String s1, String s2) { return s1 + "-" + s2; } })).runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("A-D"); probe.expectMsgEquals("B-E"); probe.expectMsgEquals("C-F"); } @Test public void mustBeAbleToUseZip2() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList("D", "E", "F"); Source.from(input1).via(Flow.of(String.class).zip(Source.from(input2))) .runForeach(new Procedure>() { public void apply(Pair elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals(new Pair("A", "D")); probe.expectMsgEquals(new Pair("B", "E")); probe.expectMsgEquals(new Pair("C", "F")); } @Test public void mustBeAbleToUseMerge2() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList("A", "B", "C"); final Iterable input2 = Arrays.asList("D", "E", "F"); Source.from(input1).via(Flow.of(String.class).merge(Source.from(input2))) .runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgAllOf("A", "B", "C", "D", "E", "F"); } @Test public void mustBeAbleToUseInitialTimeout() throws Throwable { try { try { Source. maybe().via(Flow.of(Integer.class).initialTimeout(Duration.create(1, "second"))) .runWith(Sink. head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS); org.junit.Assert.fail("A TimeoutException was expected"); } catch (ExecutionException e) { throw e.getCause(); } } catch (TimeoutException e) { // expected } } @Test public void mustBeAbleToUseCompletionTimeout() throws Throwable { try { try { Source. maybe().via(Flow.of(Integer.class).completionTimeout(Duration.create(1, "second"))) .runWith(Sink. head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS); org.junit.Assert.fail("A TimeoutException was expected"); } catch (ExecutionException e) { throw e.getCause(); } } catch (TimeoutException e) { // expected } } @Test public void mustBeAbleToUseIdleTimeout() throws Throwable { try { try { Source. maybe().via(Flow.of(Integer.class).idleTimeout(Duration.create(1, "second"))) .runWith(Sink. head(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS); org.junit.Assert.fail("A TimeoutException was expected"); } catch (ExecutionException e) { throw e.getCause(); } } catch (TimeoutException e) { // expected } } @Test public void mustBeAbleToUseKeepAlive() throws Exception { Integer result = Source.maybe() .via(Flow.of(Integer.class) .keepAlive(Duration.create(1, "second"), (Creator) () -> 0) ) .takeWithin(Duration.create(1500, "milliseconds")) .runWith(Sink.head(), materializer) .toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals((Object) 0, result); } @Test public void shouldBePossibleToCreateFromFunction() throws Exception { List out = Source.range(0, 2).via(Flow.fromFunction((Integer x) -> x + 1)) .runWith(Sink.seq(), materializer).toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(Arrays.asList(1, 2, 3), out); } public void mustSuitablyOverrideAttributeHandlingMethods() { @SuppressWarnings("unused") final Flow f = Flow.of(Integer.class).withAttributes(Attributes.name("")).addAttributes(Attributes.asyncBoundary()).named(""); } }