2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
|
2016-02-12 01:36:21 +08:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.stream.javadsl.cookbook;
|
2016-02-12 01:36:21 +08:00
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.NotUsed;
|
|
|
|
|
import org.apache.pekko.actor.ActorSystem;
|
|
|
|
|
import org.apache.pekko.stream.javadsl.Sink;
|
|
|
|
|
import org.apache.pekko.stream.javadsl.Source;
|
|
|
|
|
import org.apache.pekko.testkit.javadsl.TestKit;
|
2016-02-12 01:36:21 +08:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class RecipeSeq extends RecipeTest {
|
|
|
|
|
static ActorSystem system;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-02-12 01:36:21 +08:00
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("RecipeLoggingElements");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
2017-03-17 03:02:47 +08:00
|
|
|
TestKit.shutdownActorSystem(system);
|
2016-02-12 01:36:21 +08:00
|
|
|
system = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void drainSourceToList() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
new TestKit(system) {
|
2016-02-12 01:36:21 +08:00
|
|
|
{
|
2016-02-15 13:03:47 +01:00
|
|
|
final Source<String, NotUsed> mySource = Source.from(Arrays.asList("1", "2", "3"));
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-unsafe
|
2016-02-15 13:03:47 +01:00
|
|
|
// Dangerous: might produce a collection with 2 billion elements!
|
2019-08-23 18:19:27 +02:00
|
|
|
final CompletionStage<List<String>> strings = mySource.runWith(Sink.seq(), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-unsafe
|
2016-02-12 01:36:21 +08:00
|
|
|
|
|
|
|
|
strings.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void drainSourceToListWithLimit() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
new TestKit(system) {
|
2016-02-12 01:36:21 +08:00
|
|
|
{
|
2016-02-15 13:03:47 +01:00
|
|
|
final Source<String, NotUsed> mySource = Source.from(Arrays.asList("1", "2", "3"));
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-safe
|
2016-02-12 01:36:21 +08:00
|
|
|
final int MAX_ALLOWED_SIZE = 100;
|
|
|
|
|
|
|
|
|
|
// OK. Future will fail with a `StreamLimitReachedException`
|
|
|
|
|
// if the number of incoming elements is larger than max
|
|
|
|
|
final CompletionStage<List<String>> strings =
|
2019-08-23 18:19:27 +02:00
|
|
|
mySource.limit(MAX_ALLOWED_SIZE).runWith(Sink.seq(), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-safe
|
2016-02-12 01:36:21 +08:00
|
|
|
|
|
|
|
|
strings.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void drainSourceToListWithTake() throws Exception {
|
2017-03-17 03:02:47 +08:00
|
|
|
new TestKit(system) {
|
2016-02-12 01:36:21 +08:00
|
|
|
{
|
2016-02-15 13:03:47 +01:00
|
|
|
final Source<String, NotUsed> mySource = Source.from(Arrays.asList("1", "2", "3"));
|
2016-02-12 01:36:21 +08:00
|
|
|
final int MAX_ALLOWED_SIZE = 100;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-safe
|
|
|
|
|
|
2016-02-12 01:36:21 +08:00
|
|
|
// OK. Collect up until max-th elements only, then cancel upstream
|
|
|
|
|
final CompletionStage<List<String>> strings =
|
2019-08-23 18:19:27 +02:00
|
|
|
mySource.take(MAX_ALLOWED_SIZE).runWith(Sink.seq(), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #draining-to-list-safe
|
2016-02-12 01:36:21 +08:00
|
|
|
|
|
|
|
|
strings.toCompletableFuture().get(1, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|