Java Code Examples for reactor.core.scheduler.Schedulers#newParallel()

The following examples show how to use reactor.core.scheduler.Schedulers#newParallel() . 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: StepVerifierTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void gh783_deferredAdvanceTime() {
	int size = 61;
	Scheduler parallel = Schedulers.newParallel("gh-783");
	StepVerifier.withVirtualTime(() -> Flux.range(1, 10)
	                                       .take(size)
	                                       .subscribeOn(parallel)
	                                       .flatMap(message -> {
		                                       Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
		                                       return interval.map( tick -> message);
	                                       }, 30,1)
	                                       .take(size)
	                                       .collectList()
	)
	            .thenAwait(Duration.ofHours(2))
	            .consumeNextWith(list -> assertThat(list).hasSize(size))
	            .expectComplete()
	            .verify();
}
 
Example 2
Source File: StepVerifierTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void gh783() {
	int size = 1;
	Scheduler parallel = Schedulers.newParallel("gh-783");
	StepVerifier.withVirtualTime(() -> Flux.just("Oops")
	                                       .take(size)
	                                       .subscribeOn(parallel)
	                                       .flatMap(message -> {
		                                       Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
		                                       return interval.map( tick -> message);
	                                       })
	                                       .take(size)
	                                       .collectList()
	)
	            .thenAwait(Duration.ofHours(1))
	            .consumeNextWith(list -> assertThat(list).hasSize(size))
	            .verifyComplete();
}
 
Example 3
Source File: StepVerifierTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
//FIXME this case of doubly-nested schedules is still not fully fixed
public void gh783_withInnerFlatmap() {
	int size = 61;
	Scheduler parallel = Schedulers.newParallel("gh-783");
	StepVerifier.withVirtualTime(() -> Flux.range(1, 10)
	                                       .take(size)
	                                       .subscribeOn(parallel)
	                                       .flatMap(message -> {
		                                       Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
		                                       return interval.flatMap( tick -> Mono.delay(Duration.ofMillis(500))
		                                                                            .thenReturn(message)
		                                                                            .subscribeOn(parallel))
		                                                      .subscribeOn(parallel);
	                                       }, 1,30)
	                                       .take(size)
	                                       .collectList()
	)
	            .thenAwait(Duration.ofMillis(1500 * (size + 10)))
	            .consumeNextWith(list -> assertThat(list).hasSize(size))
	            .expectComplete()
	            .verify(Duration.ofSeconds(5));
}
 
Example 4
Source File: FluxTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void consistentMultithreadingWithPartition() throws InterruptedException {
	Scheduler supplier1 = Schedulers.newParallel("groupByPool", 2);
	Scheduler supplier2 = Schedulers.newParallel("partitionPool", 5);

	CountDownLatch latch = new CountDownLatch(10);

	/*Disposable c = */Flux.range(1, 10)
	                     .groupBy(n -> n % 2 == 0)
	                     .flatMap(stream -> stream.publishOn(supplier1)
	                                            .log("groupBy-" + stream.key()))
	                     .parallel(5)
	                     .runOn(supplier2)
	                     .sequential()
	                     .publishOn(asyncGroup)
	                     .log("join")
	                     .subscribe(t -> {
		                   latch.countDown();
	                   });


	latch.await(30, TimeUnit.SECONDS);
	assertThat("Not totally dispatched: " + latch.getCount(), latch.getCount() == 0);
	supplier1.dispose();
	supplier2.dispose();
}
 
