package akka.stream.javadsl; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.dispatch.Futures; import akka.dispatch.Mapper; import akka.japi.*; import akka.stream.*; import akka.stream.testkit.AkkaSpec; import akka.testkit.JavaTestKit; import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; import org.reactivestreams.Publisher; import scala.Option; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.FiniteDuration; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; @Ignore public class FlowTest { @ClassRule public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest", AkkaSpec.testConf()); final ActorSystem system = actorSystemResource.getSystem(); final MaterializerSettings settings = MaterializerSettings.create(system.settings().config()).withDispatcher("akka.test.stream-dispatcher"); final FlowMaterializer materializer = FlowMaterializer.create(settings, system); @Test public void mustBeAbleToUseSimpleOperators() { final JavaTestKit probe = new JavaTestKit(system); final String[] lookup = { "a", "b", "c", "d", "e", "f" }; final java.util.Iterator input = Arrays.asList(0, 1, 2, 3, 4, 5).iterator(); Flow.create(input).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"); } }).grouped(2).mapConcat(new Function, java.util.List>() { public java.util.List apply(java.util.List elem) { return elem; } }).groupedWithin(100, FiniteDuration.create(50, TimeUnit.MILLISECONDS)) .mapConcat(new Function, java.util.List>() { public java.util.List apply(java.util.List elem) { return elem; } }).fold("", new Function2() { public String apply(String acc, String elem) { return acc + elem; } }).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("de"); } @Test public void mustBeAbleToUseVoidTypeInForeach() { final JavaTestKit probe = new JavaTestKit(system); final java.util.Iterator input = Arrays.asList("a", "b", "c").iterator(); Future fut = Flow.create(input).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); fut.map(new Mapper() { public String apply(Void elem) { probe.getRef().tell(String.valueOf(elem), ActorRef.noSender()); return String.valueOf(elem); } }, system.dispatcher()); probe.expectMsgEquals("a"); probe.expectMsgEquals("b"); probe.expectMsgEquals("c"); probe.expectMsgEquals("null"); } @Test public void mustBeAbleToUseTransform() { final JavaTestKit probe = new JavaTestKit(system); final JavaTestKit probe2 = new JavaTestKit(system); final java.lang.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 Flow.create(input).transform("publish", new Creator>() { @Override public Transformer create() throws Exception { return new Transformer() { int sum = 0; int count = 0; @Override public scala.collection.immutable.Seq onNext(Integer element) { sum += element; count += 1; return Util.immutableSeq(new Integer[]{element, element}); } @Override public boolean isComplete() { return count == 4; } @Override public scala.collection.immutable.Seq onTermination(Option e) { return Util.immutableSingletonSeq(sum); } @Override public void cleanup() { probe2.getRef().tell("cleanup", ActorRef.noSender()); } }; } }).foreach(new Procedure() { public void apply(Integer 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); probe2.expectMsgEquals("cleanup"); } @Test public void mustBeAbleToUseTransformRecover() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList(0, 1, 2, 3, 4, 5); Flow.create(input).map(new Function() { public Integer apply(Integer elem) { if (elem == 4) throw new IllegalArgumentException("4 not allowed"); else return elem + elem; } }).transform("publish", new Creator>() { @Override public Transformer create() throws Exception { return new Transformer() { @Override public scala.collection.immutable.Seq onNext(Integer element) { return Util.immutableSingletonSeq(element.toString()); } @Override public scala.collection.immutable.Seq onTermination(Option e) { if (e.isEmpty()) { return Util.immutableSeq(new String[0]); } else { return Util.immutableSingletonSeq(e.get().getMessage()); } } @Override public void onError(Throwable e) { } @Override public boolean isComplete() { return false; } @Override public void cleanup() { } }; } }).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("0"); probe.expectMsgEquals("2"); probe.expectMsgEquals("4"); probe.expectMsgEquals("6"); probe.expectMsgEquals("4 not allowed"); } @Test public void mustBeAbleToUseGroupBy() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee"); Flow.create(input).groupBy(new Function() { public String apply(String elem) { return elem.substring(0, 1); } }).foreach(new Procedure>>() { public void apply(final Pair> pair) { Flow.create(pair.second()).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(new Pair(pair.first(), elem), ActorRef.noSender()); } }, materializer); } }, materializer); Map> grouped = new HashMap>(); for (Object o : probe.receiveN(5)) { @SuppressWarnings("unchecked") Pair p = (Pair) o; List g = grouped.get(p.first()); if (g == null) g = new ArrayList(); g.add(p.second()); grouped.put(p.first(), g); } assertEquals(Arrays.asList("Aaa", "Abb"), grouped.get("A")); } @Test public void mustBeAbleToUseSplitWhen() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("A", "B", "C", "\n", "D", "\n", "E", "F"); Flow.create(input).splitWhen(new Predicate() { public boolean test(String elem) { return elem.equals("\n"); } }).foreach(new Procedure>() { public void apply(Publisher subPublisher) { Flow.create(subPublisher).filter(new Predicate() { public boolean test(String elem) { return !elem.equals("\n"); } }).grouped(10).foreach(new Procedure>() { public void apply(List chunk) { probe.getRef().tell(chunk, ActorRef.noSender()); } }, materializer); } }, materializer); for (Object o : probe.receiveN(3)) { @SuppressWarnings("unchecked") List chunk = (List) o; if (chunk.get(0).equals("A")) assertEquals(Arrays.asList("A", "B", "C"), chunk); else if (chunk.get(0).equals("D")) assertEquals(Arrays.asList("D"), chunk); else if (chunk.get(0).equals("E")) assertEquals(Arrays.asList("E", "F"), chunk); else assertEquals("[A, B, C] or [D] or [E, F]", chunk); } } @Test public void mustBeAbleToUseMerge() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input1 = Arrays.asList("A", "B", "C"); final java.lang.Iterable input2 = Arrays.asList("D", "E", "F"); Flow.create(input1).merge(Flow.create(input2).toPublisher(materializer)).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); Set output = new HashSet(Arrays.asList(probe.receiveN(6))); assertEquals(new HashSet(Arrays.asList("A", "B", "C", "D", "E", "F")), output); } @Test public void mustBeAbleToUseZip() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input1 = Arrays.asList("A", "B", "C"); final java.lang.Iterable input2 = Arrays.asList(1, 2, 3); Flow.create(input1).zip(Flow.create(input2).toPublisher(materializer)) .foreach(new Procedure>() { public void apply(Pair elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); List output = Arrays.asList(probe.receiveN(3)); @SuppressWarnings("unchecked") 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 java.lang.Iterable input1 = Arrays.asList("A", "B", "C"); final java.lang.Iterable input2 = Arrays.asList("D", "E", "F"); Flow.create(input1).concat(Flow.create(input2).toPublisher(materializer)).foreach(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 mustBeAbleToUseCallableInput() { final JavaTestKit probe = new JavaTestKit(system); final Callable input = new Callable() { int countdown = 5; @Override public Integer call() { if (countdown == 0) return null; else { countdown -= 1; return countdown; } } }; Flow.create(input).foreach(new Procedure() { public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); List output = Arrays.asList(probe.receiveN(5)); assertEquals(Arrays.asList(4, 3, 2, 1, 0), output); probe.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS)); } @Test public void mustBeAbleToUseOnCompleteSuccess() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("A", "B", "C"); Flow.create(input).onComplete(new OnCompleteCallback() { @Override public void onComplete(Throwable e) { probe.getRef().tell((e == null) ? "done" : e, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("done"); } @Test public void mustBeAbleToUseOnCompleteError() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("A", "B", "C"); Flow.create(input).map(new Function() { public String apply(String arg0) throws Exception { throw new RuntimeException("simulated err"); } }).onComplete(new OnCompleteCallback() { @Override public void onComplete(Throwable e) { if (e == null) probe.getRef().tell("done", ActorRef.noSender()); else probe.getRef().tell(e.getMessage(), ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("simulated err"); } @Test public void mustBeAbleToUseToFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("A", "B", "C"); Future future = Flow.create(input).toFuture(materializer); String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals("A", result); } @Test public void mustBeAbleToUsePrefixAndTail() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList(1, 2, 3, 4, 5, 6); Future, Publisher>> future = Flow.create(input).prefixAndTail(3).toFuture(materializer); Pair, Publisher> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals(Arrays.asList(1, 2, 3), result.first()); Future> tailFuture = Flow.create(result.second()).grouped(4).toFuture(materializer); List tailResult = Await.result(tailFuture, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals(Arrays.asList(4, 5, 6), tailResult); } @Test public void mustBeAbleToUseConcatAll() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input1 = Arrays.asList(1, 2, 3); final java.lang.Iterable input2 = Arrays.asList(4, 5); final List> mainInputs = Arrays.asList(Flow.create(input1).toPublisher(materializer), Flow .create(input2).toPublisher(materializer)); Future> future = Flow.create(mainInputs). flatten(FlattenStrategy. concat()) .grouped(6).toFuture(materializer); List result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals(Arrays.asList(1, 2, 3, 4, 5), result); } @Test public void mustBeAbleToUseBuffer() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); Future> future = Flow.create(input).buffer(2, OverflowStrategy.backpressure()).grouped(4) .toFuture(materializer); List result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals(input, result); } @Test public void mustBeAbleToUseConflate() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); Future future = Flow.create(input).conflate(new Function() { @Override public String apply(String s) throws Exception { return s; } }, new Function2() { @Override public String apply(String in, String aggr) throws Exception { return in; } }).fold("", new Function2() { @Override public String apply(String aggr, String in) throws Exception { return in; } }).toFuture(materializer); String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals("C", result); } @Test public void mustBeAbleToUseExpand() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List input = Arrays.asList("A", "B", "C"); Future future = Flow.create(input).expand(new Function() { @Override public String apply(String in) throws Exception { return in; } }, new Function>() { @Override public Pair apply(String in) throws Exception { return new Pair(in, in); } }).toFuture(materializer); String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals("A", result); } @Test public void mustProduceTicks() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Callable tick = new Callable() { private int count = 1; @Override public String call() { return "tick-" + (count++); } }; Flow.create(FiniteDuration.create(1, TimeUnit.SECONDS), FiniteDuration.create(500, TimeUnit.MILLISECONDS), tick) .foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectNoMsg(FiniteDuration.create(600, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick-1"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick-2"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); } @Test public void mustBeAbleToUseMapFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("a", "b", "c"); Flow.create(input).mapFuture(new Function>() { public Future apply(String elem) { return Futures.successful(elem.toUpperCase()); } }).foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("A"); probe.expectMsgEquals("B"); probe.expectMsgEquals("C"); } }