/** * Copyright (C) 2014 Typesafe Inc. */ package akka.stream.javadsl; import akka.actor.ActorRef; import akka.actor.Cancellable; import akka.dispatch.Foreach; import akka.dispatch.Futures; import akka.dispatch.OnSuccess; import akka.japi.JavaPartialFunction; import akka.japi.Pair; import akka.stream.OverflowStrategy; import akka.stream.StreamTest; import akka.stream.stage.*; import akka.japi.function.*; import akka.stream.testkit.AkkaSpec; import akka.testkit.JavaTestKit; import org.junit.ClassRule; import org.junit.Test; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; import scala.runtime.BoxedUnit; import scala.util.Try; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; @SuppressWarnings("serial") public class SourceTest extends StreamTest { public SourceTest() { super(actorSystemResource); } @ClassRule public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("SourceTest", 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); ints.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; } }).runFold("", new Function2() { public String apply(String acc, String elem) { return acc + elem; } }, materializer).foreach(new Foreach() { // Scala Future public void each(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, system.dispatcher()); probe.expectMsgEquals("de"); } @Test public void mustBeAbleToUseVoidTypeInForeach() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable input = Arrays.asList("a", "b", "c"); Source ints = Source.from(input); Future completion = ints.runForeach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); completion.onSuccess(new OnSuccess() { @Override public void onSuccess(BoxedUnit elem) throws Throwable { probe.getRef().tell(String.valueOf(elem), ActorRef.noSender()); } }, system.dispatcher()); probe.expectMsgEquals("a"); probe.expectMsgEquals("b"); probe.expectMsgEquals("c"); probe.expectMsgEquals("()"); } @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 Source.from(input).transform(new Creator>() { @Override public PushPullStage create() throws Exception { return new StatefulStage() { int sum = 0; int count = 0; @Override public StageState initial() { return new StageState() { @Override public SyncDirective onPush(Integer element, Context ctx) { sum += element; count += 1; if (count == 4) { return emitAndFinish(Arrays.asList(element, element, sum).iterator(), ctx); } else { return emit(Arrays.asList(element, element).iterator(), ctx); } } }; } @Override public TerminationDirective onUpstreamFinish(Context ctx) { return terminationEmit(Collections.singletonList(sum).iterator(), ctx); } }; } }).runForeach(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); } @Test public void mustBeAbleToUseGroupBy() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee"); Source.from(input).groupBy(new Function() { public String apply(String elem) { return elem.substring(0, 1); } }).runForeach(new Procedure>>() { @Override public void apply(final Pair> pair) throws Exception { pair.second().runForeach(new Procedure() { @Override public void apply(String elem) throws Exception { 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 Iterable input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F"); Source.from(input).splitWhen(new Predicate() { public boolean test(String elem) { return elem.equals("."); } }).runForeach(new Procedure>() { @Override public void apply(Source subStream) throws Exception { subStream.filter(new Predicate() { @Override public boolean test(String elem) { return !elem.equals("."); } }).grouped(10).runForeach(new Procedure>() { @Override public void apply(List chunk) throws Exception { 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 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); in1.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 mustBeAbleToUseCallableInput() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input1 = Arrays.asList(4, 3, 2, 1, 0); final Creator> input = new Creator>() { @Override public Iterator create() { return input1.iterator(); } }; Source.fromIterator(input).runForeach(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 Iterable input = Arrays.asList("A", "B", "C"); Source.from(input).runWith(Sink.onComplete(new Procedure>() { @Override public void apply(Try param) throws Exception { probe.getRef().tell(param.get(), ActorRef.noSender()); } }), materializer); probe.expectMsgClass(BoxedUnit.class); } @Test public void mustBeAbleToUseOnCompleteError() { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("A", "B", "C"); Source.from(input).map(new Function() { public String apply(String arg0) throws Exception { throw new RuntimeException("simulated err"); } }).runWith(Sink.head(), materializer).onComplete(new OnSuccess>() { @Override public void onSuccess(Try e) throws Throwable { if (e == null) { probe.getRef().tell("done", ActorRef.noSender()); } else { probe.getRef().tell(e.failed().get().getMessage(), ActorRef.noSender()); } } }, system.dispatcher()); probe.expectMsgEquals("simulated err"); } @Test public void mustBeAbleToUseToFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("A", "B", "C"); Future future = Source.from(input).runWith(Sink.head(), 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 Iterable input = Arrays.asList(1, 2, 3, 4, 5, 6); Future, Source>> future = Source.from(input).prefixAndTail(3) .runWith(Sink., Source>>head(), materializer); Pair, Source> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals(Arrays.asList(1, 2, 3), result.first()); Future> tailFuture = result.second().grouped(4).runWith(Sink.>head(), materializer); List tailResult = Await.result(tailFuture, probe.dilated(FiniteDuration.create(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)); Future> future = Source.from(mainInputs) .flatten(akka.stream.javadsl.FlattenStrategy.concat()).grouped(6) .runWith(Sink.>head(), 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 = Source.from(input).buffer(2, OverflowStrategy.backpressure()).grouped(4) .runWith(Sink.>head(), 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 = Source.from(input).conflate(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; } }).runFold("", new Function2() { @Override public String apply(String aggr, String in) throws Exception { return aggr + in; } }, materializer); String result = Await.result(future, probe.dilated(FiniteDuration.create(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"); Future future = Source.from(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); } }).runWith(Sink.head(), 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); Source tickSource = Source.from(FiniteDuration.create(1, TimeUnit.SECONDS), FiniteDuration.create(500, TimeUnit.MILLISECONDS), "tick"); Cancellable cancellable = tickSource.to(Sink.foreach(new Procedure() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } })).run(materializer); probe.expectNoMsg(FiniteDuration.create(600, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); } @Test public void mustBeAbleToUseMapFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("a", "b", "c"); Source.from(input).mapAsync(4, new Function>() { public Future apply(String elem) { return Futures.successful(elem.toUpperCase()); } }).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 mustWorkFromFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable input = Arrays.asList("A", "B", "C"); Future future1 = Source.from(input).runWith(Sink.head(), materializer); Future future2 = Source.from(future1).runWith(Sink.head(), materializer); String result = Await.result(future2, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS))); assertEquals("A", result); } @Test public void mustRepeat() throws Exception { final Future> f = Source.repeat(42).grouped(10000).runWith(Sink.> head(), materializer); final List result = Await.result(f, FiniteDuration.create(3, TimeUnit.SECONDS)); assertEquals(result.size(), 10000); for (Integer i: result) assertEquals(i, (Integer) 42); } @Test public void mustBeAbleToUseActorRefSource() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source actorRefSource = Source.actorRef(10, OverflowStrategy.fail()); final ActorRef ref = actorRefSource.to(Sink.foreach(new Procedure() { public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } })).run(materializer); ref.tell(1, ActorRef.noSender()); probe.expectMsgEquals(1); ref.tell(2, ActorRef.noSender()); probe.expectMsgEquals(2); } @Test public void mustBeAbleToUseDropWhile() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList(0, 1, 2, 3)).dropWhile (new Predicate() { public boolean test(Integer elem) { return elem < 2; } }); final Future future = source.runWith(Sink.foreach(new Procedure() { // Scala Future public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }), materializer); probe.expectMsgEquals(2); probe.expectMsgEquals(3); Await.ready(future, Duration.apply(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)).takeWhile (new Predicate() { public boolean test(Integer elem) { return elem < 2; } }); final Future future = source.runWith(Sink.foreach(new Procedure() { // Scala Future public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }), materializer); probe.expectMsgEquals(0); probe.expectMsgEquals(1); FiniteDuration duration = Duration.apply(200, TimeUnit.MILLISECONDS); probe.expectNoMsg(duration); Await.ready(future, duration); } @Test public void mustBeAbleToRecover() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source source = Source.from(Arrays.asList(0, 1, 2, 3)).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 Future future = source.runWith(Sink.foreach(new Procedure() { public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }), materializer); probe.expectMsgEquals(0); probe.expectMsgEquals(1); probe.expectMsgEquals(0); Await.ready(future, Duration.apply(200, TimeUnit.MILLISECONDS)); } }