25468: added examples for Stream # cycle operator. (#26163)

This commit is contained in:
Seeta Ramayya 2019-01-02 15:46:13 +01:00 committed by Arnout Engelen
parent f3b7d316b2
commit 1cae9b0d44
3 changed files with 57 additions and 2 deletions

View file

@ -40,6 +40,7 @@ import java.util.stream.Stream;
import static akka.NotUsed.notUsed;
import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription;
import static akka.stream.testkit.TestPublisher.ManualProbe;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
@SuppressWarnings("serial")
@ -769,6 +770,32 @@ public class SourceTest extends StreamTest {
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
}
@Test
public void cycleSourceMustGenerateSameSequenceInRepeatedFashion() throws Exception {
//#cycle
final Source<Integer, NotUsed> source = Source.cycle(() -> Arrays.asList(1, 2, 3).iterator());
CompletionStage<List<Integer>> result = source.grouped(9).runWith(Sink.head(), materializer);
List<Integer> emittedValues = result.toCompletableFuture().get();
assertThat(emittedValues, is(Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3)));
//#cycle
}
@Test(expected = IllegalArgumentException.class)
public void cycleSourceMustThr() throws Throwable {
try {
//#cycle-error
Iterator<Integer> emptyIterator = Collections.<Integer>emptyList().iterator();
Source.cycle(() -> emptyIterator)
.runWith(Sink.head(), materializer)
// stream will be terminated with IllegalArgumentException
//#cycle-error
.toCompletableFuture().get();
} catch (ExecutionException e) {
throw e.getCause();
}
}
@Test
public void mustBeAbleToUseMerge() throws Exception {
final TestKit probe = new TestKit(system);