Example 5
Source File: FluxBlackboxProcessorVerification.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
	Flux<Integer> transformFlux(Flux<Integer> f) {
		Flux<String> otherStream = Flux.just("test", "test2", "test3");
//		System.out.println("Providing new downstream");

		Scheduler asyncGroup = Schedulers.newParallel("flux-p-tck", 2);

		BiFunction<Integer, String, Integer> combinator = (t1, t2) -> t1;

		return f.publishOn(sharedGroup)
		        .parallel(2)
		        .groups()
		        .flatMap(stream -> stream.publishOn(asyncGroup)
				                          .doOnNext(this::monitorThreadUse)
				                          .scan((prev, next) -> next)
				                          .map(integer -> -integer)
				                          .filter(integer -> integer <= 0)
				                          .map(integer -> -integer)
				                          .bufferTimeout(batch, Duration.ofMillis(50))
				                          .flatMap(Flux::fromIterable)
				                          .flatMap(i -> Flux.zip(Flux.just(i), otherStream, combinator))
				 )
		        .publishOn(sharedGroup)
		        .doAfterTerminate(asyncGroup::dispose)
		        .doOnError(Throwable::printStackTrace);
	}
 
Example 6
Source File: FluxTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * https://gist.github.com/nithril/444d8373ce67f0a8b853 Contribution by Nicolas Labrot
 * @throws InterruptedException on interrupt
 */
@Test
public void testParallelWithJava8StreamsInput() throws InterruptedException {
	Scheduler supplier = Schedulers.newParallel("test-p", 2);

	int max = ThreadLocalRandom.current()
	                           .nextInt(100, 300);
	CountDownLatch countDownLatch = new CountDownLatch(max);

	Flux<Integer> worker = Flux.range(0, max)
	                                 .publishOn(asyncGroup);
	worker.parallel(2)
	      .runOn(supplier)
	      .map(v -> v)
	      .subscribe(v -> countDownLatch.countDown());

	countDownLatch.await(10, TimeUnit.SECONDS);
	Assert.assertEquals(0, countDownLatch.getCount());
}
 
Example 7
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	scheduler = Schedulers.newParallel(discardScenario.scenarioDescription + "DiscardScheduler", discardScenario.subscriptionsNumber + 1);
	scheduler.start();

	tracker = new MemoryUtils.OffHeapDetector();
	Hooks.onNextDropped(Tracked::safeRelease);
	Hooks.onErrorDropped(e -> {});
	Hooks.onOperatorError((e, v) -> null);
}
 
Example 8
Source File: FluxSpecTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
	public void whenProcessorIsStreamed() {
//		"When a processor is streamed"
//		given: "a source composable and a async downstream"
		FluxIdentityProcessor<Integer> source = Processors.replayAll();
		Scheduler scheduler = Schedulers.newParallel("test", 2);

		try {
			Mono<List<Integer>> res = source.subscribeOn(scheduler)
			                                .delaySubscription(Duration.ofMillis(1L))
			                                .log("streamed")
			                                .map(it -> it * 2)
			                                .buffer()
			                                .publishNext();

			res.subscribe();

//		when: "the source accepts a value"
			source.onNext(1);
			source.onNext(2);
			source.onNext(3);
			source.onNext(4);
			source.onComplete();

//		then: "the res is passed on"
			assertThat(res.block()).containsExactly(2, 4, 6, 8);
		}
		finally {
			scheduler.dispose();
		}
	}
 
Example 9
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void allocatedReleasedOrAbortedIfCancelRequestRace(int round, AtomicInteger newCount, AtomicInteger releasedCount, boolean cancelFirst) throws InterruptedException {
    Scheduler scheduler = Schedulers.newParallel("poolable test allocator");

    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.defer(() -> Mono.delay(Duration.ofMillis(50)).thenReturn(new PoolableTest(newCount.incrementAndGet())))
                .subscribeOn(scheduler),
            pt -> releasedCount.incrementAndGet());
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //acquire the only element and capture the subscription, don't request just yet
    CountDownLatch latch = new CountDownLatch(1);
    final BaseSubscriber<PoolableTest> baseSubscriber = new BaseSubscriber<PoolableTest>() {
        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            //don't request
            latch.countDown();
        }
    };
    pool.withPoolable(Mono::just).subscribe(baseSubscriber);
    latch.await();

    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    if (cancelFirst) {
        executorService.submit(baseSubscriber::cancel);
        executorService.submit(baseSubscriber::requestUnbounded);
    }
    else {
        executorService.submit(baseSubscriber::requestUnbounded);
        executorService.submit(baseSubscriber::cancel);
    }

    //release due to cancel is async, give it a bit of time
    await().atMost(100, TimeUnit.MILLISECONDS).with().pollInterval(10, TimeUnit.MILLISECONDS)
           .untilAsserted(() -> assertThat(releasedCount)
                   .as("released vs created in round " + round + (cancelFirst? " (cancel first)" : " (request first)"))
                   .hasValue(newCount.get()));
}
 
