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

The following examples show how to use reactor.core.publisher.Flux#range() . 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
Source File: R071_Reduce.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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 2
Source File: R041_Window.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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 3
Source File: R041_Window.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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 4
Source File: ReactiveSeqTest.java    From cyclops with 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 5
Source File: R040_Buffer.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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 6
Source File: ReactorUtilsTest.java    From james-project with 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 7
Source File: R071_Reduce.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 8
Source File: R030_MapAndFilter.java    From reactor-workshop with GNU General Public License v3.0 5 votes vote down vote up
@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 9
Source File: FluxTests.java    From reactor-core with 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 10
Source File: FluxCreationTests.java    From spring-in-action-5-samples with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: ReactorEssentialsTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@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 12
Source File: FluxTests.java    From reactor-core with 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 13
Source File: FluxTests.java    From reactor-core with 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 14
Source File: StepVerifierTests.java    From reactor-core with 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 15
Source File: FooService.java    From Sentinel with 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);
}
 
Example 16
Source File: PortalController.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/channel1")
public Flux<User> channel1() {
    Flux<Integer> userIdFlux = Flux.range(1, 20);
    return userService.recent(userIdFlux);
}
 
Example 17
Source File: DispatchHandlerTests.java    From spring-cloud-sockets with Apache License 2.0 4 votes vote down vote up
@RequestManyMapping(value = "/requestMany", mimeType = "application/json")
public Flux<Integer> range(Integer count){
	return Flux.range(0, count);
}
 
Example 18
Source File: WebFluxTestController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@GetMapping("/flux")
public Flux<Integer> apiFlux() {
    return Flux.range(0, 5);
}
 
Example 19
Source File: FooService.java    From Sentinel-Dashboard-Nacos with 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);
}
 
Example 20
Source File: PortalController.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/channel2")
public Flux<User> channel2() {
    Flux<Integer> userIdFlux = Flux.range(1, 20);
    return userService.recentWithType("VIP", userIdFlux);
}