Java Code Examples for reactor.core.publisher.Flux#range()

The following examples show how to use reactor.core.publisher.Flux#range() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void overlapping() throws Exception {
	//given
	final Flux<Integer> nums = Flux.range(1, 8);

	//when
	final Flux<List<Integer>> windows = nums
			.window(3, 2)
			.flatMap(Flux::collectList);

	//then
	windows
			.as(StepVerifier::create)
			.expectNext(List.of(1, 2, 3))
			.expectNext(List.of(3, 4, 5))
			.expectNext(List.of(5, 6, 7))
			.expectNext(List.of(7, 8))
			.verifyComplete();
}
 
Example 2
@Test
public void gaps() throws Exception {
	//given
	final Flux<Integer> nums = Flux.range(1, 10);

	//when
	final Flux<List<Integer>> windows = nums
			.window(2, 3)
			.flatMap(Flux::collectList);

	//then
	windows
			.as(StepVerifier::create)
			.expectNext(List.of(1, 2))
			.expectNext(List.of(4, 5))
			.expectNext(List.of(7, 8))
			.expectNext(List.of(10))
			.verifyComplete();
}
 
Example 3
Source Project: cyclops   File: ReactiveSeqTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReplay(){
    Flux<Integer> f1 = Flux.range(0,100);
    Flux<Integer> f2 = f1.map(i->i*2);

    System.out.println(f1.count().block());
    System.out.println(f2.count().block());

    ReactiveSeq<String> stream = of("hello","world");
    ReactiveSeq<String> stream1 = stream.map(str->"hello world " + str);
    stream.forEach(System.out::println);
    stream1.forEach(System.out::println);

    ReactiveSeq<Integer> streama = ReactiveSeq.range(1,100);
    ReactiveSeq<Integer> streamb = streama.map(i->i*2);

    System.out.println(streama.count());
    System.out.println(streama.map(i->i*3).zipWithIndex().count());
    System.out.println(streamb.zipWithIndex().count());
}
 
Example 4
@Test
public void overlapping() throws Exception {
	//given
	final Flux<Integer> nums = Flux.range(1, 8);

	//when
	final Flux<List<Integer>> buffers = nums.buffer(3, 2);

	//then
	buffers
			.as(StepVerifier::create)
			.expectNext(List.of(1, 2, 3))
			.expectNext(List.of(3, 4, 5))
			.expectNext(List.of(5, 6, 7))
			.expectNext(List.of(7, 8))
			.verifyComplete();
}
 
Example 5
Source Project: james-project   File: ReactorUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void throttleDownStreamConcurrencyShouldNotExceedWindowMaxSize() {
    int windowMaxSize = 3;
    Duration windowDuration = Duration.ofMillis(100);

    AtomicInteger ongoingProcessing = new AtomicInteger();

    Flux<Integer> originalFlux = Flux.range(0, 10);
    Function<Integer, Publisher<Integer>> longRunningOperation =
        any -> Mono.fromCallable(ongoingProcessing::incrementAndGet)
            .flatMap(i -> Mono.delay(windowDuration.multipliedBy(2)).thenReturn(i))
            .flatMap(i -> Mono.fromRunnable(ongoingProcessing::decrementAndGet).thenReturn(i));

    ImmutableList<Integer> ongoingProcessingUponComputationStart = originalFlux
        .transform(ReactorUtils.<Integer, Integer>throttle()
            .elements(windowMaxSize)
            .per(windowDuration)
            .forOperation(longRunningOperation))
        .collect(Guavate.toImmutableList())
        .block();

    assertThat(ongoingProcessingUponComputationStart)
        .allSatisfy(processingCount -> assertThat(processingCount).isLessThanOrEqualTo(windowMaxSize));
}
 
Example 6
/**
 * TODO Computer factorial (n!) using {@link Flux#reduce(BiFunction)}
 */
@Test
public void factorialUsingReduce() throws Exception {
    //given
    final Flux<Integer> nums = Flux.range(1, 10);

    //when
    final Mono<Integer> factorial = null;  //TODO

    //then
    factorial
            .as(StepVerifier::create)
            .expectNext(
                    (((((((((1 * 2) * 3) * 4) * 5) * 6) * 7) * 8) * 9) * 10)
            )
            .verifyComplete();
}
 
