Java Code Examples for reactor.core.scheduler.Scheduler#schedule()
The following examples show how to use
reactor.core.scheduler.Scheduler#schedule() .
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: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void defaultThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); //the pool is started with no elements, and has capacity for 1 //we prepare to acquire, which would allocate the element Mono<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread //we look at which thread the element was delivered from acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("poolable test allocator-"); }
Example 2
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void defaultThreadDeliveringWhenHasElements() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); pool.warmup().block(); //the pool is started and warmed up with one available element //we prepare to acquire it Mono<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread and see from which thread the element was delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("acquire-"); }
Example 3
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void consistentThreadDeliveringWhenHasElements() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); //the pool is started with one available element //we prepare to acquire it Mono<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread and see from which thread the element was delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 4
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void defaultThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig); //the pool is started with no elements, and has capacity for 1 //we prepare to acquire, which would allocate the element Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread //we look at which thread the element was delivered from acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("poolable test allocator-"); }
Example 5
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void consistentThreadDeliveringWhenHasElements() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig); //the pool is started with one available element //we prepare to acquire it Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread and see from which thread the element was delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 6
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void defaultThreadDeliveringWhenHasElements() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig); pool.warmup().block(); //the pool is started and warmed up with one available element //we prepare to acquire it Mono<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread and see from which thread the element was delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("acquire-"); }
Example 7
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void consistentThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig); //the pool is started with no elements, and has capacity for 1 //we prepare to acquire, which would allocate the element Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread //we look at which thread the element was delivered from acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 8
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void defaultThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); //the pool is started with no elements, and has capacity for 1 //we prepare to acquire, which would allocate the element Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread //we look at which thread the element was delivered from acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("poolable test allocator-"); }
Example 9
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void consistentThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); //the pool is started with no elements, and has capacity for 1 //we prepare to acquire, which would allocate the element Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread //we look at which thread the element was delivered from acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 10
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 6 votes |
@Test void consistentThreadDeliveringWhenHasElements() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig); //the pool is started with one available element //we prepare to acquire it Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually request the acquire from a separate thread and see from which thread the element was delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 11
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void defaultThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); 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> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .isEqualTo("release"); }
Example 12
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void consistentThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), 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> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 13
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void consistentThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), deliveryScheduler); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(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> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 14
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void defaultThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(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> borrower = Mono.fromDirect(pool.withPoolable(Mono::just)); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .isEqualTo("release"); }
Example 15
Source File: SchedulerUtils.java From rsocket-java with Apache License 2.0 | 5 votes |
public static void warmup(Scheduler scheduler, int times) throws InterruptedException { scheduler.start(); // warm up CountDownLatch latch = new CountDownLatch(times); for (int i = 0; i < times; i++) { scheduler.schedule(latch::countDown); } latch.await(5, TimeUnit.SECONDS); }
Example 16
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void consistentThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { Scheduler deliveryScheduler = Schedulers.newSingle("delivery"); AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator")), 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<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .startsWith("delivery-"); }
Example 17
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 5 votes |
@Test void defaultThreadDeliveringWhenNoElementsAndFull() throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); Scheduler acquireScheduler = Schedulers.newSingle("acquire"); Scheduler releaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(PoolableTest::new) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(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<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown)); //after a short while, we release the acquired unique element from a third thread releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(threadName.get()) .isEqualTo("release"); }
Example 18
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 4 votes |
void defaultThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int round, AtomicInteger releaserWins, AtomicInteger borrowerWins) throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); AtomicInteger newCount = new AtomicInteger(); Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1"); Scheduler racerReleaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerRelease")))); Scheduler racerAcquireScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerAcquire")))); Scheduler allocatorScheduler = Schedulers.newParallel("poolable test allocator"); try { PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement())) .subscribeOn(allocatorScheduler)); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(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> borrower = 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(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()) , e -> latch.countDown(), latch::countDown)); //in parallel, we'll both attempt concurrent acquire AND release the unique element (each on their dedicated threads) racerAcquireScheduler.schedule(() -> { pool.acquire().block(); latch.countDown(); }, 100, TimeUnit.MILLISECONDS); racerReleaseScheduler.schedule(() -> { uniqueSlot.release().block(); latch.countDown(); }, 100, TimeUnit.MILLISECONDS); assertThat(latch.await(1, TimeUnit.SECONDS)).as("1s").isTrue(); assertThat(newCount).as("created 1 poolable in round " + round).hasValue(1); //we expect that sometimes the race will let the second borrower thread drain, which would mean first borrower //will get the element delivered from racerAcquire thread. Yet the rest of the time it would get drained by racerRelease. if (threadName.get().startsWith("racerRelease")) releaserWins.incrementAndGet(); else if (threadName.get().startsWith("racerAcquire")) borrowerWins.incrementAndGet(); else System.out.println(threadName.get()); } finally { acquire1Scheduler.dispose(); racerAcquireScheduler.dispose(); racerReleaseScheduler.dispose(); allocatorScheduler.dispose(); } }
Example 19
Source File: SimpleLifoPoolTest.java From reactor-pool with Apache License 2.0 | 4 votes |
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 two more acquires Mono<PooledRef<PoolableTest>> firstBorrower = pool.acquire(); Mono<PooledRef<PoolableTest>> secondBorrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we'll enqueue a first acquire from a first thread //in parallel, we'll race a second acquire AND release the unique element (each on their dedicated threads) //we expect the release might sometimes win, which would mean acquire 1 would get served. mostly we want to verify delivery thread though acquire1Scheduler.schedule(() -> firstBorrower.subscribe(v -> threadName.compareAndSet(null, Thread.currentThread().getName()) , e -> latch.countDown(), latch::countDown)); RaceTestUtils.race(() -> secondBorrower.subscribe(v -> threadName.compareAndSet(null, Thread.currentThread().getName()) , e -> latch.countDown(), latch::countDown), uniqueSlot.release()::block); 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-"); //we expect that only 1 element was created assertThat(newCount).as("elements created in round " + i).hasValue(1); } finally { allocatorScheduler.dispose(); deliveryScheduler.dispose(); racerScheduler.dispose(); acquire1Scheduler.dispose(); } }
Example 20
Source File: SimpleFifoPoolTest.java From reactor-pool with Apache License 2.0 | 4 votes |
void defaultThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int round, AtomicInteger releaserWins, AtomicInteger borrowerWins) throws InterruptedException { AtomicReference<String> threadName = new AtomicReference<>(); AtomicInteger newCount = new AtomicInteger(); Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1"); Scheduler racerReleaseScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerRelease")))); Scheduler racerAcquireScheduler = Schedulers.fromExecutorService( Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerAcquire")))); PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1, Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement())) .subscribeOn(Schedulers.newParallel("poolable test allocator"))); SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(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<PooledRef<PoolableTest>> borrower = pool.acquire(); CountDownLatch latch = new CountDownLatch(1); //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered acquire1Scheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()) , e -> latch.countDown(), latch::countDown)); //in parallel, we'll both attempt concurrent acquire AND release the unique element (each on their dedicated threads) racerAcquireScheduler.schedule(pool.acquire()::block, 100, TimeUnit.MILLISECONDS); racerReleaseScheduler.schedule(uniqueSlot.release()::block, 100, TimeUnit.MILLISECONDS); latch.await(1, TimeUnit.SECONDS); assertThat(newCount).as("created 1 poolable in round " + round).hasValue(1); //we expect that sometimes the race will let the second borrower thread drain, which would mean first borrower //will get the element delivered from racerAcquire thread. Yet the rest of the time it would get drained by racerRelease. if (threadName.get().startsWith("racerRelease")) releaserWins.incrementAndGet(); else if (threadName.get().startsWith("racerAcquire")) borrowerWins.incrementAndGet(); else System.out.println(threadName.get()); }