2014-10-30 16:23:43 +01:00
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2014-2016 Typesafe Inc. <http://www.typesafe.com>
|
2014-10-30 16:23:43 +01:00
|
|
|
*/
|
|
|
|
|
package akka.stream.javadsl;
|
|
|
|
|
|
2015-03-30 14:42:30 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2014-11-07 15:00:50 +01:00
|
|
|
import java.util.ArrayList;
|
2015-03-30 14:42:30 +02:00
|
|
|
import java.util.Collections;
|
2014-11-07 15:00:50 +01:00
|
|
|
import java.util.List;
|
2016-01-21 16:37:26 +01:00
|
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-06-29 23:47:31 -04:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.NotUsed;
|
2015-06-29 23:47:31 -04:00
|
|
|
import akka.japi.function.Function;
|
2015-12-22 20:56:02 +01:00
|
|
|
import akka.stream.*;
|
2014-10-30 16:23:43 +01:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.reactivestreams.Publisher;
|
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
import scala.concurrent.Future;
|
|
|
|
|
import scala.concurrent.duration.Duration;
|
2015-04-23 20:59:55 +02:00
|
|
|
import akka.japi.function.Function2;
|
2015-03-30 14:42:30 +02:00
|
|
|
import akka.stream.testkit.AkkaSpec;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
2014-10-30 16:23:43 +01:00
|
|
|
|
2014-11-07 15:00:50 +01:00
|
|
|
public class SinkTest extends StreamTest {
|
|
|
|
|
public SinkTest() {
|
|
|
|
|
super(actorSystemResource);
|
|
|
|
|
}
|
2014-10-30 16:23:43 +01:00
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest",
|
|
|
|
|
AkkaSpec.testConf());
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseFanoutPublisher() throws Exception {
|
2016-01-20 21:01:27 +01:00
|
|
|
final Sink<Object, Publisher<Object>> pubSink = Sink.asPublisher(AsPublisher.WITH_FANOUT);
|
2015-03-30 14:42:30 +02:00
|
|
|
@SuppressWarnings("unused")
|
2014-10-30 19:43:37 +01:00
|
|
|
final Publisher<Object> publisher = Source.from(new ArrayList<Object>()).runWith(pubSink, materializer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseFuture() throws Exception {
|
2016-01-21 16:37:26 +01:00
|
|
|
final Sink<Integer, CompletionStage<Integer>> futSink = Sink.head();
|
2015-03-30 14:42:30 +02:00
|
|
|
final List<Integer> list = Collections.singletonList(1);
|
2016-01-21 16:37:26 +01:00
|
|
|
final CompletionStage<Integer> future = Source.from(list).runWith(futSink, materializer);
|
|
|
|
|
assert future.toCompletableFuture().get(1, TimeUnit.SECONDS).equals(1);
|
2014-10-30 16:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-30 19:47:14 +01:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseFold() throws Exception {
|
2016-01-21 16:37:26 +01:00
|
|
|
Sink<Integer, CompletionStage<Integer>> foldSink = Sink.fold(0, (arg1, arg2) -> arg1 + arg2);
|
2015-03-30 14:42:30 +02:00
|
|
|
@SuppressWarnings("unused")
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<Integer> integerFuture = Source.from(new ArrayList<Integer>()).runWith(foldSink, materializer);
|
2014-10-30 19:47:14 +01:00
|
|
|
}
|
2015-03-30 14:42:30 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToUseActorRefSink() throws Exception {
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
final Sink<Integer, ?> actorRefSink = Sink.actorRef(probe.getRef(), "done");
|
|
|
|
|
Source.from(Arrays.asList(1, 2, 3)).runWith(actorRefSink, materializer);
|
|
|
|
|
probe.expectMsgEquals(1);
|
|
|
|
|
probe.expectMsgEquals(2);
|
|
|
|
|
probe.expectMsgEquals(3);
|
|
|
|
|
probe.expectMsgEquals("done");
|
|
|
|
|
}
|
2014-10-30 19:47:14 +01:00
|
|
|
|
2015-06-29 23:47:31 -04:00
|
|
|
@Test
|
|
|
|
|
public void mustBeAbleToCombine() throws Exception {
|
|
|
|
|
final JavaTestKit probe1 = new JavaTestKit(system);
|
|
|
|
|
final JavaTestKit probe2 = new JavaTestKit(system);
|
|
|
|
|
|
|
|
|
|
final Sink<Integer, ?> sink1 = Sink.actorRef(probe1.getRef(), "done1");
|
|
|
|
|
final Sink<Integer, ?> sink2 = Sink.actorRef(probe2.getRef(), "done2");
|
|
|
|
|
|
2015-09-25 12:51:55 +02:00
|
|
|
final Sink<Integer, ?> sink = Sink.combine(sink1, sink2, new ArrayList<Sink<Integer, ?>>(),
|
2016-01-20 10:00:37 +02:00
|
|
|
new Function<Integer, Graph<UniformFanOutShape<Integer, Integer>, NotUsed>>() {
|
|
|
|
|
public Graph<UniformFanOutShape<Integer, Integer>, NotUsed> apply(Integer elem) {
|
2015-06-29 23:47:31 -04:00
|
|
|
return Broadcast.create(elem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Source.from(Arrays.asList(0, 1)).runWith(sink, materializer);
|
|
|
|
|
|
|
|
|
|
probe1.expectMsgEquals(0);
|
|
|
|
|
probe2.expectMsgEquals(0);
|
|
|
|
|
probe1.expectMsgEquals(1);
|
|
|
|
|
probe2.expectMsgEquals(1);
|
|
|
|
|
|
|
|
|
|
probe1.expectMsgEquals("done1");
|
|
|
|
|
probe2.expectMsgEquals("done2");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 20:56:02 +01:00
|
|
|
public void mustSuitablyOverrideAttributeHandlingMethods() {
|
|
|
|
|
@SuppressWarnings("unused")
|
2016-01-21 16:37:26 +01:00
|
|
|
final Sink<Integer, CompletionStage<Integer>> s =
|
2015-12-22 20:56:02 +01:00
|
|
|
Sink.<Integer> head().withAttributes(Attributes.name("")).addAttributes(Attributes.asyncBoundary()).named("");
|
|
|
|
|
}
|
2014-10-30 16:23:43 +01:00
|
|
|
}
|