Example 10
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void allocatedReleasedOrAbortedIfCancelRequestRace(int round, AtomicInteger newCount, AtomicInteger releasedCount, boolean cancelFirst) throws InterruptedException {
    Scheduler scheduler = Schedulers.newParallel("poolable test allocator");

    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.defer(() -> Mono.delay(Duration.ofMillis(50)).thenReturn(new PoolableTest(newCount.incrementAndGet())))
                .subscribeOn(scheduler),
            pt -> releasedCount.incrementAndGet());
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //acquire the only element and capture the subscription, don't request just yet
    CountDownLatch latch = new CountDownLatch(1);
    final BaseSubscriber<PooledRef<PoolableTest>> baseSubscriber = new BaseSubscriber<PooledRef<PoolableTest>>() {
        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            //don't request
            latch.countDown();
        }
    };
    pool.acquire().subscribe(baseSubscriber);
    latch.await();

    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    if (cancelFirst) {
        executorService.submit(baseSubscriber::cancel);
        executorService.submit(baseSubscriber::requestUnbounded);
    }
    else {
        executorService.submit(baseSubscriber::requestUnbounded);
        executorService.submit(baseSubscriber::cancel);
    }

    //release due to cancel is async, give it a bit of time
    await().atMost(200, TimeUnit.MILLISECONDS).with().pollInterval(10, TimeUnit.MILLISECONDS)
           .untilAsserted(() -> assertThat(releasedCount)
                   .as("released vs created in round " + round + (cancelFirst? " (cancel first)" : " (request first)"))
                   .hasValue(newCount.get()));
}
 
Example 11
Source File: PollingTopicMessageRetriever.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
public PollingTopicMessageRetriever(RetrieverProperties retrieverProperties,
                                    TopicMessageRepository topicMessageRepository) {
    this.retrieverProperties = retrieverProperties;
    this.topicMessageRepository = topicMessageRepository;
    int threadCount = retrieverProperties.getThreadMultiplier() * Runtime.getRuntime().availableProcessors();
    scheduler = Schedulers.newParallel("retriever", threadCount, true);
}
 
Example 12
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void allocatedReleasedOrAbortedIfCancelRequestRace(int round, AtomicInteger newCount, AtomicInteger releasedCount, boolean cancelFirst) throws InterruptedException {
    Scheduler scheduler = Schedulers.newParallel("poolable test allocator");

    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.defer(() -> Mono.delay(Duration.ofMillis(50)).thenReturn(new PoolableTest(newCount.incrementAndGet())))
                .subscribeOn(scheduler),
            pt -> releasedCount.incrementAndGet());
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //acquire the only element and capture the subscription, don't request just yet
    CountDownLatch latch = new CountDownLatch(1);
    final BaseSubscriber<PoolableTest> baseSubscriber = new BaseSubscriber<PoolableTest>() {
        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            //don't request
            latch.countDown();
        }
    };
    pool.withPoolable(Mono::just).subscribe(baseSubscriber);
    latch.await();

    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    if (cancelFirst) {
        executorService.submit(baseSubscriber::cancel);
        executorService.submit(baseSubscriber::requestUnbounded);
    }
    else {
        executorService.submit(baseSubscriber::requestUnbounded);
        executorService.submit(baseSubscriber::cancel);
    }

    //release due to cancel is async, give it a bit of time
    await().atMost(100, TimeUnit.MILLISECONDS).with().pollInterval(10, TimeUnit.MILLISECONDS)
           .untilAsserted(() -> assertThat(releasedCount)
                   .as("released vs created in round " + round + (cancelFirst? " (cancel first)" : " (request first)"))
                   .hasValue(newCount.get()));
}
 
