2014-04-23 10:05:09 +02:00
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.dispatch.Foreach;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.dispatch.Futures;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.dispatch.OnSuccess;
|
|
|
|
|
import akka.japi.Pair;
|
|
|
|
|
import akka.japi.Util;
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.stream.*;
|
2014-10-03 17:33:14 +02:00
|
|
|
import akka.stream.javadsl.japi.*;
|
2014-08-22 11:42:05 +02:00
|
|
|
import akka.stream.testkit.AkkaSpec;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
2014-04-23 10:05:09 +02:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
2014-07-22 12:21:53 +02:00
|
|
|
import org.reactivestreams.Publisher;
|
2014-05-12 16:45:30 +02:00
|
|
|
import scala.Option;
|
2014-10-03 17:33:14 +02:00
|
|
|
import scala.collection.immutable.Seq;
|
2014-04-23 10:05:09 +02:00
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
2014-10-03 17:33:14 +02:00
|
|
|
import scala.concurrent.duration.Duration;
|
2014-04-23 10:05:09 +02:00
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
2014-10-03 17:33:14 +02:00
|
|
|
import scala.runtime.BoxedUnit;
|
|
|
|
|
import scala.util.Try;
|
2014-05-23 13:52:39 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
public class FlowTest {
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
2014-10-27 14:35:41 +01:00
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest",
|
|
|
|
|
AkkaSpec.testConf());
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
2014-10-03 17:33:14 +02:00
|
|
|
final MaterializerSettings settings = new MaterializerSettings(2, 4, 2, 4, "akka.test.stream-dispatcher");
|
2014-10-27 14:35:41 +01:00
|
|
|
final FlowMaterializer materializer = FlowMaterializer.create(settings, system);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSimpleOperators() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-27 14:35:41 +01:00
|
|
|
final String[] lookup = { "a", "b", "c", "d", "e", "f" };
|
2014-04-23 10:05:09 +02:00
|
|
|
final java.util.Iterator<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5).iterator();
|
2014-10-03 17:33:14 +02:00
|
|
|
final Source<Integer> ints = Source.from(input);
|
|
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
ints.drop(2).take(3).takeWithin(FiniteDuration.create(10, TimeUnit.SECONDS)).map(new Function<Integer, String>() {
|
|
|
|
|
public String apply(Integer elem) {
|
|
|
|
|
return lookup[elem];
|
|
|
|
|
}
|
|
|
|
|
}).filter(new Predicate<String>() {
|
2014-10-03 17:33:14 +02:00
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return !elem.equals("c");
|
|
|
|
|
}
|
|
|
|
|
}).grouped(2).mapConcat(new Function<java.util.List<String>, java.util.List<String>>() {
|
|
|
|
|
public java.util.List<String> apply(java.util.List<String> elem) {
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
|
|
|
|
}).groupedWithin(100, FiniteDuration.create(50, TimeUnit.MILLISECONDS))
|
2014-10-27 14:35:41 +01:00
|
|
|
.mapConcat(new Function<java.util.List<String>, java.util.List<String>>() {
|
|
|
|
|
public java.util.List<String> apply(java.util.List<String> elem) {
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
|
|
|
|
}).fold("", new Function2<String, String, String>() {
|
|
|
|
|
public String apply(String acc, String elem) {
|
|
|
|
|
return acc + elem;
|
|
|
|
|
}
|
|
|
|
|
}, materializer).foreach(new Foreach<String>() { // Scala Future
|
|
|
|
|
public void each(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, system.dispatcher());
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("de");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 16:15:36 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseVoidTypeInForeach() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final java.util.Iterator<String> input = Arrays.asList("a", "b", "c").iterator();
|
2014-10-03 17:33:14 +02:00
|
|
|
Source<String> ints = Source.from(input);
|
2014-08-15 15:37:09 +02:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<BoxedUnit> completion = ints.foreach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
completion.onSuccess(new OnSuccess<BoxedUnit>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void onSuccess(BoxedUnit elem) throws Throwable {
|
2014-05-13 16:15:36 +02:00
|
|
|
probe.getRef().tell(String.valueOf(elem), ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, system.dispatcher());
|
2014-05-13 16:15:36 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("a");
|
|
|
|
|
probe.expectMsgEquals("b");
|
|
|
|
|
probe.expectMsgEquals("c");
|
2014-10-03 17:33:14 +02:00
|
|
|
probe.expectMsgEquals("()");
|
2014-05-13 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
2014-04-23 10:05:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseTransform() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final JavaTestKit probe2 = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7);
|
2014-04-23 10:05:09 +02:00
|
|
|
// duplicate each element, stop after 4 elements, and emit sum to the end
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).transform("publish", new Creator<Transformer<Integer, Integer>>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
@Override
|
2014-08-22 11:42:05 +02:00
|
|
|
public Transformer<Integer, Integer> create() throws Exception {
|
|
|
|
|
return new Transformer<Integer, Integer>() {
|
|
|
|
|
int sum = 0;
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public scala.collection.immutable.Seq<Integer> onNext(Integer element) {
|
|
|
|
|
sum += element;
|
|
|
|
|
count += 1;
|
2014-10-27 14:35:41 +01:00
|
|
|
return Util.immutableSeq(new Integer[] { element, element });
|
2014-08-22 11:42:05 +02:00
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean isComplete() {
|
|
|
|
|
return count == 4;
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public scala.collection.immutable.Seq<Integer> onTermination(Option<Throwable> e) {
|
|
|
|
|
return Util.immutableSingletonSeq(sum);
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public void cleanup() {
|
|
|
|
|
probe2.getRef().tell("cleanup", ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
}).foreach(new Procedure<Integer>() {
|
|
|
|
|
public void apply(Integer elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
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);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5);
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).map(new Function<Integer, Integer>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public Integer apply(Integer elem) {
|
2014-10-03 17:33:14 +02:00
|
|
|
if (elem == 4) {
|
2014-04-23 10:05:09 +02:00
|
|
|
throw new IllegalArgumentException("4 not allowed");
|
2014-10-03 17:33:14 +02:00
|
|
|
} else {
|
2014-04-23 10:05:09 +02:00
|
|
|
return elem + elem;
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-22 11:42:05 +02:00
|
|
|
}).transform("publish", new Creator<Transformer<Integer, String>>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
@Override
|
2014-08-22 11:42:05 +02:00
|
|
|
public Transformer<Integer, String> create() throws Exception {
|
|
|
|
|
return new Transformer<Integer, String>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public scala.collection.immutable.Seq<String> onNext(Integer element) {
|
|
|
|
|
return Util.immutableSingletonSeq(element.toString());
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public scala.collection.immutable.Seq<String> onTermination(Option<Throwable> e) {
|
|
|
|
|
if (e.isEmpty()) {
|
|
|
|
|
return Util.immutableSeq(new String[0]);
|
|
|
|
|
} else {
|
|
|
|
|
return Util.immutableSingletonSeq(e.get().getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable e) {
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean isComplete() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-08-22 11:42:05 +02:00
|
|
|
@Override
|
|
|
|
|
public void cleanup() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
}).foreach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
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);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee");
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).groupBy(new Function<String, String>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public String apply(String elem) {
|
|
|
|
|
return elem.substring(0, 1);
|
|
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}).foreach(new Procedure<Pair<String, Source<String>>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(final Pair<String, Source<String>> pair) throws Exception {
|
2014-10-03 17:33:14 +02:00
|
|
|
pair.second().foreach(new Procedure<String>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(String elem) throws Exception {
|
2014-05-13 16:15:36 +02:00
|
|
|
probe.getRef().tell(new Pair<String, String>(pair.first(), elem), ActorRef.noSender());
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
Map<String, List<String>> grouped = new HashMap<String, List<String>>();
|
|
|
|
|
for (Object o : probe.receiveN(5)) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
Pair<String, String> p = (Pair<String, String>) o;
|
2014-05-13 16:15:36 +02:00
|
|
|
List<String> g = grouped.get(p.first());
|
2014-10-03 17:33:14 +02:00
|
|
|
if (g == null) {
|
2014-04-23 10:05:09 +02:00
|
|
|
g = new ArrayList<String>();
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-05-13 16:15:36 +02:00
|
|
|
g.add(p.second());
|
|
|
|
|
grouped.put(p.first(), g);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(Arrays.asList("Aaa", "Abb"), grouped.get("A"));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSplitWhen() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", "\n", "D", "\n", "E", "F");
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).splitWhen(new Predicate<String>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return elem.equals("\n");
|
|
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}).foreach(new Procedure<Source<String>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public void apply(Source<String> subStream) throws Exception {
|
|
|
|
|
subStream.filter(new Predicate<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return !elem.equals("\n");
|
|
|
|
|
}
|
|
|
|
|
}).grouped(10).foreach(new Procedure<List<String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(List<String> chunk) throws Exception {
|
|
|
|
|
probe.getRef().tell(chunk, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
for (Object o : probe.receiveN(3)) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
List<String> chunk = (List<String>) o;
|
2014-10-03 17:33:14 +02:00
|
|
|
if (chunk.get(0).equals("A")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("A", "B", "C"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else if (chunk.get(0).equals("D")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("D"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else if (chunk.get(0).equals("E")) {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals(Arrays.asList("E", "F"), chunk);
|
2014-10-03 17:33:14 +02:00
|
|
|
} else {
|
2014-04-23 10:05:09 +02:00
|
|
|
assertEquals("[A, B, C] or [D] or [E, F]", chunk);
|
|
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 17:33:14 +02:00
|
|
|
public <In, Out> Creator<Transformer<In, Out>> op() {
|
|
|
|
|
return new akka.stream.javadsl.japi.Creator<Transformer<In, Out>>() {
|
2014-10-27 14:35:41 +01:00
|
|
|
@Override
|
|
|
|
|
public Transformer<In, Out> create() throws Exception {
|
2014-10-03 17:33:14 +02:00
|
|
|
return new Transformer<In, Out>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Seq<Out> onNext(In element) {
|
|
|
|
|
return Util.immutableSeq(Collections.singletonList((Out) element)); // TODO needs helpers
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2014-10-03 17:33:14 +02:00
|
|
|
public void mustBeAbleToUseMerge() throws Exception {
|
|
|
|
|
final Flow<String, String> f1 = Flow.of(String.class).transform("f1", this.<String, String>op()); // javadsl
|
|
|
|
|
final Flow<String, String> f2 = Flow.of(String.class).transform("f2", this.<String, String>op()); // javadsl
|
|
|
|
|
final Flow<String, String> f3 = Flow.of(String.class).transform("f2", this.<String, String>op()); // javadsl
|
|
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
final Source<String> in1 = Source.from(Arrays.asList("a", "b", "c"));
|
|
|
|
|
final Source<String> in2 = Source.from(Arrays.asList("d", "e", "f"));
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
final KeyedSink<String, Publisher<String>> publisher = Sink.publisher();
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
// this is red in intellij, but actually valid, scalac generates bridge methods for Java, so inference *works*
|
2014-10-27 14:35:41 +01:00
|
|
|
final Merge<String> merge = Merge.<String> create();
|
|
|
|
|
MaterializedMap m = FlowGraph.builder().addEdge(in1, f1, merge).addEdge(in2, f2, merge)
|
|
|
|
|
.addEdge(merge, f3, publisher).build().run(materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
// collecting
|
2014-10-17 14:05:50 +02:00
|
|
|
final Publisher<String> pub = m.get(publisher);
|
2014-10-27 14:35:41 +01:00
|
|
|
final Future<List<String>> all = Source.from(pub).grouped(100).runWith(Sink.<List<String>> future(), materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
final List<String> result = Await.result(all, Duration.apply(200, TimeUnit.MILLISECONDS));
|
|
|
|
|
assertEquals(new HashSet<Object>(Arrays.asList("a", "b", "c", "d", "e", "f")), new HashSet<String>(result));
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-20 20:53:03 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZip() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3);
|
|
|
|
|
|
|
|
|
|
final Source<String> in1 = Source.from(input1);
|
|
|
|
|
final Source<Integer> in2 = Source.from(input2);
|
|
|
|
|
final Zip<String, Integer> zip = Zip.create();
|
2014-10-27 14:35:41 +01:00
|
|
|
final KeyedSink<Pair<String, Integer>, Future<BoxedUnit>> out = Sink
|
|
|
|
|
.foreach(new Procedure<Pair<String, Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Pair<String, Integer> param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-10-20 20:53:03 +02:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
FlowGraph.builder().addEdge(in1, zip.left()).addEdge(in2, zip.right()).addEdge(zip.out(), out).run(materializer);
|
2014-10-20 20:53:03 +02:00
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(3));
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2014-10-27 14:35:41 +01:00
|
|
|
List<Pair<String, Integer>> expected = Arrays.asList(new Pair<String, Integer>("A", 1), new Pair<String, Integer>(
|
|
|
|
|
"B", 2), new Pair<String, Integer>("C", 3));
|
2014-10-20 20:53:03 +02:00
|
|
|
assertEquals(expected, output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseUnzip() {
|
|
|
|
|
final JavaTestKit probe1 = new JavaTestKit(system);
|
|
|
|
|
final JavaTestKit probe2 = new JavaTestKit(system);
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2014-10-27 14:35:41 +01:00
|
|
|
final List<Pair<String, Integer>> input = Arrays.asList(new Pair<String, Integer>("A", 1),
|
|
|
|
|
new Pair<String, Integer>("B", 2), new Pair<String, Integer>("C", 3));
|
2014-10-20 20:53:03 +02:00
|
|
|
|
|
|
|
|
final Iterable<String> expected1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<Integer> expected2 = Arrays.asList(1, 2, 3);
|
|
|
|
|
|
|
|
|
|
final Source<Pair<String, Integer>> in = Source.from(input);
|
|
|
|
|
final Unzip<String, Integer> unzip = Unzip.create();
|
|
|
|
|
|
|
|
|
|
final KeyedSink<String, Future<BoxedUnit>> out1 = Sink.foreach(new Procedure<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(String param) throws Exception {
|
|
|
|
|
probe1.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
final KeyedSink<Integer, Future<BoxedUnit>> out2 = Sink.foreach(new Procedure<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Integer param) throws Exception {
|
|
|
|
|
probe2.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
FlowGraph.builder().addEdge(in, unzip.in()).addEdge(unzip.left(), out1).addEdge(unzip.right(), out2)
|
|
|
|
|
.run(materializer);
|
2014-10-20 20:53:03 +02:00
|
|
|
|
|
|
|
|
List<Object> output1 = Arrays.asList(probe1.receiveN(3));
|
|
|
|
|
List<Object> output2 = Arrays.asList(probe2.receiveN(3));
|
|
|
|
|
assertEquals(expected1, output1);
|
|
|
|
|
assertEquals(expected2, output2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConcat() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
|
|
|
|
final Source<String> in1 = Source.from(input1);
|
|
|
|
|
final Source<String> in2 = Source.from(input2);
|
|
|
|
|
|
|
|
|
|
in1.concat(in2).foreach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
|
|
|
|
|
|
|
|
|
List<Object> output = Arrays.asList(probe.receiveN(6));
|
|
|
|
|
assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 10:05:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseCallableInput() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-03 17:33:14 +02:00
|
|
|
final akka.stream.javadsl.japi.Creator<akka.japi.Option<Integer>> input = new akka.stream.javadsl.japi.Creator<akka.japi.Option<Integer>>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
int countdown = 5;
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-10-03 17:33:14 +02:00
|
|
|
public akka.japi.Option<Integer> create() {
|
|
|
|
|
if (countdown == 0) {
|
|
|
|
|
return akka.japi.Option.none();
|
|
|
|
|
} else {
|
2014-04-23 10:05:09 +02:00
|
|
|
countdown -= 1;
|
2014-10-03 17:33:14 +02:00
|
|
|
return akka.japi.Option.option(countdown);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).foreach(new Procedure<Integer>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public void apply(Integer elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
List<Object> 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);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
2014-04-23 10:05:09 +02:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
Source.from(input).runWith(Sink.<String> onComplete(new Procedure<BoxedUnit>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(BoxedUnit param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}), materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgClass(BoxedUnit.class);
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseOnCompleteError() {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
2014-10-03 17:33:14 +02:00
|
|
|
|
|
|
|
|
Source.from(input).map(new Function<String, String>() {
|
2014-04-23 10:05:09 +02:00
|
|
|
public String apply(String arg0) throws Exception {
|
|
|
|
|
throw new RuntimeException("simulated err");
|
|
|
|
|
}
|
2014-10-27 14:35:41 +01:00
|
|
|
}).runWith(Sink.<String> future(), materializer).onComplete(new OnSuccess<Try<String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess(Try<String> e) throws Throwable {
|
|
|
|
|
if (e == null) {
|
|
|
|
|
probe.getRef().tell("done", ActorRef.noSender());
|
|
|
|
|
} else {
|
|
|
|
|
probe.getRef().tell(e.failed().get().getMessage(), ActorRef.noSender());
|
2014-10-03 17:33:14 +02:00
|
|
|
}
|
2014-10-27 14:35:41 +01:00
|
|
|
}
|
|
|
|
|
}, system.dispatcher());
|
2014-04-23 10:05:09 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("simulated err");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseToFuture() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<String> future = Source.from(input).runWith(Sink.<String> future(), materializer);
|
2014-04-23 10:05:09 +02:00
|
|
|
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
assertEquals("A", result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 14:21:15 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePrefixAndTail() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6);
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<Pair<List<Integer>, Source<Integer>>> future = Source.from(input).prefixAndTail(3)
|
|
|
|
|
.runWith(Sink.<Pair<List<Integer>, Source<Integer>>> future(), materializer);
|
|
|
|
|
Pair<List<Integer>, Source<Integer>> result = Await.result(future,
|
|
|
|
|
probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
2014-05-16 14:21:15 +02:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3), result.first());
|
|
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<List<Integer>> tailFuture = result.second().grouped(4).runWith(Sink.<List<Integer>> future(), materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
List<Integer> tailResult = Await.result(tailFuture, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
2014-05-16 14:21:15 +02:00
|
|
|
assertEquals(Arrays.asList(4, 5, 6), tailResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2014-10-03 17:33:14 +02:00
|
|
|
public void mustBeAbleToUseConcatAllWithSources() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<Integer> input1 = Arrays.asList(1, 2, 3);
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(4, 5);
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
final List<Source<Integer>> mainInputs = Arrays.asList(Source.from(input1), Source.from(input2));
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<List<Integer>> future = Source.from(mainInputs)
|
|
|
|
|
.flatten(akka.stream.javadsl.FlattenStrategy.<Integer> concat()).grouped(6)
|
|
|
|
|
.runWith(Sink.<List<Integer>> future(), materializer);
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-08-15 15:37:09 +02:00
|
|
|
List<Integer> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
2014-05-16 14:21:15 +02:00
|
|
|
|
2014-08-15 15:37:09 +02:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3, 4, 5), result);
|
2014-05-16 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseBuffer() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<List<String>> future = Source.from(input).buffer(2, OverflowStrategy.backpressure()).grouped(4)
|
|
|
|
|
.runWith(Sink.<List<String>> future(), materializer);
|
2014-10-03 17:33:14 +02:00
|
|
|
|
2014-05-20 16:02:09 +02:00
|
|
|
List<String> result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
assertEquals(input, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConflate() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-27 14:35:41 +01:00
|
|
|
// final List<String> input = Arrays.asList("A", "B", "C"); // test was fleaky // TODO FIXME, test was fleaky!
|
2014-10-03 17:33:14 +02:00
|
|
|
final List<String> input = Arrays.asList("C");
|
|
|
|
|
Future<String> future = Source.from(input).conflate(new Function<String, String>() {
|
2014-08-15 15:37:09 +02:00
|
|
|
@Override
|
|
|
|
|
public String apply(String s) throws Exception {
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}, new Function2<String, String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String in, String aggr) throws Exception {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
}).fold("", new Function2<String, String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String aggr, String in) throws Exception {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
2014-10-03 17:33:14 +02:00
|
|
|
}, materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
assertEquals("C", result);
|
2014-05-20 16:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseExpand() throws Exception {
|
2014-08-15 15:37:09 +02:00
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2014-10-27 14:35:41 +01:00
|
|
|
Future<String> future = Source.from(input).expand(new Function<String, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(String in) throws Exception {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
}, new Function<String, Pair<String, String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Pair<String, String> apply(String in) throws Exception {
|
|
|
|
|
return new Pair<String, String>(in, in);
|
|
|
|
|
}
|
|
|
|
|
}).runWith(Sink.<String> future(), materializer);
|
2014-08-15 15:37:09 +02:00
|
|
|
String result = Await.result(future, probe.dilated(FiniteDuration.create(3, TimeUnit.SECONDS)));
|
|
|
|
|
assertEquals("A", result);
|
2014-05-20 16:02:09 +02:00
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
|
2014-05-22 20:58:38 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustProduceTicks() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Callable<String> tick = new Callable<String>() {
|
|
|
|
|
private int count = 1;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String call() {
|
|
|
|
|
return "tick-" + (count++);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(FiniteDuration.create(1, TimeUnit.SECONDS), FiniteDuration.create(500, TimeUnit.MILLISECONDS), tick)
|
2014-10-27 14:35:41 +01:00
|
|
|
.foreach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}, materializer);
|
2014-07-18 10:55:34 +07:00
|
|
|
probe.expectNoMsg(FiniteDuration.create(600, TimeUnit.MILLISECONDS));
|
2014-05-22 20:58:38 +02:00
|
|
|
probe.expectMsgEquals("tick-1");
|
|
|
|
|
probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS));
|
|
|
|
|
probe.expectMsgEquals("tick-2");
|
|
|
|
|
probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS));
|
|
|
|
|
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
|
2014-05-23 13:52:39 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMapFuture() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2014-10-20 20:53:03 +02:00
|
|
|
final Iterable<String> input = Arrays.asList("a", "b", "c");
|
2014-10-03 17:33:14 +02:00
|
|
|
Source.from(input).mapAsync(new Function<String, Future<String>>() {
|
2014-05-23 13:52:39 +02:00
|
|
|
public Future<String> apply(String elem) {
|
|
|
|
|
return Futures.successful(elem.toUpperCase());
|
|
|
|
|
}
|
|
|
|
|
}).foreach(new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2014-08-15 15:37:09 +02:00
|
|
|
}, materializer);
|
2014-05-23 13:52:39 +02:00
|
|
|
probe.expectMsgEquals("A");
|
|
|
|
|
probe.expectMsgEquals("B");
|
|
|
|
|
probe.expectMsgEquals("C");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 10:05:09 +02:00
|
|
|
}
|