2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2014-2020 Lightbend Inc. <https://www.lightbend.com>
|
2014-12-16 11:48:42 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.Done;
|
|
|
|
|
import akka.NotUsed;
|
2014-12-03 10:33:21 +01:00
|
|
|
import akka.actor.ActorRef;
|
2015-01-22 10:57:42 +01:00
|
|
|
import akka.actor.Cancellable;
|
2018-06-15 18:16:58 +03:00
|
|
|
import akka.actor.Status;
|
2014-12-03 10:33:21 +01:00
|
|
|
import akka.japi.Pair;
|
2015-06-29 23:47:31 -04:00
|
|
|
import akka.japi.function.*;
|
2016-01-21 16:37:26 +01:00
|
|
|
import akka.japi.pf.PFBuilder;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
2015-12-22 20:56:02 +01:00
|
|
|
import akka.stream.*;
|
2018-07-20 08:15:13 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
2018-01-06 04:33:35 +08:00
|
|
|
import akka.stream.scaladsl.FlowSpec;
|
2017-05-11 00:00:42 +08:00
|
|
|
import akka.util.ConstantFun;
|
2017-04-25 22:24:41 -07:00
|
|
|
import akka.stream.stage.*;
|
2016-02-25 14:27:45 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
2015-07-23 22:01:05 -04:00
|
|
|
import akka.stream.testkit.TestPublisher;
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2020-02-11 16:58:00 +03:00
|
|
|
import com.google.common.collect.Iterables;
|
2014-12-03 10:33:21 +01:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
|
|
|
|
import scala.util.Try;
|
2016-05-30 12:54:27 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2015-06-29 23:47:31 -04:00
|
|
|
|
2018-03-19 22:14:33 +08:00
|
|
|
import java.time.Duration;
|
2014-12-03 10:33:21 +01:00
|
|
|
import java.util.*;
|
2016-01-21 16:37:26 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-12-03 10:33:21 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-11-02 15:30:10 +01:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2016-01-18 11:29:14 +01:00
|
|
|
import java.util.stream.Stream;
|
2015-06-29 23:47:31 -04:00
|
|
|
|
2018-05-31 21:42:20 +09:30
|
|
|
import static akka.NotUsed.notUsed;
|
2015-07-23 22:01:05 -04:00
|
|
|
import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription;
|
|
|
|
|
import static akka.stream.testkit.TestPublisher.ManualProbe;
|
2019-01-02 15:46:13 +01:00
|
|
|
import static org.hamcrest.core.Is.is;
|
2015-11-25 19:58:48 +01:00
|
|
|
import static org.junit.Assert.*;
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2015-04-09 15:16:59 +02:00
|
|
|
@SuppressWarnings("serial")
|
2014-12-03 10:33:21 +01:00
|
|
|
public class SourceTest extends StreamTest {
|
|
|
|
|
public SourceTest() {
|
|
|
|
|
super(actorSystemResource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
2019-01-12 04:00:53 +08:00
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("SourceTest", AkkaSpec.testConf());
|
2018-03-15 14:53:50 +01:00
|
|
|
|
|
|
|
|
interface Fruit {}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2018-03-15 14:53:50 +01:00
|
|
|
static class Apple implements Fruit {};
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2018-03-15 14:53:50 +01:00
|
|
|
static class Orange implements Fruit {};
|
|
|
|
|
|
|
|
|
|
public void compileOnlyUpcast() {
|
|
|
|
|
Source<Apple, NotUsed> apples = null;
|
|
|
|
|
Source<Orange, NotUsed> oranges = null;
|
|
|
|
|
Source<Fruit, NotUsed> appleFruits = Source.upcast(apples);
|
|
|
|
|
Source<Fruit, NotUsed> orangeFruits = Source.upcast(oranges);
|
|
|
|
|
|
|
|
|
|
Source<Fruit, NotUsed> fruits = appleFruits.merge(orangeFruits);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSimpleOperators() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final String[] lookup = {"a", "b", "c", "d", "e", "f"};
|
|
|
|
|
final java.lang.Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5);
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<Integer, NotUsed> ints = Source.from(input);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
ints.drop(2)
|
|
|
|
|
.take(3)
|
|
|
|
|
.takeWithin(Duration.ofSeconds(10))
|
|
|
|
|
.map(elem -> lookup[elem])
|
|
|
|
|
.filter(elem -> !elem.equals("c"))
|
|
|
|
|
.grouped(2)
|
|
|
|
|
.mapConcat(elem -> elem)
|
|
|
|
|
.groupedWithin(100, Duration.ofMillis(50))
|
|
|
|
|
.mapConcat(elem -> elem)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runFold("", (acc, elem) -> acc + elem, system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender()));
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("de");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseVoidTypeInForeach() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final java.lang.Iterable<String> input = Arrays.asList("a", "b", "c");
|
2016-01-21 16:37:26 +01:00
|
|
|
Source<String, NotUsed> ints = Source.from(input);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> completion =
|
2019-08-23 18:19:27 +02:00
|
|
|
ints.runForeach(elem -> probe.getRef().tell(elem, ActorRef.noSender()), system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
completion.thenAccept(elem -> probe.getRef().tell(String.valueOf(elem), ActorRef.noSender()));
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("a");
|
|
|
|
|
probe.expectMsgEquals("b");
|
|
|
|
|
probe.expectMsgEquals("c");
|
2016-01-21 18:06:42 +02:00
|
|
|
probe.expectMsgEquals("Done");
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
2017-04-25 22:24:41 -07:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseVia() {
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7);
|
|
|
|
|
// duplicate each element, stop after 4 elements, and emit sum to the end
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input)
|
|
|
|
|
.via(
|
|
|
|
|
new GraphStage<FlowShape<Integer, Integer>>() {
|
|
|
|
|
public final Inlet<Integer> in = Inlet.create("in");
|
|
|
|
|
public final Outlet<Integer> out = Outlet.create("out");
|
|
|
|
|
|
2017-04-25 22:24:41 -07:00
|
|
|
@Override
|
2019-01-12 04:00:53 +08:00
|
|
|
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() {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-04-25 22:24:41 -07:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2017-04-25 22:24:41 -07:00
|
|
|
@Override
|
2019-01-12 04:00:53 +08:00
|
|
|
public FlowShape<Integer, Integer> shape() {
|
|
|
|
|
return FlowShape.of(in, out);
|
2017-04-25 22:24:41 -07:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
})
|
|
|
|
|
.runForeach(
|
2019-08-23 18:19:27 +02:00
|
|
|
(Procedure<Integer>) elem -> probe.getRef().tell(elem, ActorRef.noSender()), system);
|
2017-04-25 22:24:41 -07: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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-25 19:58:48 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-12-03 10:33:21 +01:00
|
|
|
@Test
|
2015-11-25 19:58:48 +01:00
|
|
|
public void mustBeAbleToUseGroupBy() throws Exception {
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee");
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<List<String>, NotUsed> source =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.groupBy(
|
|
|
|
|
3,
|
|
|
|
|
new Function<String, String>() {
|
|
|
|
|
public String apply(String elem) {
|
|
|
|
|
return elem.substring(0, 1);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.mergeSubstreams();
|
2015-12-17 11:48:30 +02:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2019-08-23 18:19:27 +02:00
|
|
|
source.grouped(10).runWith(Sink.<List<List<String>>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Object[] result = future.toCompletableFuture().get(1, TimeUnit.SECONDS).toArray();
|
2019-01-12 04:00:53 +08:00
|
|
|
Arrays.sort(
|
|
|
|
|
result,
|
|
|
|
|
(Comparator<Object>)
|
|
|
|
|
(Object)
|
|
|
|
|
new Comparator<List<String>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(List<String> o1, List<String> 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);
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2015-11-25 19:58:48 +01:00
|
|
|
public void mustBeAbleToUseSplitWhen() throws Exception {
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F");
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<List<String>, NotUsed> source =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.splitWhen(
|
|
|
|
|
new Predicate<String>() {
|
|
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return elem.equals(".");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.concatSubstreams();
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2019-08-23 18:19:27 +02:00
|
|
|
source.grouped(10).runWith(Sink.<List<List<String>>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
assertEquals(
|
|
|
|
|
Arrays.asList(
|
|
|
|
|
Arrays.asList("A", "B", "C"), Arrays.asList(".", "D"), Arrays.asList(".", "E", "F")),
|
|
|
|
|
result);
|
2015-11-25 19:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSplitAfter() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C", ".", "D", ".", "E", "F");
|
|
|
|
|
final Source<List<String>, NotUsed> source =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.splitAfter(
|
|
|
|
|
new Predicate<String>() {
|
|
|
|
|
public boolean test(String elem) {
|
|
|
|
|
return elem.equals(".");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.grouped(10)
|
|
|
|
|
.concatSubstreams();
|
2015-11-25 19:58:48 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<List<List<String>>> future =
|
2019-08-23 18:19:27 +02:00
|
|
|
source.grouped(10).runWith(Sink.<List<List<String>>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<List<String>> result = future.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
2015-11-25 19:58:48 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
assertEquals(
|
|
|
|
|
Arrays.asList(
|
|
|
|
|
Arrays.asList("A", "B", "C", "."), Arrays.asList("D", "."), Arrays.asList("E", "F")),
|
|
|
|
|
result);
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConcat() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<String, NotUsed> in1 = Source.from(input1);
|
|
|
|
|
final Source<String, NotUsed> in2 = Source.from(input2);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
in1.concat(in2)
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2015-12-07 12:16:59 +00:00
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
List<Object> output = probe.receiveN(6);
|
2015-12-07 12:16:59 +00:00
|
|
|
assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePrepend() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-12-07 12:16:59 +00:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<String, NotUsed> in1 = Source.from(input1);
|
|
|
|
|
final Source<String, NotUsed> in2 = Source.from(input2);
|
2015-12-07 12:16:59 +00:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
in2.prepend(in1)
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
List<Object> output = probe.receiveN(6);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(Arrays.asList("A", "B", "C", "D", "E", "F"), output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseCallableInput() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<Integer> input1 = Arrays.asList(4, 3, 2, 1, 0);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Creator<Iterator<Integer>> input =
|
|
|
|
|
new Creator<Iterator<Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Iterator<Integer> create() {
|
|
|
|
|
return input1.iterator();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Source.fromIterator(input)
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<Integer>() {
|
|
|
|
|
public void apply(Integer elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
List<Object> output = probe.receiveN(5);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(Arrays.asList(4, 3, 2, 1, 0), output);
|
2019-06-13 12:06:02 +01:00
|
|
|
probe.expectNoMessage(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseOnCompleteSuccess() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input)
|
|
|
|
|
.runWith(
|
|
|
|
|
Sink.<String>onComplete(
|
|
|
|
|
new Procedure<Try<Done>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Try<Done> param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param.get(), ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}),
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2016-01-21 18:06:42 +02:00
|
|
|
probe.expectMsgClass(Done.class);
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseOnCompleteError() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
Source.from(input)
|
2019-01-12 04:00:53 +08:00
|
|
|
.<String>map(
|
|
|
|
|
in -> {
|
|
|
|
|
throw new RuntimeException("simulated err");
|
|
|
|
|
})
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<String>head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.whenComplete(
|
|
|
|
|
(s, ex) -> {
|
|
|
|
|
if (ex == null) {
|
|
|
|
|
probe.getRef().tell("done", ActorRef.noSender());
|
|
|
|
|
} else {
|
|
|
|
|
probe.getRef().tell(ex.getMessage(), ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("simulated err");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseToFuture() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
2019-08-23 18:19:27 +02:00
|
|
|
CompletionStage<String> future = Source.from(input).runWith(Sink.<String>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals("A", result);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-20 08:15:13 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseSingle() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #source-single
|
2019-08-23 18:19:27 +02:00
|
|
|
CompletionStage<List<String>> future = Source.single("A").runWith(Sink.seq(), system);
|
2018-07-20 08:15:13 +02:00
|
|
|
CompletableFuture<List<String>> completableFuture = future.toCompletableFuture();
|
|
|
|
|
completableFuture.thenAccept(result -> System.out.printf("collected elements: %s\n", result));
|
|
|
|
|
// result list will contain exactly one element "A"
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #source-single
|
2018-07-20 08:15:13 +02:00
|
|
|
// DO NOT use get() directly in your production code!
|
|
|
|
|
List<String> result = completableFuture.get();
|
|
|
|
|
assertEquals(1, result.size());
|
|
|
|
|
assertEquals("A", result.get(0));
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePrefixAndTail() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6);
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<Pair<List<Integer>, Source<Integer, NotUsed>>> future =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.prefixAndTail(3)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<Pair<List<Integer>, Source<Integer, NotUsed>>>head(), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
Pair<List<Integer>, Source<Integer, NotUsed>> result =
|
|
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(Arrays.asList(1, 2, 3), result.first());
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<List<Integer>> tailFuture =
|
2019-08-23 18:19:27 +02:00
|
|
|
result.second().limit(4).runWith(Sink.<Integer>seq(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> tailResult = tailFuture.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(Arrays.asList(4, 5, 6), tailResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConcatAllWithSources() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<Integer> input1 = Arrays.asList(1, 2, 3);
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(4, 5);
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final List<Source<Integer, NotUsed>> mainInputs = new ArrayList<Source<Integer, NotUsed>>();
|
2015-01-28 14:19:50 +01:00
|
|
|
mainInputs.add(Source.from(input1));
|
|
|
|
|
mainInputs.add(Source.from(input2));
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<List<Integer>> future =
|
|
|
|
|
Source.from(mainInputs)
|
|
|
|
|
.<Integer, NotUsed>flatMapConcat(
|
|
|
|
|
ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction())
|
|
|
|
|
.grouped(6)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<List<Integer>>head(), system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
|
|
|
|
assertEquals(Arrays.asList(1, 2, 3, 4, 5), result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 18:03:30 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseFlatMapMerge() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-12-01 18:03:30 +01:00
|
|
|
final Iterable<Integer> input1 = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
|
|
|
|
|
final Iterable<Integer> input3 = Arrays.asList(20, 21, 22, 23, 24, 25, 26, 27, 28, 29);
|
|
|
|
|
final Iterable<Integer> input4 = Arrays.asList(30, 31, 32, 33, 34, 35, 36, 37, 38, 39);
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final List<Source<Integer, NotUsed>> mainInputs = new ArrayList<Source<Integer, NotUsed>>();
|
2015-12-01 18:03:30 +01:00
|
|
|
mainInputs.add(Source.from(input1));
|
|
|
|
|
mainInputs.add(Source.from(input2));
|
|
|
|
|
mainInputs.add(Source.from(input3));
|
|
|
|
|
mainInputs.add(Source.from(input4));
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<List<Integer>> future =
|
|
|
|
|
Source.from(mainInputs)
|
|
|
|
|
.flatMapMerge(3, ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction())
|
|
|
|
|
.grouped(60)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<List<Integer>>head(), system);
|
2015-12-01 18:03:30 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-12-01 18:03:30 +01:00
|
|
|
final Set<Integer> set = new HashSet<Integer>();
|
2019-01-12 04:00:53 +08:00
|
|
|
for (Integer i : result) {
|
2015-12-01 18:03:30 +01:00
|
|
|
set.add(i);
|
|
|
|
|
}
|
|
|
|
|
final Set<Integer> expected = new HashSet<Integer>();
|
|
|
|
|
for (int i = 0; i < 40; ++i) {
|
|
|
|
|
expected.add(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertEquals(expected, set);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseBuffer() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<List<String>> future =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.buffer(2, OverflowStrategy.backpressure())
|
|
|
|
|
.grouped(4)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<List<String>>head(), system);
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
List<String> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals(input, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseConflate() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<String> future =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.conflateWithSeed(s -> s, (aggr, in) -> aggr + in)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runFold("", (aggr, in) -> aggr + in, system);
|
2016-01-21 16:37:26 +01:00
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals("ABC", result);
|
2016-01-22 15:22:30 +01:00
|
|
|
|
|
|
|
|
final Flow<String, String, NotUsed> flow2 = Flow.of(String.class).conflate((a, b) -> a + b);
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<String> future2 =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.conflate((String a, String b) -> a + b)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runFold("", (a, b) -> a + b, system);
|
2016-01-22 15:22:30 +01:00
|
|
|
String result2 = future2.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
assertEquals("ABC", result2);
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseExpand() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final List<String> input = Arrays.asList("A", "B", "C");
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<String> future =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.expand(in -> Stream.iterate(in, i -> i).iterator())
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.<String>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals("A", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustProduceTicks() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
Source<String, Cancellable> tickSource =
|
|
|
|
|
Source.tick(Duration.ofSeconds(1), Duration.ofMillis(500), "tick");
|
2016-01-17 16:37:45 +01:00
|
|
|
@SuppressWarnings("unused")
|
2019-01-12 04:00:53 +08:00
|
|
|
Cancellable cancellable =
|
|
|
|
|
tickSource
|
|
|
|
|
.to(
|
|
|
|
|
Sink.foreach(
|
|
|
|
|
new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}))
|
2019-08-23 18:19:27 +02:00
|
|
|
.run(system);
|
2018-05-29 18:17:48 +08:00
|
|
|
probe.expectNoMessage(Duration.ofMillis(600));
|
2015-01-26 14:16:57 +01:00
|
|
|
probe.expectMsgEquals("tick");
|
2018-05-29 18:17:48 +08:00
|
|
|
probe.expectNoMessage(Duration.ofMillis(200));
|
2015-01-26 14:16:57 +01:00
|
|
|
probe.expectMsgEquals("tick");
|
2018-05-29 18:17:48 +08:00
|
|
|
probe.expectNoMessage(Duration.ofMillis(200));
|
2018-06-15 18:16:58 +03:00
|
|
|
cancellable.cancel();
|
2018-03-06 00:13:15 +08:00
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
|
2018-03-06 00:13:15 +08:00
|
|
|
@Test
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
public void mustCompileMethodsWithJavaDuration() {
|
2019-01-12 04:00:53 +08:00
|
|
|
Source<NotUsed, Cancellable> tickSource =
|
|
|
|
|
Source.tick(Duration.ofSeconds(1), Duration.ofMillis(500), notUsed());
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMapFuture() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2014-12-03 10:33:21 +01:00
|
|
|
final Iterable<String> input = Arrays.asList("a", "b", "c");
|
2016-01-21 16:37:26 +01:00
|
|
|
Source.from(input)
|
2019-01-12 04:00:53 +08:00
|
|
|
.mapAsync(4, elem -> CompletableFuture.completedFuture(elem.toUpperCase()))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runForeach(elem -> probe.getRef().tell(elem, ActorRef.noSender()), system);
|
2014-12-03 10:33:21 +01:00
|
|
|
probe.expectMsgEquals("A");
|
|
|
|
|
probe.expectMsgEquals("B");
|
|
|
|
|
probe.expectMsgEquals("C");
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-06 04:33:35 +08:00
|
|
|
@Test
|
2019-01-12 04:00:53 +08:00
|
|
|
public void mustBeAbleToUseCollectType() throws Exception {
|
2018-01-06 04:33:35 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
final Iterable<FlowSpec.Apple> input = Collections.singletonList(new FlowSpec.Apple());
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<FlowSpec.Apple, ?> appleSource = Source.from(input);
|
|
|
|
|
final Source<FlowSpec.Fruit, ?> fruitSource = appleSource.collectType(FlowSpec.Fruit.class);
|
|
|
|
|
fruitSource
|
|
|
|
|
.collectType(FlowSpec.Apple.class)
|
|
|
|
|
.collectType(FlowSpec.Apple.class)
|
|
|
|
|
.runForeach(
|
|
|
|
|
(elem) -> {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2018-01-06 04:33:35 +08:00
|
|
|
probe.expectMsgAnyClassOf(FlowSpec.Apple.class);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 10:33:21 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustWorkFromFuture() throws Exception {
|
|
|
|
|
final Iterable<String> input = Arrays.asList("A", "B", "C");
|
2019-08-23 18:19:27 +02:00
|
|
|
CompletionStage<String> future1 = Source.from(input).runWith(Sink.<String>head(), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<String> future2 =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.fromCompletionStage(future1).runWith(Sink.<String>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
String result = future2.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2014-12-03 10:33:21 +01:00
|
|
|
assertEquals("A", result);
|
|
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2019-04-16 09:08:05 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustWorkFromFutureVoid() throws Exception {
|
|
|
|
|
CompletionStage<Void> future = CompletableFuture.completedFuture(null);
|
|
|
|
|
CompletionStage<List<Void>> future2 =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.fromCompletionStage(future).runWith(Sink.seq(), system);
|
2019-04-16 09:08:05 +02:00
|
|
|
List<Void> result = future2.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
assertEquals(0, result.size());
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-27 22:24:24 -05:00
|
|
|
@Test
|
|
|
|
|
public void mustWorkFromRange() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<List<Integer>> f =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.range(0, 10).grouped(20).runWith(Sink.<List<Integer>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<Integer> result = f.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-12-02 11:20:47 -05:00
|
|
|
assertEquals(11, result.size());
|
2015-11-27 22:24:24 -05:00
|
|
|
Integer counter = 0;
|
2019-01-12 04:00:53 +08:00
|
|
|
for (Integer i : result) assertEquals(i, counter++);
|
2015-11-27 22:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 11:20:47 -05:00
|
|
|
@Test
|
|
|
|
|
public void mustWorkFromRangeWithStep() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<List<Integer>> f =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.range(0, 10, 2).grouped(20).runWith(Sink.<List<Integer>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<Integer> result = f.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-12-02 11:20:47 -05:00
|
|
|
assertEquals(6, result.size());
|
|
|
|
|
Integer counter = 0;
|
2019-01-12 04:00:53 +08:00
|
|
|
for (Integer i : result) {
|
2015-12-02 11:20:47 -05:00
|
|
|
assertEquals(i, counter);
|
2019-01-12 04:00:53 +08:00
|
|
|
counter += 2;
|
2015-12-02 11:20:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 12:36:46 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustRepeat() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<List<Integer>> f =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.repeat(42).grouped(10000).runWith(Sink.<List<Integer>>head(), system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final List<Integer> result = f.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-02-26 12:36:46 +01:00
|
|
|
assertEquals(result.size(), 10000);
|
2019-01-12 04:00:53 +08:00
|
|
|
for (Integer i : result) assertEquals(i, (Integer) 42);
|
2015-02-26 12:36:46 +01:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-02-25 16:05:35 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseQueue() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
final Pair<SourceQueueWithComplete<String>, CompletionStage<List<String>>> x =
|
2019-08-23 18:19:27 +02:00
|
|
|
Flow.of(String.class).runWith(Source.queue(2, OverflowStrategy.fail()), Sink.seq(), system);
|
2016-02-25 16:05:35 +01:00
|
|
|
final SourceQueueWithComplete<String> source = x.first();
|
|
|
|
|
final CompletionStage<List<String>> result = x.second();
|
|
|
|
|
source.offer("hello");
|
|
|
|
|
source.offer("world");
|
|
|
|
|
source.complete();
|
2019-01-12 04:00:53 +08:00
|
|
|
assertEquals(
|
|
|
|
|
result.toCompletableFuture().get(3, TimeUnit.SECONDS), Arrays.asList("hello", "world"));
|
2016-02-25 16:05:35 +01:00
|
|
|
}
|
2015-10-31 14:46:10 +01:00
|
|
|
|
2015-03-31 15:13:57 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseActorRefSource() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-03-31 15:13:57 +02:00
|
|
|
final Source<Integer, ActorRef> actorRefSource = Source.actorRef(10, OverflowStrategy.fail());
|
2019-01-12 04:00:53 +08:00
|
|
|
final ActorRef ref =
|
|
|
|
|
actorRefSource
|
|
|
|
|
.to(
|
|
|
|
|
Sink.foreach(
|
|
|
|
|
new Procedure<Integer>() {
|
|
|
|
|
public void apply(Integer elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
}))
|
2019-08-23 18:19:27 +02:00
|
|
|
.run(system);
|
2015-03-31 15:13:57 +02:00
|
|
|
ref.tell(1, ActorRef.noSender());
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
ref.tell(2, ActorRef.noSender());
|
|
|
|
|
probe.expectMsgEquals(2);
|
2018-06-15 18:16:58 +03:00
|
|
|
ref.tell(new Status.Success("ok"), ActorRef.noSender());
|
2015-03-31 15:13:57 +02:00
|
|
|
}
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2016-02-15 16:53:57 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseStatefulMaponcat() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2016-02-15 16:53:57 +01:00
|
|
|
final java.lang.Iterable<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, NotUsed> ints =
|
|
|
|
|
Source.from(input)
|
|
|
|
|
.statefulMapConcat(
|
|
|
|
|
() -> {
|
|
|
|
|
int[] state = new int[] {0};
|
|
|
|
|
return (elem) -> {
|
|
|
|
|
List<Integer> list = new ArrayList<>(Collections.nCopies(state[0], elem));
|
|
|
|
|
state[0] = elem;
|
|
|
|
|
return list;
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-23 18:19:27 +02:00
|
|
|
ints.runFold("", (acc, elem) -> acc + elem, system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.thenAccept(elem -> probe.getRef().tell(elem, ActorRef.noSender()));
|
2016-02-15 16:53:57 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("2334445555");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseIntersperse() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<String, NotUsed> source =
|
|
|
|
|
Source.from(Arrays.asList("0", "1", "2", "3")).intersperse("[", ",", "]");
|
2016-02-15 16:53:57 +01:00
|
|
|
|
|
|
|
|
final CompletionStage<Done> future =
|
2019-01-12 04:00:53 +08:00
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2016-02-15 16:53:57 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("[");
|
|
|
|
|
probe.expectMsgEquals("0");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("1");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("2");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("3");
|
|
|
|
|
probe.expectMsgEquals("]");
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2016-02-15 16:53:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseIntersperseAndConcat() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<String, NotUsed> source =
|
|
|
|
|
Source.from(Arrays.asList("0", "1", "2", "3")).intersperse(",");
|
2016-02-15 16:53:57 +01:00
|
|
|
|
|
|
|
|
final CompletionStage<Done> future =
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.single(">> ")
|
|
|
|
|
.concat(source)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2016-02-15 16:53:57 +01:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(">> ");
|
|
|
|
|
probe.expectMsgEquals("0");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("1");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("2");
|
|
|
|
|
probe.expectMsgEquals(",");
|
|
|
|
|
probe.expectMsgEquals("3");
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2016-02-15 16:53:57 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-12 23:22:36 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseDropWhile() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, NotUsed> source =
|
|
|
|
|
Source.from(Arrays.asList(0, 1, 2, 3))
|
|
|
|
|
.dropWhile(
|
|
|
|
|
new Predicate<Integer>() {
|
|
|
|
|
public boolean test(Integer elem) {
|
|
|
|
|
return elem < 2;
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(2);
|
|
|
|
|
probe.expectMsgEquals(3);
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseTakeWhile() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, NotUsed> source =
|
|
|
|
|
Source.from(Arrays.asList(0, 1, 2, 3))
|
|
|
|
|
.takeWhile(
|
|
|
|
|
new Predicate<Integer>() {
|
|
|
|
|
public boolean test(Integer elem) {
|
|
|
|
|
return elem < 2;
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals(0);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
|
2018-05-29 18:17:48 +08:00
|
|
|
Duration duration = Duration.ofMillis(200);
|
2015-06-12 23:22:36 -04:00
|
|
|
|
2018-05-29 18:17:48 +08:00
|
|
|
probe.expectNoMessage(duration);
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-06-12 23:22:36 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-13 14:02:37 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToRecover() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
final ManualProbe<Integer> publisherProbe = TestPublisher.manualProbe(true, system);
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-07-23 22:01:05 -04:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<Integer, NotUsed> source =
|
|
|
|
|
Source.fromPublisher(publisherProbe)
|
2019-01-12 04:00:53 +08:00
|
|
|
.map(
|
|
|
|
|
elem -> {
|
|
|
|
|
if (elem == 1) throw new RuntimeException("ex");
|
|
|
|
|
else return elem;
|
|
|
|
|
})
|
|
|
|
|
.recover(new PFBuilder<Throwable, Integer>().matchAny(ex -> 0).build());
|
|
|
|
|
|
|
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2015-07-23 22:01:05 -04:00
|
|
|
final PublisherProbeSubscription<Integer> s = publisherProbe.expectSubscription();
|
|
|
|
|
s.sendNext(0);
|
2015-06-13 14:02:37 -04:00
|
|
|
probe.expectMsgEquals(0);
|
2015-07-23 22:01:05 -04:00
|
|
|
s.sendNext(1);
|
2015-06-13 14:02:37 -04:00
|
|
|
probe.expectMsgEquals(0);
|
2015-07-23 22:01:05 -04:00
|
|
|
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-06-13 14:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-29 23:47:31 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCombine() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2016-01-21 16:37:26 +01:00
|
|
|
final Source<Integer, NotUsed> source1 = Source.from(Arrays.asList(0, 1));
|
|
|
|
|
final Source<Integer, NotUsed> source2 = Source.from(Arrays.asList(2, 3));
|
2015-06-29 23:47:31 -04:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, NotUsed> source =
|
|
|
|
|
Source.combine(
|
|
|
|
|
source1,
|
|
|
|
|
source2,
|
|
|
|
|
new ArrayList<Source<Integer, ?>>(),
|
|
|
|
|
width -> Merge.<Integer>create(width));
|
2015-06-29 23:47:31 -04:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2015-06-29 23:47:31 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgAllOf(0, 1, 2, 3);
|
|
|
|
|
|
2016-04-22 12:04:28 +02:00
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 10:34:40 +09:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCombineMat() throws Exception {
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, SourceQueueWithComplete<Integer>> source1 =
|
|
|
|
|
Source.queue(1, OverflowStrategy.dropNew());
|
2017-11-02 10:34:40 +09:00
|
|
|
final Source<Integer, NotUsed> source2 = Source.from(Arrays.asList(2, 3));
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// compiler to check the correct materialized value of type = SourceQueueWithComplete<Integer>
|
|
|
|
|
// available
|
|
|
|
|
final Source<Integer, SourceQueueWithComplete<Integer>> combined =
|
|
|
|
|
Source.combineMat(
|
|
|
|
|
source1,
|
|
|
|
|
source2,
|
|
|
|
|
width -> Concat.<Integer>create(width),
|
|
|
|
|
Keep.left()); // Keep.left() (i.e. preserve queueSource's materialized value)
|
|
|
|
|
|
|
|
|
|
SourceQueueWithComplete<Integer> queue =
|
|
|
|
|
combined
|
|
|
|
|
.toMat(
|
|
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), Keep.left())
|
2019-08-23 18:19:27 +02:00
|
|
|
.run(system);
|
2017-11-02 10:34:40 +09:00
|
|
|
|
|
|
|
|
queue.offer(0);
|
|
|
|
|
queue.offer(1);
|
2019-01-12 04:00:53 +08:00
|
|
|
queue.complete(); // complete queueSource so that combined with `Concat` pulls elements from
|
|
|
|
|
// queueSource
|
2017-11-02 10:34:40 +09:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// elements from source1 (i.e. first of combined source) come first, then source2 elements, due
|
|
|
|
|
// to `Concat`
|
2017-11-02 10:34:40 +09:00
|
|
|
probe.expectMsgAllOf(0, 1, 2, 3);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 12:04:28 +02:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToZipN() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2016-04-22 12:04:28 +02:00
|
|
|
final Source<Integer, NotUsed> source1 = Source.from(Arrays.asList(0, 1));
|
|
|
|
|
final Source<Integer, NotUsed> source2 = Source.from(Arrays.asList(2, 3));
|
|
|
|
|
|
|
|
|
|
final List<Source<Integer, ?>> sources = Arrays.asList(source1, source2);
|
|
|
|
|
|
|
|
|
|
final Source<List<Integer>, ?> source = Source.zipN(sources);
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2016-04-22 12:04:28 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgAllOf(Arrays.asList(0, 2), Arrays.asList(1, 3));
|
|
|
|
|
|
|
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToZipWithN() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2016-04-22 12:04:28 +02:00
|
|
|
final Source<Integer, NotUsed> source1 = Source.from(Arrays.asList(0, 1));
|
|
|
|
|
final Source<Integer, NotUsed> source2 = Source.from(Arrays.asList(2, 3));
|
|
|
|
|
|
|
|
|
|
final List<Source<Integer, ?>> sources = Arrays.asList(source1, source2);
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Boolean, ?> source =
|
|
|
|
|
Source.zipWithN(list -> new Boolean(list.contains(0)), sources);
|
2016-04-22 12:04:28 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final CompletionStage<Done> future =
|
|
|
|
|
source.runWith(
|
2019-08-23 18:19:27 +02:00
|
|
|
Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), system);
|
2016-04-22 12:04:28 +02:00
|
|
|
|
|
|
|
|
probe.expectMsgAllOf(Boolean.TRUE, Boolean.FALSE);
|
|
|
|
|
|
|
|
|
|
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
2015-06-29 23:47:31 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 17:40:06 +03:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToZipAll() {
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
final Iterable<String> input1 =
|
|
|
|
|
Arrays.asList("A", "B", "C", "D", "new kid on the block1", "second newbie");
|
|
|
|
|
final Iterable<Integer> input2 = Arrays.asList(1, 2, 3, 4);
|
|
|
|
|
|
|
|
|
|
Source<String, NotUsed> src1 = Source.from(input1);
|
|
|
|
|
Source<Integer, NotUsed> src2 = Source.from(input2);
|
|
|
|
|
Sink<Pair<String, Integer>, CompletionStage<Done>> sink =
|
|
|
|
|
Sink.foreach(
|
|
|
|
|
new Procedure<Pair<String, Integer>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Pair<String, Integer> param) throws Exception {
|
|
|
|
|
probe.getRef().tell(param, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Source<Pair<String, Integer>, NotUsed> zippedSrc = src1.zipAll(src2, "MISSING", -1);
|
2019-08-23 18:19:27 +02:00
|
|
|
zippedSrc.runWith(sink, system);
|
2019-07-05 17:40:06 +03:00
|
|
|
|
|
|
|
|
List<Object> output = probe.receiveN(6);
|
|
|
|
|
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),
|
|
|
|
|
new Pair<String, Integer>("D", 4),
|
|
|
|
|
new Pair<String, Integer>("new kid on the block1", -1),
|
|
|
|
|
new Pair<String, Integer>("second newbie", -1));
|
|
|
|
|
assertEquals(expected, output);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 15:02:24 +01:00
|
|
|
@Test
|
|
|
|
|
public void createEmptySource() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
List<Integer> actual =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.empty(Integer.class).runWith(Sink.seq(), system).toCompletableFuture().get();
|
2019-01-12 04:00:53 +08:00
|
|
|
assertThat(actual, is(Collections.emptyList()));
|
2019-01-07 15:02:24 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-02 15:46:13 +01:00
|
|
|
@Test
|
|
|
|
|
public void cycleSourceMustGenerateSameSequenceInRepeatedFashion() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #cycle
|
2019-01-02 15:46:13 +01:00
|
|
|
final Source<Integer, NotUsed> source = Source.cycle(() -> Arrays.asList(1, 2, 3).iterator());
|
2019-08-23 18:19:27 +02:00
|
|
|
CompletionStage<List<Integer>> result = source.grouped(9).runWith(Sink.head(), system);
|
2019-01-02 15:46:13 +01:00
|
|
|
List<Integer> emittedValues = result.toCompletableFuture().get();
|
|
|
|
|
assertThat(emittedValues, is(Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3)));
|
2019-01-12 04:00:53 +08:00
|
|
|
// #cycle
|
2019-01-02 15:46:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class)
|
2019-01-07 15:02:24 +01:00
|
|
|
public void cycleSourceMustThrow() throws Throwable {
|
2019-01-02 15:46:13 +01:00
|
|
|
|
|
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #cycle-error
|
2019-01-02 15:46:13 +01:00
|
|
|
Iterator<Integer> emptyIterator = Collections.<Integer>emptyList().iterator();
|
|
|
|
|
Source.cycle(() -> emptyIterator)
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
// stream will be terminated with IllegalArgumentException
|
|
|
|
|
// #cycle-error
|
|
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get();
|
2019-01-02 15:46:13 +01:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
throw e.getCause();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 08:10:45 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMerge() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-09-21 08:10:45 -04:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input1)
|
|
|
|
|
.merge(Source.from(input2))
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2015-09-21 08:10:45 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgAllOf("A", "B", "C", "D", "E", "F");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZipWith() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-09-21 08:10:45 -04:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input1)
|
|
|
|
|
.zipWith(
|
|
|
|
|
Source.from(input2),
|
|
|
|
|
new Function2<String, String, String>() {
|
|
|
|
|
public String apply(String s1, String s2) {
|
|
|
|
|
return s1 + "-" + s2;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<String>() {
|
|
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2015-09-21 08:10:45 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgEquals("A-D");
|
|
|
|
|
probe.expectMsgEquals("B-E");
|
|
|
|
|
probe.expectMsgEquals("C-F");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseZip() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-09-21 08:10:45 -04:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input1)
|
|
|
|
|
.zip(Source.from(input2))
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<Pair<String, String>>() {
|
|
|
|
|
public void apply(Pair<String, String> elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2015-09-21 08:10:45 -04:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
probe.expectMsgEquals(new Pair<String, String>("A", "D"));
|
|
|
|
|
probe.expectMsgEquals(new Pair<String, String>("B", "E"));
|
|
|
|
|
probe.expectMsgEquals(new Pair<String, String>("C", "F"));
|
2015-09-21 08:10:45 -04:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-09-21 08:10:45 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseMerge2() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2015-09-21 08:10:45 -04:00
|
|
|
final Iterable<String> input1 = Arrays.asList("A", "B", "C");
|
|
|
|
|
final Iterable<String> input2 = Arrays.asList("D", "E", "F");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.from(input1)
|
|
|
|
|
.merge(Source.from(input2))
|
|
|
|
|
.runForeach(
|
|
|
|
|
new Procedure<String>() {
|
2015-09-21 08:10:45 -04:00
|
|
|
public void apply(String elem) {
|
|
|
|
|
probe.getRef().tell(elem, ActorRef.noSender());
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
},
|
2019-08-23 18:19:27 +02:00
|
|
|
system);
|
2015-09-21 08:10:45 -04:00
|
|
|
|
|
|
|
|
probe.expectMsgAllOf("A", "B", "C", "D", "E", "F");
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 15:30:10 +01:00
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseInitialTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.maybe()
|
|
|
|
|
.initialTimeout(Duration.ofSeconds(1))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2016-08-03 14:06:57 +02:00
|
|
|
org.junit.Assert.fail("A TimeoutException was expected");
|
2016-01-21 16:37:26 +01:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
throw e.getCause();
|
|
|
|
|
}
|
|
|
|
|
} catch (TimeoutException e) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseCompletionTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.maybe()
|
|
|
|
|
.completionTimeout(Duration.ofSeconds(1))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2016-08-03 14:06:57 +02:00
|
|
|
org.junit.Assert.fail("A TimeoutException was expected");
|
2016-01-21 16:37:26 +01:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
throw e.getCause();
|
|
|
|
|
}
|
|
|
|
|
} catch (TimeoutException e) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-01-21 16:37:26 +01:00
|
|
|
public void mustBeAbleToUseIdleTimeout() throws Throwable {
|
2015-11-02 15:30:10 +01:00
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.maybe()
|
|
|
|
|
.idleTimeout(Duration.ofSeconds(1))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2016-08-03 14:06:57 +02:00
|
|
|
org.junit.Assert.fail("A TimeoutException was expected");
|
2016-01-21 16:37:26 +01:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
throw e.getCause();
|
|
|
|
|
}
|
|
|
|
|
} catch (TimeoutException e) {
|
2015-11-02 15:30:10 +01:00
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseIdleInject() throws Exception {
|
2016-01-21 16:37:26 +01:00
|
|
|
Integer result =
|
2018-03-15 14:53:50 +01:00
|
|
|
Source.<Integer>maybe()
|
2018-03-19 22:14:33 +08:00
|
|
|
.keepAlive(Duration.ofSeconds(1), () -> 0)
|
|
|
|
|
.takeWithin(Duration.ofMillis(1500))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2015-11-02 15:30:10 +01:00
|
|
|
|
|
|
|
|
assertEquals((Object) 0, result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 20:56:02 +01:00
|
|
|
public void mustSuitablyOverrideAttributeHandlingMethods() {
|
|
|
|
|
@SuppressWarnings("unused")
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<Integer, NotUsed> f =
|
2019-01-12 04:00:53 +08:00
|
|
|
Source.single(42)
|
|
|
|
|
.withAttributes(Attributes.name(""))
|
|
|
|
|
.addAttributes(Attributes.asyncBoundary())
|
|
|
|
|
.named("");
|
2015-12-22 20:56:02 +01:00
|
|
|
}
|
2016-02-10 10:17:02 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseThrottle() throws Exception {
|
|
|
|
|
Integer result =
|
|
|
|
|
Source.from(Arrays.asList(0, 1, 2))
|
2018-03-19 22:14:33 +08:00
|
|
|
.throttle(10, Duration.ofSeconds(1), 10, ThrottleMode.shaping())
|
|
|
|
|
.throttle(10, Duration.ofSeconds(1), 10, ThrottleMode.enforcing())
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(Sink.head(), system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2016-02-10 10:17:02 +02:00
|
|
|
|
|
|
|
|
assertEquals((Object) 0, result);
|
|
|
|
|
}
|
2018-01-22 19:17:41 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseAlsoTo() {
|
|
|
|
|
final Source<Integer, NotUsed> f = Source.<Integer>empty().alsoTo(Sink.ignore());
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, String> f2 =
|
|
|
|
|
Source.<Integer>empty().alsoToMat(Sink.ignore(), (i, n) -> "foo");
|
2018-01-22 19:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseDivertTo() {
|
|
|
|
|
final Source<Integer, NotUsed> f = Source.<Integer>empty().divertTo(Sink.ignore(), e -> true);
|
2019-01-12 04:00:53 +08:00
|
|
|
final Source<Integer, String> f2 =
|
|
|
|
|
Source.<Integer>empty().divertToMat(Sink.ignore(), e -> true, (i, n) -> "foo");
|
2018-01-22 19:17:41 +01:00
|
|
|
}
|
2018-02-21 06:06:01 +00:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUsePreMaterialize() {
|
2019-01-12 04:00:53 +08:00
|
|
|
final Pair<NotUsed, Source<Integer, NotUsed>> p =
|
2019-08-23 18:19:27 +02:00
|
|
|
Source.<Integer>empty().preMaterialize(system);
|
2018-02-21 06:06:01 +00:00
|
|
|
}
|
2019-02-07 16:41:49 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToConvertToJavaInJava() {
|
|
|
|
|
final akka.stream.scaladsl.Source<Integer, NotUsed> scalaSource =
|
|
|
|
|
akka.stream.scaladsl.Source.empty();
|
|
|
|
|
Source<Integer, NotUsed> javaSource = scalaSource.asJava();
|
|
|
|
|
}
|
2020-02-11 16:58:00 +03:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustProperlyIterate() throws Exception {
|
|
|
|
|
final Creator<Iterator<Boolean>> input = () -> Iterables.cycle(false, true).iterator();
|
|
|
|
|
|
|
|
|
|
final CompletableFuture<List<Boolean>> future =
|
|
|
|
|
Source.fromIterator(input).grouped(10).runWith(Sink.head(), system).toCompletableFuture();
|
|
|
|
|
|
|
|
|
|
assertArrayEquals(
|
|
|
|
|
new Boolean[] {false, true, false, true, false, true, false, true, false, true},
|
|
|
|
|
future.get(1, TimeUnit.SECONDS).toArray());
|
|
|
|
|
}
|
2014-12-03 10:33:21 +01:00
|
|
|
}
|