Example 13
Source File: SoulWebHandler.java    From soul with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Soul web handler.
 *
 * @param plugins the plugins
 */
public SoulWebHandler(final List<SoulPlugin> plugins) {
    this.plugins = plugins;
    String schedulerType = System.getProperty("soul.scheduler.type", "fixed");
    if (Objects.equals(schedulerType, "fixed")) {
        int threads = Integer.parseInt(System.getProperty(
                "soul.work.threads", "" + Math.max((Runtime.getRuntime().availableProcessors() << 1) + 1, 16)));
        scheduler = Schedulers.newParallel("soul-work-threads", threads);
    } else {
        scheduler = Schedulers.elastic();
    }
}
 
Example 14
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void allocatedReleasedOrAbortedIfCancelRequestRace(int round, AtomicInteger newCount, AtomicInteger releasedCount, boolean cancelFirst) throws InterruptedException {
    Scheduler scheduler = Schedulers.newParallel("poolable test allocator");
    final ExecutorService executorService = Executors.newFixedThreadPool(2);

    try {

        PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
                Mono.defer(() -> Mono.delay(Duration.ofMillis(50)).thenReturn(new PoolableTest(newCount.incrementAndGet())))
                    .subscribeOn(scheduler),
                pt -> releasedCount.incrementAndGet());
        SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

        //acquire the only element and capture the subscription, don't request just yet
        CountDownLatch latch = new CountDownLatch(1);
        final BaseSubscriber<PooledRef<PoolableTest>> baseSubscriber = new BaseSubscriber<PooledRef<PoolableTest>>() {
            @Override
            protected void hookOnSubscribe(Subscription subscription) {
                //don't request
                latch.countDown();
            }
        };
        pool.acquire().subscribe(baseSubscriber);
        latch.await();

        if (cancelFirst) {
            executorService.submit(baseSubscriber::cancel);
            executorService.submit(baseSubscriber::requestUnbounded);
        }
        else {
            executorService.submit(baseSubscriber::requestUnbounded);
            executorService.submit(baseSubscriber::cancel);
        }

        //release due to cancel is async, give it ample time
        await().atMost(200, TimeUnit.MILLISECONDS).with().pollInterval(10, TimeUnit.MILLISECONDS)
               .untilAsserted(() -> assertThat(releasedCount)
                       .as("released vs created in round " + round + (cancelFirst? " (cancel first)" : " (request first)"))
                       .hasValue(newCount.get()));
    }
    finally {
        scheduler.dispose();
        executorService.shutdownNow();
    }
}
 
Example 15
Source File: FluxBlackboxProcessorVerification.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void init() {
	sharedGroup = Schedulers.newParallel("fluxion-tck", 2);
}
 
Example 16
Source File: TransactionalWalletServiceTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 4 votes vote down vote up
void simulateOperations(WalletService walletService) {
   int accounts = 1000;
   int defaultBalance = 1000;
   int iterations = 10;
   int parallelism = 2;

   walletService.initializeDatabase()
   .block();

   // given
   // Clean up just in case
   walletService.removeAllClients()
      .block();

   List<String> clients = walletService.generateClients(accounts, defaultBalance)
      .doOnNext(name -> log.info("Created wallet for: {}", name))
      .collectList()
      .block();

   // when
   Scheduler mongoScheduler = Schedulers
      .newParallel("MongoOperations", parallelism);

   Instant startTime = now();
   Operations operations = Flux.range(0, iterations)
      .flatMap(i -> Mono
         .delay(Duration.ofMillis(rnd.nextInt(10)))
         .publishOn(mongoScheduler)
         .flatMap(_i -> {
            int amount = rnd.nextInt(defaultBalance);
            int from = rnd.nextInt(accounts);
            int to;
            do {
               to = rnd.nextInt(accounts);
            } while (to == from);

            return walletService.transferMoney(
               Mono.just(clients.get(from)),
               Mono.just(clients.get(to)),
               Mono.just(amount));
         }))
      .reduce(Operations.start(), Operations::outcome)
      .block();

   // then
   log.info("--- Results --------------------------------");
   WalletService.Statistics statistics = walletService.reportAllWallets()
      .block();
   log.info("Expected/actual total balance: {}$ / {}$ | Took: {}",
      accounts * defaultBalance, statistics.getTotalBalance(), between(startTime, now()));
   log.info("{}", statistics);
   log.info("{}", operations);

   log.info("Cleaning up database");
   walletService.removeAllClients()
      .block();
}
 
