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>
|
2018-01-16 14:29:03 +00:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2018-01-16 14:29:03 +00:00
|
|
|
package jdocs.stream.javadsl.cookbook;
|
|
|
|
|
|
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;
|
2018-01-16 14:29:03 +00:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class RecipeSourceFromFunction extends RecipeTest {
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
2019-01-12 04:00:53 +08:00
|
|
|
system =
|
|
|
|
|
ActorSystem.create(
|
|
|
|
|
"RecipeSourceFromFunction",
|
|
|
|
|
ConfigFactory.parseString(
|
2022-12-02 04:53:48 -08:00
|
|
|
"pekko.loglevel=DEBUG\nakka.loggers = [org.apache.pekko.testkit.TestEventListener]"));
|
2018-01-16 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
|
|
|
|
TestKit.shutdownActorSystem(system);
|
|
|
|
|
system = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void beMappingOfRepeat() throws Exception {
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
final String builderFunction() {
|
|
|
|
|
return UUID.randomUUID().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2019-01-12 04:00:53 +08:00
|
|
|
// #source-from-function
|
|
|
|
|
final Source<String, NotUsed> source =
|
|
|
|
|
Source.repeat(NotUsed.getInstance()).map(elem -> builderFunction());
|
|
|
|
|
// #source-from-function
|
2018-01-16 14:29:03 +00:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final List<String> result =
|
2019-08-23 18:19:27 +02:00
|
|
|
source
|
|
|
|
|
.take(2)
|
|
|
|
|
.runWith(Sink.seq(), system)
|
|
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(3, TimeUnit.SECONDS);
|
2018-01-16 14:29:03 +00:00
|
|
|
|
|
|
|
|
Assert.assertEquals(2, result.size());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|