Example 7
@Test
public void reduceWithCustomAccumulator() throws Exception {
    //given
    final Flux<Integer> nums = Flux.range(1, 100);

    //when
    final Mono<BigInteger> factorial = nums.reduce(BigInteger.ONE, (bi, x) -> {
        log.info("bi = {}, x = {}", bi, x);
        return bi.multiply(BigInteger.valueOf(x));
    });

    //then
    factorial
            .as(StepVerifier::create)
            .expectNext(new BigInteger(FACTORIAL_100))
            .verifyComplete();
}
 
Example 8
@Test
public void createAFlux_range() {
  Flux<Integer> intervalFlux = 
      Flux.range(1, 5);
  
  StepVerifier.create(intervalFlux)
      .expectNext(1)
      .expectNext(2)
      .expectNext(3)
      .expectNext(4)
      .expectNext(5)
      .verifyComplete();
}
 
Example 9
@Test
public void createFlux() {
    Flux<String> stream1 = Flux.just("Hello", "world");
    Flux<Integer> stream2 = Flux.fromArray(new Integer[]{1, 2, 3});
    Flux<Integer> stream3 = Flux.range(1, 500);

    Flux<String> emptyStream = Flux.empty();
    Flux<String> streamWithError = Flux.error(new RuntimeException("Hi!"));
}
 
Example 10
Source Project: reactor-core   File: FluxTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void fluxFromFluxSourceDoesntCallAssemblyHook() {
	final Flux<Integer> source = Flux.range(1, 10);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(0);
}
 
Example 11
Source Project: reactor-core   File: FluxTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void delayErrorConcatMapVsFlatMap() {
	Function<Integer, Flux<String>> mapFunction = i -> {
		char c = (char) ('A' + i);
		return Flux.range(1, i + 1)
		           .doOnNext(v -> {
		           	if (i == 3 && v == 3) {
		           		throw new IllegalStateException("boom " + c + v);
		            }
		           })
		    .map(v -> "" + c + "" + v);
	};

	Flux<Integer> source = Flux.range(0, 5);

	Flux<String> concatMap = source.concatMapDelayError(mapFunction)
	                               .materialize()
			                       .map(Object::toString);
	Flux<String> flatMap = source.flatMapDelayError(mapFunction, 2, 32)
	                               .materialize()
			                       .map(Object::toString);

	List<String> signalsConcat = concatMap.collectList().block();
	List<String> signalsFlat = flatMap.collectList().block();

	Assertions.assertThat(signalsConcat)
	          .containsExactlyElementsOf(signalsFlat);
}
 
Example 12
Source Project: reactor-core   File: StepVerifierTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void expectNextsMoreThan6() {
	Flux<Integer> flux = Flux.range(1, 7);

	StepVerifier.create(flux)
	            .expectNext(1, 2, 3, 4, 5, 6, 7)
	            .expectComplete()
	            .verify();
}
 
Example 13
Source Project: reactor-core   File: FluxTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void fluxNextNormalCallsAssemblyHook() {
	Flux<Integer> source = Flux.range(1, 10);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	source.next();
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 14
@Test
public void mapTransformsItemsOnTheFly() throws Exception {
	//given
	final Flux<Integer> numbers = Flux.range(5, 4);

	//when
	final Flux<Integer> even = numbers.map(x -> x * 2);

	//then
	even
			.as(StepVerifier::create)
			.expectNext(10, 12, 14, 16)
			.verifyComplete();
}
 
Example 15
@RequestMapping("/channel1")
public Flux<User> channel1() {
    Flux<Integer> userIdFlux = Flux.range(1, 20);
    return userService.recent(userIdFlux);
}
 
Example 16
@RequestMapping("/channel2")
public Flux<User> channel2() {
    Flux<Integer> userIdFlux = Flux.range(1, 20);
    return userService.recentWithType("VIP", userIdFlux);
}
 
Example 17
public Flux<Integer> emitMultiple() {
    int start = ThreadLocalRandom.current().nextInt(0, 6000);
    return Flux.range(start, 10);
}
 
Example 18
@GetMapping("/flux")
public Flux<Integer> apiFlux() {
    return Flux.range(0, 5);
}
 
Example 19
@RequestManyMapping(value = "/requestMany", mimeType = "application/json")
public Flux<Integer> range(Integer count){
	return Flux.range(0, count);
}
 
Example 20
Source Project: Sentinel   File: FooService.java    License: Apache License 2.0 4 votes vote down vote up
public Flux<Integer> emitMultiple() {
    int start = ThreadLocalRandom.current().nextInt(0, 6000);
    return Flux.range(start, 10);
}