@Test public void mustBeAbleToUseThrottle() throws Exception { Integer result = Source.from(Arrays.asList(0, 1, 2)) .throttle(10, FiniteDuration.create(1, TimeUnit.SECONDS), 10, ThrottleMode.shaping()) .throttle(10, FiniteDuration.create(1, TimeUnit.SECONDS), 10, ThrottleMode.enforcing()) .runWith(Sink.head(), materializer) .toCompletableFuture() .get(3, TimeUnit.SECONDS); assertEquals((Object) 0, result); }
@Test public void mustWorksForTwoStreams() throws Exception { final Flow<Integer, Integer, NotUsed> sharedThrottle = Flow.of(Integer.class) .throttle(1, FiniteDuration.create(1, TimeUnit.DAYS), 1, ThrottleMode.enforcing()); CompletionStage<List<Integer>> result1 = Source.single(1).via(sharedThrottle).via(sharedThrottle).runWith(Sink.seq(), materializer); // If there is accidental shared state then we would not be able to pass through the single // element assertEquals( result1.toCompletableFuture().get(3, TimeUnit.SECONDS), Collections.singletonList(1)); // It works with a new stream, too CompletionStage<List<Integer>> result2 = Source.single(1).via(sharedThrottle).via(sharedThrottle).runWith(Sink.seq(), materializer); assertEquals( result2.toCompletableFuture().get(3, TimeUnit.SECONDS), Collections.singletonList(1)); }