/** * Copyright (C) 2014 Typesafe Inc. */ package akka.stream.javadsl; import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import akka.actor.ActorRef; import akka.japi.function.Function; import akka.japi.function.Procedure; import akka.stream.*; 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; import akka.japi.function.Function2; import akka.stream.testkit.AkkaSpec; import akka.testkit.JavaTestKit; import scala.runtime.BoxedUnit; public class SinkTest extends StreamTest { public SinkTest() { super(actorSystemResource); } @ClassRule public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("FlowTest", AkkaSpec.testConf()); @Test public void mustBeAbleToUseFanoutPublisher() throws Exception { final Sink> pubSink = Sink.asPublisher(true); @SuppressWarnings("unused") final Publisher publisher = Source.from(new ArrayList()).runWith(pubSink, materializer); } @Test public void mustBeAbleToUseFuture() throws Exception { final Sink> futSink = Sink.head(); final List list = Collections.singletonList(1); final Future future = Source.from(list).runWith(futSink, materializer); assert Await.result(future, Duration.create("1 second")).equals(1); } @Test public void mustBeAbleToUseFold() throws Exception { Sink> foldSink = Sink.fold(0, new Function2() { @Override public Integer apply(Integer arg1, Integer arg2) throws Exception { return arg1 + arg2; } }); @SuppressWarnings("unused") Future integerFuture = Source.from(new ArrayList()).runWith(foldSink, materializer); } @Test public void mustBeAbleToUseActorRefSink() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Sink 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"); } @Test public void mustBeAbleToCombine() throws Exception { final JavaTestKit probe1 = new JavaTestKit(system); final JavaTestKit probe2 = new JavaTestKit(system); final Sink sink1 = Sink.actorRef(probe1.getRef(), "done1"); final Sink sink2 = Sink.actorRef(probe2.getRef(), "done2"); final Sink sink = Sink.combine(sink1, sink2, new ArrayList>(), new Function, BoxedUnit>>() { public Graph, BoxedUnit> apply(Integer elem) { 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"); } public void mustSuitablyOverrideAttributeHandlingMethods() { @SuppressWarnings("unused") final Sink> s = Sink. head().withAttributes(Attributes.name("")).addAttributes(Attributes.asyncBoundary()).named(""); } }