Example 17
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
void consistentThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int i) throws InterruptedException {
    Scheduler allocatorScheduler = Schedulers.newParallel("poolable test allocator");
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1");
    Scheduler racerScheduler = Schedulers.fromExecutorService(
            Executors.newFixedThreadPool(2, (r -> new Thread(r,"racer"))));

    try {
        AtomicReference<String> threadName = new AtomicReference<>();
        AtomicInteger newCount = new AtomicInteger();


        PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
                Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement()))
                    .subscribeOn(allocatorScheduler),
                deliveryScheduler);
        SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

        //the pool is started with one elements, and has capacity for 1.
        //we actually first acquire that element so that next acquire will wait for a release
        PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
        assertThat(uniqueSlot).isNotNull();

        //we prepare next acquire
        Mono<PoolableTest> firstBorrower = Mono.fromDirect(pool.withPoolable(Mono::just));
        Mono<PoolableTest> otherBorrower = Mono.fromDirect(pool.withPoolable(Mono::just));

        CountDownLatch latch = new CountDownLatch(3);

        //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
        acquire1Scheduler.schedule(() -> firstBorrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
                , e -> latch.countDown(), latch::countDown));

        //in parallel, we'll race a second acquire AND release the unique element (each on their dedicated threads)
        //since LIFO we expect that if the release loses, it will server acquire1
        RaceTestUtils.race(
                () -> otherBorrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
                        , e -> latch.countDown(), latch::countDown),
                () -> {
                    uniqueSlot.release().block();
                    latch.countDown();
                },
                racerScheduler);
        latch.await(1, TimeUnit.SECONDS);

        //we expect that, consistently, the poolable is delivered on a `delivery` thread
        assertThat(threadName.get()).as("round #" + i).startsWith("delivery-");

        //2 elements MIGHT be created if the first acquire wins (since we're in auto-release mode)
        assertThat(newCount.get()).as("1 or 2 elements created in round " + i).isIn(1, 2);
    }
    finally {
        allocatorScheduler.dispose();
        deliveryScheduler.dispose();
        acquire1Scheduler.dispose();
        racerScheduler.dispose();
    }
}
 
Example 18
Source File: AbstractReactorTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void loadEnv() {
	ioGroup = Schedulers.newBoundedElastic(4, Integer.MAX_VALUE, "work");
	asyncGroup = Schedulers.newParallel("parallel", 4);
}
 
Example 19
Source File: LinStorModule.java    From linstor-server with GNU General Public License v3.0 4 votes vote down vote up
@Provides
@Singleton
public Scheduler mainWorkerPoolScheduler()
{
    return Schedulers.newParallel("MainWorkerPool");
}
 
Example 20
Source File: ElasticsearchConfig.java    From staccato with Apache License 2.0 4 votes vote down vote up
/**
 * A custom parallel scheduler for executing non-reactive, blocking operations in a reactive threadpool.
 *
 * @return The scheduler
 */
@Bean
public Scheduler scheduler() {
    return Schedulers.newParallel("async-bridge",
            configProps.getRestClientMaxConnectionsTotal(), configProps.getAsyncBridgeThreadPool().isDaemon());
}